#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
pair<long long, long long> a[N];
long long n, ans(0), base = 1e9 + 7, tr[2][N], i;
void add(int s, int p, int x) {
  for (; p < N; p += p & -p) tr[s][p] += x;
}
long long sum(int s, int p) {
  long long res = 0;
  for (; p; p -= p & -p) res += tr[s][p];
  return res % base;
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    long long p = a[i].second;
    ans += (sum(0, p - 1) * (n + 1 - p) + (sum(1, n) - sum(1, p)) * p +
            p * (n + 1 - p)) %
           base * a[i].first % base;
    add(0, p, p);
    add(1, p, n + 1 - p);
  }
  cout << ans % base;
}
