#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <typename T>
void maxi(T &a, const T &b) {
  if (b > a) a = b;
}
template <typename T>
void mini(T &a, const T &b) {
  if (b < a) a = b;
}
template <typename T>
void no(const T &a) {
  cout << a;
  exit(0);
}
constexpr LL maxn = 540000;
constexpr LL mod = 1e9 + 7;
LL a[maxn];
LL added[maxn << 2], ssum[maxn << 2];
LL added2[maxn << 2], ssum2[maxn << 2];
void add(LL v, LL tl, LL tr, LL x) {
  added[v] += x;
  ssum[v] += x * (tr - tl + 1);
}
void push_down(LL v, LL tl, LL tr) {
  add((v << 1), tl, ((tl + tr) >> 1), added[v]);
  add((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, added[v]);
  added[v] = 0;
}
void add(LL v, LL tl, LL tr, LL L, LL R, LL x) {
  if (tl >= L and tr <= R)
    add(v, tl, tr, x);
  else {
    push_down(v, tl, tr);
    if (R > ((tl + tr) >> 1))
      add((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, L, R, x);
    if (L <= ((tl + tr) >> 1)) add((v << 1), tl, ((tl + tr) >> 1), L, R, x);
    ssum[v] = ssum[(v << 1)] + ssum[(v << 1 | 1)];
  }
}
LL sum(LL v, LL tl, LL tr, LL L, LL R) {
  if (tl >= L and tr <= R) return ssum[v];
  LL res = 0;
  push_down(v, tl, tr);
  if (R > ((tl + tr) >> 1))
    res += sum((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, L, R);
  if (L <= ((tl + tr) >> 1)) res += sum((v << 1), tl, ((tl + tr) >> 1), L, R);
  return res;
}
void add2(LL v, LL tl, LL tr, LL x) {
  added2[v] += x;
  ssum2[v] += x * (tr - tl + 1);
}
void push_down2(LL v, LL tl, LL tr) {
  add2((v << 1), tl, ((tl + tr) >> 1), added2[v]);
  add2((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, added2[v]);
  added2[v] = 0;
}
void add2(LL v, LL tl, LL tr, LL L, LL R, LL x) {
  if (tl >= L and tr <= R)
    add2(v, tl, tr, x);
  else {
    push_down2(v, tl, tr);
    if (R > ((tl + tr) >> 1))
      add2((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, L, R, x);
    if (L <= ((tl + tr) >> 1)) add2((v << 1), tl, ((tl + tr) >> 1), L, R, x);
    ssum2[v] = ssum2[(v << 1)] + ssum2[(v << 1 | 1)];
  }
}
LL sum2(LL v, LL tl, LL tr, LL L, LL R) {
  if (tl >= L and tr <= R) return ssum2[v];
  LL res = 0;
  push_down2(v, tl, tr);
  if (R > ((tl + tr) >> 1))
    res += sum2((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, L, R);
  if (L <= ((tl + tr) >> 1)) res += sum2((v << 1), tl, ((tl + tr) >> 1), L, R);
  return res;
}
vector<pair<LL, LL>> v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  LL n;
  cin >> n;
  for (LL i = 1; i <= n; i += 1) {
    cin >> a[i];
    v.push_back({a[i], i});
  }
  sort(v.begin(), v.end());
  LL res = 0;
  add(1, 1, n, 1, n, 1);
  add2(1, 1, n, 1, n, 1);
  for (auto p : v) {
    LL x1 = sum(1, 1, n, p.second, p.second);
    LL x2 = sum2(1, 1, n, p.second, p.second);
    LL y = (sum(1, 1, n, 1, p.second) - (x1 - 1) * p.second) % mod;
    LL z = (sum2(1, 1, n, p.second, n) - (x2 - 1) * (n - p.second + 1)) % mod;
    res += p.first *
           ((y * (n - p.second + 1) + z * p.second -
             (n - p.second + 1) * p.second) %
                mod +
            mod) %
           mod;
    res %= mod;
    add(1, 1, n, 1, p.second, 1);
    add2(1, 1, n, p.second, n, 1);
  }
  cout << res;
  return 0;
}
