#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[500005];
struct aa {
  long long x, i;
} b[500005];
struct zszs {
  long long c[500005];
  void add(long long x, long long y) {
    for (long long i = x; i <= n; i += (i & (-i))) {
      c[i] += y;
      if (c[i] >= 1000000007) c[i] -= 1000000007;
    }
  }
  long long sum(long long x) {
    long long ans = 0;
    for (long long i = x; i > 0; i -= (i & (-i))) ans += c[i];
    ans %= 1000000007;
    return ans;
  }
} c, d;
bool cmp(aa x, aa y) { return x.x < y.x; }
void rd() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    b[i].x = a[i];
    b[i].i = i;
  }
  sort(b + 1, b + n + 1, cmp);
}
long long ans = 0;
signed main() {
  rd();
  for (long long i = 1; i <= n; i++) {
    long long x = b[i].i;
    ans += (x) * (n - x + 1) % 1000000007 * a[x] % 1000000007;
    long long sl = c.sum(x - 1), sr = d.sum(n) - d.sum(x);
    ans += sl * (n - x + 1) % 1000000007 * a[x] % 1000000007;
    ans += sr * x % 1000000007 * a[x] % 1000000007;
    ans %= 1000000007;
    c.add(x, x);
    d.add(x, n - x + 1);
  }
  cout << ans;
  return 0;
}
