#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5.1e5;
const int INF = (int)1e9;
const int MOD = (int)(1e9) + 7;
int A[MAXN];
int S[2][MAXN];
struct BIT {
  vector<int> v;
  BIT(int sz) : v(sz + 1) {}
  int query(int n) {
    int ans = 0;
    for (int i = n + 1; i > 0; i -= i & -i) {
      ans += v[i];
      ans %= MOD;
    }
    return ans;
  }
  void update(int n, int u) {
    for (int i = n + 1; i < v.size(); i += i & -i) {
      v[i] += u;
      v[i] %= MOD;
    }
  }
};
int main() {
  int n;
  scanf("%d", &n);
  set<int> unq;
  map<int, int> down;
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
    unq.insert(A[i]);
  }
  vector<int> C(A, A + n);
  int cnt = 1;
  for (int v : unq) down[v] = cnt++;
  for (int i = 0; i < n; i++) A[i] = down[A[i]];
  for (int i = 0; i < 2; i++) {
    BIT B(n);
    for (int j = 0; j < n; j++) {
      S[i][j] = B.query(A[j] - 1);
      B.update(A[j], j + 1);
    }
    reverse(A, A + n);
  }
  reverse(S[1], S[1] + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int add = 0;
    add += (1LL * (n - i) * S[0][i]) % MOD;
    add %= MOD;
    add += (1LL * (i + 1) * S[1][i]) % MOD;
    add %= MOD;
    add += (1LL * (n - i) * (i + 1)) % MOD;
    add %= MOD;
    add = (1LL * add * C[i]) % MOD;
    ans += add;
    ans %= MOD;
  }
  printf("%d\n", ans);
  return 0;
}
