#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;
int norm(int a) {
  if (a > mod) a -= mod;
  if (a < 0) a += mod;
  return a;
}
int _plus(int a, int b) { return norm(norm(a) + norm(b)); }
pair<int, int> _plus(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(_plus(a.first, b.first), a.second + b.second);
}
pair<int, int> _sub(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(_plus(a.first, -b.first), a.second - b.second);
}
int mul(int a, int b) { return 1ll * norm(a) * norm(b) % mod; }
struct node {
  int val, id;
} a[maxn];
pair<int, int> bit[maxn];
int n;
int lowbit(int x) { return x & -x; }
void update(int x, pair<int, int> val) {
  while (x <= n) {
    bit[x] = _plus(bit[x], val);
    x += lowbit(x);
  }
}
pair<int, int> ask(int x) {
  pair<int, int> res(0, 0);
  while (x) {
    res = _plus(res, bit[x]);
    x -= lowbit(x);
  }
  return res;
}
bool cmp(node a, node b) {
  if (a.val == b.val) return a.id < b.id;
  return a.val < b.val;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].val);
    a[i].id = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    update(a[i].id, pair<int, int>(a[i].id, 1));
    pair<int, int> pre = ask(a[i].id);
    pair<int, int> suf = _sub(ask(n), ask(a[i].id - 1));
    ans = _plus(ans, mul(mul(pre.first, a[i].val), n + 1 - a[i].id));
    ans = _plus(
        ans,
        mul(mul(_plus(mul(n + 1, suf.second), -suf.first), a[i].val), a[i].id));
    ans = _plus(ans, -mul(mul(a[i].id, n - a[i].id + 1), a[i].val));
  }
  printf("%d\n", ans);
  return 0;
}
