#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
class range {
 private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I& lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

 public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I& begin() { return i; }
  I& end() { return n; }
};
const int N = 101000;
int n, vis[N];
pair<int, int> p[N];
long long ret, prec[N], sufc[N], c[N];
struct node {
  long long fgp, fgv;
  long long sp, sv;
} nd[4 * N];
void upd(int p) {
  nd[p].sp = (nd[p + p].sp + nd[p + p + 1].sp) % mod;
  nd[p].sv = (nd[p + p].sv + nd[p + p + 1].sv) % mod;
}
void setf(int p, long long fgp, long long fgv) {
  nd[p].fgv = (nd[p].fgv + fgv * nd[p].fgp) % mod;
  nd[p].fgp = nd[p].fgp * fgp % mod;
  nd[p].sv = (nd[p].sv + fgv * nd[p].sp) % mod;
  nd[p].sp = nd[p].sp * fgp % mod;
}
void build(int p, int l, int r) {
  nd[p].fgp = 1;
  if (l == r) {
  } else {
    int md = (l + r) >> 1;
    build(p + p, l, md);
    build(p + p + 1, md + 1, r);
    upd(p);
  }
}
void push(int p) {
  if (nd[p].fgp != 1 || nd[p].fgv != 0) {
    setf(p + p, nd[p].fgp, nd[p].fgv);
    setf(p + p + 1, nd[p].fgp, nd[p].fgv);
    nd[p].fgp = 1;
    nd[p].fgv = 0;
  }
}
long long query(int p, int l, int r, int tl, int tr) {
  if (tl == l && tr == r)
    return nd[p].sv;
  else {
    push(p);
    int md = (l + r) >> 1;
    if (tr <= md)
      return query(p + p, l, md, tl, tr);
    else if (tl > md)
      return query(p + p + 1, md + 1, r, tl, tr);
    else
      return (query(p + p, l, md, tl, md) +
              query(p + p + 1, md + 1, r, md + 1, tr)) %
             mod;
  }
}
void modify(int p, int l, int r, int x, long long v) {
  if (l == r) {
    nd[p].sp = v;
  } else {
    push(p);
    int md = (l + r) >> 1;
    if (x <= md)
      modify(p + p, l, md, x, v);
    else if (x > md)
      modify(p + p + 1, md + 1, r, x, v);
    upd(p);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n + 1; i++) scanf("%d", &p[i].first), p[i].second = i;
  sort(p + 1, p + n + 1);
  for (int i = 1; i < n + 1; i++) c[i] = p[i].first - i + 1;
  prec[0] = 1;
  sufc[n] = 1;
  for (int i = 1; i < n + 1; i++) prec[i] = prec[i - 1] * c[i] % mod;
  for (int i = n + 1 - 1; i >= 1; i--) sufc[i - 1] = sufc[i] * c[i] % mod;
  long long totval = 0;
  build(1, 1, n);
  for (int i = n + 1 - 1; i >= 1; i--) {
    totval = (totval * (c[i + 1] - 1) + sufc[i + 1]) % mod;
    ret = (ret + totval * prec[i - 1] % mod * (c[i] * (c[i] - 1) / 2 % mod)) %
          mod;
    long long key = prec[i] * (p[i + 1].first - p[i].first) % mod;
    setf(1, c[i + 1] - 1, 0);
    if (i < n) modify(1, 1, n, p[i + 1].second, sufc[i + 1]);
    setf(1, 1, key);
    if (p[i].second > 1) ret = (ret + query(1, 1, n, 1, p[i].second - 1)) % mod;
  }
  printf("%lld\n", ret);
}
