#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 14, mod = 1e9 + 7;
int n, a[maxn], per[maxn];
struct Fen {
  int f[maxn];
  void upd(int p, int v) {
    for (p++; p < maxn; p += p & -p) (f[p] += v) %= mod;
  }
  int get(int p) {
    int ans = 0;
    for (; p; p ^= p & -p) (ans += f[p]) %= mod;
    return ans;
  }
  int getSuf(int p) { return (get(maxn - 1) - get(p) + mod) % mod; }
} sum, ps;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  iota(per, per + n, 0);
  sort(per, per + n, [](int i, int j) { return a[i] < a[j]; });
  int ans = 0;
  for_each(per, per + n, [&](int i) {
    sum.upd(i, +1);
    ps.upd(i, i);
    ans = (ans + (long long)(n * (long long)sum.getSuf(i + 1) % mod -
                             ps.getSuf(i + 1) + mod) *
                     (i + 1) % mod * a[i]) %
          mod;
    ans = (ans +
           (long long)(sum.get(i + 1) + ps.get(i + 1)) * (n - i) % mod * a[i]) %
          mod;
  });
  cout << ans << '\n';
}
