#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 1e9;
const long long llf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;
int n;
long long a[maxn], c[maxn], tr[maxn];
void add(int x, long long k) {
  while (x <= n) {
    tr[x] = (tr[x] + k) % mod;
    x += x & -x;
  }
}
long long dor(int x) {
  long long ret = 0;
  while (x) {
    ret = (ret + tr[x]) % mod;
    x -= x & -x;
  }
  return ret;
}
long long ans = 0;
void solve() {
  memset(tr, 0, sizeof(tr));
  for (int i = 1; i <= n; i++) {
    long long t = dor(a[i]) * (n - i + 1) % mod;
    ans += c[a[i]] * t % mod;
    ans %= mod;
    add(a[i], i);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += a[i] * (n - i + 1) % mod * i % mod;
    ans %= mod;
    c[i] = a[i];
  }
  sort(c + 1, c + n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(c + 1, c + n + 1, a[i]) - c;
  }
  solve();
  reverse(a + 1, a + n + 1);
  solve();
  cout << ans << endl;
  return 0;
}
