#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 500002;
struct node {
  int x, id;
  bool operator<(node a) const { return x < a.x; }
} a[N];
int n, i, p, ans, t1[N], t2[N];
void add(int *t, int x, int y) {
  for (; x <= n; x += x & -x) (t[x] += y) %= M;
}
int ask(int *t, int x) {
  int s = 0;
  for (; x; x ^= x & -x) (s += t[x]) %= M;
  return s;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i].x, a[i].id = i;
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    p = a[i].id;
    (ans += (1ll * p * (n - p + 1) + 1ll * ask(t1, p - 1) * (n - p + 1) +
             1ll * p * (ask(t2, n) - ask(t2, p - 1))) %
            M * a[i].x % M) %= M;
    add(t1, p, p), add(t2, p, n - p + 1);
  }
  printf("%d", (ans + M) % M);
}
