#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
int modpow(int x, int y, int md = mod) {
  if (y == 0) return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (long long)ret * ret % md;
  if (y & 1) ret = (long long)ret * x % md;
  return ret;
}
char in[10000000];
int itr = 0, llen = 0;
char get() {
  if (itr == llen) llen = fread(in, 1, 10000000, stdin), itr = 0;
  if (llen == 0) return EOF;
  return in[itr++];
}
int getint() {
  int ret = 0;
  char ch = get();
  while (ch < '0' || ch > '9') {
    if (ch == '-') return -getint();
    ch = get();
  }
  while ('0' <= ch && ch <= '9') {
    ret = ret * 10 - 48 + ch;
    ch = get();
  }
  return ret;
}
struct SegTree {
  struct node {
    long long f = 0, add = 0, sz;
  } f[524288 << 1];
  long long l, r, v;
  void pushdown(long long k) {
    f[(k << 1)].add += f[k].add;
    f[(k << 1 | 1)].add += f[k].add;
    f[k].add = 0;
  }
  void build(long long l = 0, long long r = 524288 - 1, long long k = 1,
             long long sz = 524288) {
    long long mid = l + r >> 1;
    f[k].sz = sz;
    if (l == r) return;
    build(l, mid, (k << 1), sz >> 1);
    build(mid + 1, r, (k << 1 | 1), sz >> 1);
  }
  void _add(long long l1 = 0, long long r1 = 524288 - 1, long long k = 1) {
    if (r < l1 || r1 < l) return;
    if (l <= l1 && r1 <= r) {
      f[k].add++;
      return;
    }
    long long L = (k << 1), R = (k << 1 | 1);
    f[L].add += f[k].add;
    f[R].add += f[k].add;
    f[k].add = 0;
    long long mid = l1 + r1 >> 1;
    _add(l1, mid, L);
    _add(mid + 1, r1, R);
    f[k].f = f[L].f + f[R].f + f[L].add * f[L].sz + f[R].add * f[R].sz;
  }
  long long _query(long long l1 = 0, long long r1 = 524288 - 1,
                   long long k = 1) {
    if (r < l1 || r1 < l) return 0;
    if (l <= l1 && r1 <= r) {
      return f[k].f + f[k].add * f[k].sz;
    }
    long long L = (k << 1), R = (k << 1 | 1);
    f[L].add += f[k].add;
    f[R].add += f[k].add;
    f[k].add = 0;
    long long mid = l1 + r1 >> 1;
    long long vl = _query(l1, mid, L);
    long long vr = _query(mid + 1, r1, R);
    f[k].f = f[L].f + f[R].f + f[L].add * f[L].sz + f[R].add * f[R].sz;
    return vl + vr;
  }
  long long query(long long _l, long long _r) {
    l = _l;
    r = _r;
    return _query() % mod;
  }
  void add(long long _l, long long _r, long long _v) {
    l = _l;
    r = _r;
    v = 1;
    _add();
  }
} seg1, seg2;
int n;
long long a[500000 + 5], tmp1, tmp2, tmp3;
int main() {
  n = getint();
  seg1.build();
  seg2.build();
  for (int i = 1; i <= n; i++) a[i] = 1000000ll * getint() + i;
  sort(a + 1, a + n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int pos = a[i] % 1000000;
    long long v = a[i] / 1000000;
    tmp1 = seg1.query(pos, pos) * (n - pos) % mod;
    tmp2 = seg2.query(pos, pos) * (pos - 1) % mod;
    tmp3 = pos;
    tmp3 = tmp3 * (n + 1 - pos) % mod;
    ans += (seg1.query(pos + 1, n) - tmp1) * (v * pos % mod);
    ans += (seg2.query(1, pos - 1) - tmp2) * (v * (n + 1 - pos) % mod);
    ans += tmp3 * v;
    seg1.add(pos, n, 1);
    seg2.add(1, pos, 1);
    ans %= mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
