#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 5;
const long long inf = 9e17;
const long long mod = 1e9 + 7;
long long ans, fw[maxn], a[maxn], b[maxn], t[maxn], n;
inline long long lowbit(long long x) { return x & -x; }
inline void update(long long i, long long x) {
  while (i <= n) {
    (fw[i] += x) %= mod;
    i += lowbit(i);
  }
  return;
}
inline long long getsum(long long i) {
  long long sum = 0;
  while (i) {
    (sum += fw[i]) %= mod;
    i -= lowbit(i);
  }
  return sum;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  memcpy(t, a, sizeof(a));
  sort(t + 1, t + 1 + n);
  for (int i = 1; i <= n; i++) b[i] = lower_bound(t + 1, t + 1 + n, a[i]) - t;
  for (int i = 1; i <= n; i++) {
    ans = (ans + a[i] * (n - i + 1) % mod * getsum(b[i]) % mod) % mod;
    update(b[i], i);
  }
  memset(fw, 0, sizeof(fw));
  for (int i = n; i >= 1; i--) {
    ans = (ans + a[i] * i % mod * getsum(b[i]) % mod) % mod;
    update(b[i], n - i + 1);
  }
  for (int i = 1; i <= n; i++)
    ans = (ans + a[i] * i % mod * (n - i + 1) % mod) % mod;
  cout << ans % mod << "\n";
  return 0;
}
