#include <bits/stdc++.h>
using namespace std;
const long long INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 500005, M = 1000000007;
long long n, v[nmax], aib1[nmax], aib2[nmax];
pair<int, int> e[nmax];
void update(long long idx, long long val, long long aib[]) {
  while (idx <= n) {
    aib[idx] = (aib[idx] + val) % M;
    idx += (idx & (-idx));
  }
}
long long read(long long idx, long long aib[]) {
  long long sum = 0;
  while (idx) {
    sum = (sum + aib[idx]) % M;
    idx -= (idx & (-idx));
  }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    e[i] = {v[i], i};
  }
  sort(e + 1, e + n + 1);
  long long sol = 0;
  for (int i = 1; i <= n; ++i) {
    long long pos = e[i].second;
    long long sum = (pos * (n - pos + 1)) % M;
    sol = (sol + sum * e[i].first) % M;
    sum = (read(pos - 1, aib1) * (n - pos + 1)) % M;
    sol = (sol + sum * e[i].first) % M;
    sum = read(n, aib2) - read(pos, aib2);
    if (sum < 0) sum += M;
    sum = (sum * pos) % M;
    sol = (sol + sum * e[i].first) % M;
    update(pos, pos, aib1);
    update(pos, n - pos + 1, aib2);
  }
  cout << sol;
  return 0;
}
