#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int add(int x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
}
int mul(int x, int y) { return x = 1ll * x * y % MOD; }
class FenwickTree {
 private:
  vector<int> t;

 public:
  FenwickTree(int n) { t.assign(n + 2, 0); }
  void update(int x, int value) {
    for (; x < t.size(); x += x & -x) t[x] = add(t[x], value);
  }
  int get(int x) {
    int res = 0;
    for (; x > 0; x -= x & -x) res = add(res, t[x]);
    return res;
  }
};
int n;
vector<int> a;
vector<int> sum[2];
int main() {
  scanf("%d", &n);
  a.resize(n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int step = 0; step < 2; ++step) {
    FenwickTree bit(n + 1);
    sum[step].assign(n, 0);
    vector<int> c(a.begin(), a.end());
    sort(c.begin(), c.end());
    for (int i = 0; i < n; ++i) {
      int p = lower_bound(c.begin(), c.end(), a[i]) - c.begin() + 1;
      sum[step][i] = bit.get(p);
      bit.update(p, i + 1);
    }
    reverse(a.begin(), a.end());
  }
  reverse(sum[1].begin(), sum[1].end());
  int answer = 0;
  for (int i = 0; i < n; ++i) {
    answer = add(answer,
                 mul(a[i], add(mul(i + 1, n - i), add(mul(n - i, sum[0][i]),
                                                      mul(i + 1, sum[1][i])))));
  }
  printf("%d\n", answer);
  return 0;
}
