#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
const long long inf = 0x3f3f3f3f;
const long long mod = 1000000007;
int n, m;
int ls[3 * N], rs[3 * N], lft[3 * N], rgt[3 * N];
long long num[3 * N], tag[3 * N];
int rt, P;
void update(int p) {
  num[p] = ((num[ls[p]] + tag[ls[p]] * (rgt[ls[p]] - lft[ls[p]] + 1)) +
            (num[rs[p]] + tag[rs[p]] * (rgt[rs[p]] - lft[rs[p]] + 1))) %
           mod;
  return;
}
void pushd(int p) {
  if (!tag[p]) return;
  tag[ls[p]] += tag[p];
  tag[rs[p]] += tag[p];
  num[p] = (num[p] + tag[p] * (rgt[p] - lft[p] + 1));
  tag[p] = 0;
  return;
}
int build(int l, int r) {
  int p = ++P;
  tag[p] = num[p] = 0;
  lft[p] = l;
  rgt[p] = r;
  if (l == r) return p;
  int mid = (l + r) >> 1;
  ls[p] = build(l, mid);
  rs[p] = build(mid + 1, r);
  return p;
}
void change(int p, int pos) {
  if (lft[p] == rgt[p]) {
    tag[p]++;
    return;
  }
  pushd(p);
  int mid = (lft[p] + rgt[p]) >> 1;
  if (pos <= mid) {
    tag[rs[p]]++;
    change(ls[p], pos);
  } else
    change(rs[p], pos);
  update(p);
  return;
}
long long query(int pos) {
  if (pos < 1 || pos > n) return 0;
  int p = rt;
  long long ans = 0;
  while (lft[p] < rgt[p]) {
    pushd(p);
    int mid = (lft[p] + rgt[p]) >> 1;
    if (pos > mid) {
      ans += (num[ls[p]] + tag[ls[p]] * (rgt[ls[p]] - lft[ls[p]] + 1));
      ans %= mod;
      p = rs[p];
    } else
      p = ls[p];
  }
  ans += (num[p] + tag[p] * (rgt[p] - lft[p] + 1));
  ans %= mod;
  return ans;
}
long long _query(int pos) {
  int p = rt;
  long long ans = 0;
  while (lft[p] < rgt[p]) {
    pushd(p);
    int mid = (lft[p] + rgt[p]) >> 1;
    if (pos > mid)
      p = rs[p];
    else
      p = ls[p];
  }
  ans += (num[p] + tag[p] * (rgt[p] - lft[p] + 1));
  return ans;
}
struct node {
  long long v;
  int id;
  bool operator<(const node &b) const { return v < b.v; }
};
node a[N];
int main() {
  ios::sync_with_stdio(0);
  int i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].v;
    a[i].id = i;
  }
  sort(a + 1, a + n + 1);
  long long ans = 0;
  rt = build(1, n);
  for (i = 1; i <= n; i++) {
    int x = a[i].id;
    change(rt, x);
    long long k1 = query(x - 1);
    long long k2 = _query(x);
    long long k3 =
        (((num[rt] + tag[rt] * (rgt[rt] - lft[rt] + 1)) - k1 - k2) % mod +
         mod) %
        mod;
    ans += (a[i].v *
                ((n - x + 1) * (k2 * x - k1) % mod +
                 x * (k3 - (n - x) * k2) % mod) %
                mod +
            mod) %
           mod;
    ans %= mod;
  }
  cout << ans;
  return 0;
}
