#include <bits/stdc++.h>
int in() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') f |= c == '-', c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f ? -x : x;
}
const int N = 5e5 + 5, mod = 1e9 + 7;
int n, id[N], a[N];
inline void add(int &_, int __) {
  _ += __;
  if (_ >= mod) _ -= mod;
}
struct binary_index_tree {
  int a[N];
  void insert(int p, int k) {
    for (; p <= n; p += (p & -p)) add(a[p], k);
  }
  int ask(int p, int ret = 0) {
    for (; p; p -= (p & -p)) add(ret, a[p]);
    return ret;
  }
} bit1, bit2;
inline bool cmp(const int &i, const int &j) { return a[i] > a[j]; }
int main() {
  n = in();
  for (int i = 1; i <= n; ++i) a[i] = in(), id[i] = i;
  std::sort(id + 1, id + 1 + n, cmp);
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    add(res, 1ll * id[i] * (n - id[i] + 1) % mod * a[id[i]] % mod);
    add(res, 1ll * (n - id[i] + 1) * bit1.ask(id[i]) % mod);
    add(res, 1ll * id[i] * (bit2.ask(n) - bit2.ask(id[i]) + mod) % mod);
    bit1.insert(id[i], 1ll * id[i] * a[id[i]] % mod);
    bit2.insert(id[i], 1ll * (n - id[i] + 1) * a[id[i]] % mod);
  }
  printf("%d\n", res);
  return 0;
}
