#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, ch;
  while (!isdigit(ch = getchar()))
    ;
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}
const int MAXN = 500000;
const int mod = 1000000007;
int n;
int a[MAXN + 1];
int v[MAXN + 1];
int T[MAXN + 1];
inline void Add(int& a, int b) {
  a += b;
  a -= a >= mod ? mod : 0;
}
inline void Modify(int p, int x) {
  while (p <= MAXN) Add(T[p], x), p += p & -p;
}
inline int Query(int p) {
  int res = 0;
  while (p) Add(res, T[p]), p ^= p & -p;
  return res;
}
inline int pos(int x) { return lower_bound(v + 1, v + 1 + n, x) - v; }
int main() {
  n = read();
  int res = 0;
  for (int i = 1; i <= n; i++)
    a[i] = v[i] = read(), Add(res, 1LL * a[i] * i % mod * (n - i + 1) % mod);
  sort(v + 1, v + 1 + n);
  for (int i = 1; i <= n; i++) a[i] = pos(a[i]);
  for (int i = 1; i <= n; i++) {
    Add(res, 1LL * (n - i + 1) * (Query(n) - Query(a[i]) + mod) % mod);
    Modify(a[i], 1LL * v[a[i]] * i % mod);
  }
  memset(T, 0, sizeof(T));
  for (int i = n; i >= 1; i--) {
    Add(res, 1LL * i * (Query(n) - Query(a[i]) + mod) % mod);
    Modify(a[i], 1LL * v[a[i]] * (n - i + 1) % mod);
  }
  printf("%d\n", res);
  return 0;
}
