#include <bits/stdc++.h>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n') ch = getchar();
  return ch;
}
const int maxn = 500010, MOD = int(1e9) + 7;
int N, a[maxn];
struct my {
  int v, id;
  bool friend operator<(const my& A, const my& B) {
    return A.v < B.v || (A.v == B.v && A.id < B.id);
  }
} b[maxn];
struct BIT {
  int sum[maxn];
  inline int lowbit(int x) { return x & (-x); }
  void upd(int x, int v) {
    for (; x <= N; x += lowbit(x)) (sum[x] += v) %= MOD;
  }
  int query(int x) {
    int re = 0;
    for (; x; x -= lowbit(x)) (re += sum[x]) %= MOD;
    return re;
  }
} b1, b2;
int main() {
  N = read();
  for (int i = 1; i <= N; i++) a[i] = read(), b[i].v = a[i], b[i].id = i;
  sort(b + 1, b + N + 1);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    int in = b[i].id;
    b1.upd(in, in);
    b2.upd(in, N - in + 1);
    int s1 = b1.query(in), s2 = (b2.query(N) - b2.query(in - 1) + MOD) % MOD;
    ans += 1LL * s1 * (N - in + 1) % MOD * a[in] % MOD;
    ans += 1LL * s2 * in % MOD * a[in] % MOD;
    ans -= 1LL * in * (N - in + 1) % MOD * a[in] % MOD;
    (ans += MOD) %= MOD;
  }
  cout << ans << endl;
  return 0;
}
