#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 100100, MAXA = 1001001001, MOD = 1e9 + 7;
int n, a[MAXN];
long long ans;
struct no {
  int cnt;
  no *l, *r;
  no(int cnt = 0) : cnt(cnt), l(nullptr), r(nullptr) {}
  ~no() {
    if (l) delete l;
    if (r) delete r;
  }
};
typedef no *noptr;
inline int getcnt(no *n) { return (n ? n->cnt : 0); }
void upd(noptr &n, int pos, int cnt, int beg = 1, int end = MAXA) {
  if (!n) n = new no();
  if (beg == end) return void(n->cnt += cnt);
  int mid = (beg + end) >> 1;
  if (pos <= mid)
    upd(n->l, pos, cnt, beg, mid);
  else
    upd(n->r, pos, cnt, mid + 1, end);
  n->cnt = (0LL + getcnt(n->l) + getcnt(n->r)) % MOD;
}
int get(no *n, int a, int b, int beg = 1, int end = MAXA) {
  if (!n || b < beg || end < a) return 0;
  if (a <= beg && end <= b) return n->cnt;
  int mid = (beg + end) >> 1;
  return (get(n->l, a, b, beg, mid) + get(n->r, a, b, mid + 1, end)) % MOD;
}
no *pseg = nullptr;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    ans = (ans + (1LL * a[i] * ((i + get(pseg, 1, a[i] - 1)) % MOD) % MOD) *
                     (n - i + 1)) %
          MOD;
    upd(pseg, a[i], i);
  }
  delete pseg;
  pseg = nullptr;
  for (int i = n; i >= 1; --i) {
    ans = (ans + (1LL * a[i] * (get(pseg, 1, a[i] - 1) % MOD) % MOD) * i) % MOD;
    upd(pseg, a[i], n - i + 1);
  }
  printf("%lld\n", ans);
}
