#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int MOD = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n;
struct BIT {
  long long c[N];
  void add(int i, int dd) {
    for (; i <= n; i += (i & (-i))) c[i] = (c[i] + dd) % MOD;
  }
  long long ask(int i) {
    long long res = 0;
    for (; i; i -= (i & (-i))) res = (res + c[i]) % MOD;
    return res;
  }
} c1, c2;
int a[N];
pair<int, int> p[N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) p[i] = {read(), i};
  sort(p + 1, p + n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long w = p[i].first;
    int id = p[i].second;
    long long tmp = 0;
    long long lsum = c1.ask(id), lnum = c2.ask(id);
    tmp += lsum * (n - id + 1) % MOD;
    long long rsum = c1.ask(n) - c1.ask(id), rnum = c2.ask(n) - c2.ask(id);
    tmp += ((((long long)n + 1) * rnum % MOD - rsum) % MOD * id) % MOD;
    tmp %= MOD;
    ans = (ans + (tmp + (id * ((long long)n - id + 1) % MOD)) % MOD * w % MOD) %
          MOD;
    c1.add(id, id);
    c2.add(id, 1);
  }
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
  return 0;
}
