#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 5;
int n;
struct node {
  int x, id;
} q[maxn];
bool cmp(node q1, node q2) { return q1.x < q2.x; }
struct BIT {
  long long c[maxn];
  int lowbit(int x) { return x & (-x); }
  void update(int k, int x) {
    for (int i = k; i <= n; i += lowbit(i)) (c[i] += x) %= mod;
  }
  long long query(int k) {
    long long res = 0;
    for (int i = k; i; i -= lowbit(i)) (res += c[i]) %= mod;
    return res;
  }
} T1, T2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &q[i].x);
    q[i].id = i;
  }
  sort(q + 1, q + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    T1.update(q[i].id, q[i].id);
    T2.update(q[i].id, 1);
    ans = (ans + T1.query(q[i].id) * q[i].x % mod * (n - q[i].id + 1) % mod) %
          mod;
    long long tmp = (T2.query(n) - T2.query(q[i].id)) % mod * (n + 1) % mod -
                    (T1.query(n) - T1.query(q[i].id)) % mod;
    ans = (ans + tmp * q[i].x % mod * q[i].id % mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
