#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') y = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * y;
}
void put(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) put(x / 10);
  putchar((x % 10) + 48);
}
long long n, ans;
long long a[500010], b[500010], c[500010];
inline void Add(long long x, long long k) {
  while (x <= n) {
    c[x] = (c[x] + k) % 1000000007;
    x += ((x) & (-(x)));
  }
}
inline long long Ask(long long x) {
  long long res = 0;
  while (x) {
    res = (res + c[x]) % 1000000007;
    x -= ((x) & (-(x)));
  }
  return res;
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) b[i] = a[i] = read();
  sort(b + 1, b + 1 + n);
  for (long long i = 1; i <= n; i++) {
    ans = (ans + i * (n - i + 1) % 1000000007 * a[i] % 1000000007) % 1000000007;
    a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
  }
  for (long long i = 1; i <= n; i++) {
    ans = (ans + b[a[i]] * (n - i + 1) % 1000000007 * Ask(a[i]) % 1000000007) %
          1000000007;
    Add(a[i], i);
  }
  memset(c, 0, sizeof c);
  for (long long i = n; i >= 1; i--) {
    ans =
        (ans + b[a[i]] * i % 1000000007 * Ask(a[i]) % 1000000007) % 1000000007;
    Add(a[i], n - i + 1);
  }
  cout << ans << "\n";
  fclose(stdin);
  fclose(stdout);
  return 0;
}
