#include <bits/stdc++.h>
using namespace std;
long long BIT[500005];
long long sum[2][5000005];
long long n;
void inc(long long pos, long long val) {
  for (; pos < n; pos |= (pos + 1)) {
    BIT[pos] += val;
  }
}
long long summation(long long ind) {
  long long res = 0;
  for (; ind >= 0; ind = (ind & (ind + 1)) - 1) {
    res += BIT[ind];
  }
  return res;
}
int main() {
  cin >> n;
  long long a[n];
  long long b[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  for (long long num = 0; num < 2; num++) {
    memset(BIT, 0, sizeof(BIT));
    for (long long i = 0; i < n; i++) {
      long long z = lower_bound(b, b + n, a[i]) - b;
      sum[num][i] = summation(z) % 1000000007;
      inc(z, i + 1);
    }
    reverse(a, a + n);
  }
  long long ans = 0;
  reverse(sum[1], sum[1] + n);
  for (long long i = 0; i < n; i++) {
    ans = ans + ((a[i] * ((((i + 1) * (n - i)) % 1000000007) +
                          ((sum[0][i] * (n - i)) % 1000000007) +
                          ((sum[1][i] * (i + 1)) % 1000000007))) %
                 1000000007);
    ans = ans % 1000000007;
  }
  cout << ans << "\n";
}
