#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 5e5 + 10;
struct Node {
  int x, id;
} a[maxn];
int n, c1[maxn], c2[maxn];
int lowbit(int x) { return x & (-x); }
int sum(int c[], int x) {
  int ret = 0;
  for (int i = x; i; i -= lowbit(i)) ret = (ret + c[i]) % mod;
  return ret;
}
void update(int c[], int x, int val) {
  for (int i = x; i <= n; i += lowbit(i)) c[i] = (c[i] + val) % mod;
}
bool cmp(Node A, Node B) { return A.x < B.x; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].x), a[i].id = i;
  sort(a + 1, a + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int id = a[i].id;
    update(c1, id, id);
    long long ans1 = 1ll * (n - id + 1) * sum(c1, id) % mod;
    long long ans2 = 1ll * id * (sum(c2, n - id + 1)) % mod;
    ans = (ans + 1ll * a[i].x * (ans1 + ans2) % mod) % mod;
    update(c2, n - id + 1, n - id + 1);
  }
  printf("%lld\n", ans);
}
