#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
constexpr int inf = 1000000010;
constexpr long long INF = 1000000000000000010;
constexpr long double eps = 1e-12;
constexpr long double pi = 3.141592653589793238;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr long long mod = 1000000007;
constexpr long long modsize = 0;
vector<long long> fac(modsize);
vector<long long> inv(modsize);
vector<long long> facinv(modsize);
void modcalc() {
  if (modsize == 0) abort();
  fac[0] = 1;
  fac[1] = 1;
  inv[1] = 1;
  facinv[0] = 1;
  facinv[1] = 1;
  for (long long i = 2; i < modsize; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    facinv[i] = facinv[i - 1] * inv[i] % mod;
  }
}
long long modinv(long long a) {
  if (a == 0) abort();
  long long b = mod, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0) u += mod;
  return u;
}
long long modpow(long long a, long long b) {
  long long ans = 1;
  a %= mod;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long modcomb(long long n, long long k) {
  if (n < 0 || k < 0 || n < k) return 0;
  return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}
long long modperm(long long n, long long k) {
  if (n < 0 || k < 0 || n < k) return 0;
  return fac[n] * facinv[n - k] % mod;
}
long long modhom(long long n, long long k) {
  if (n < 0 || k < 0 || n == 0 && k > 0) return 0;
  if (n == 0 && k == 0) return 1;
  return fac[n + k - 1] * facinv[k] % mod * facinv[n - 1] % mod;
}
template <class T>
class lazysegtree {
  int n;
  vector<T> data, lazy;
  vector<bool> lazyflag;
  T id = 0;
  T operation(T a, T b) { return (a + b) % mod; };

 public:
  lazysegtree(int _n) {
    n = 1;
    while (n < _n + 2) {
      n <<= 1;
    }
    data = lazy = vector<T>(2 * n - 1, id);
    lazyflag = vector<bool>(2 * n - 1, false);
  }
  void eval(int k, int l, int r) {
    if (lazyflag[k]) {
      data[k] = lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] = lazy[k];
        lazy[2 * k + 2] = lazy[k];
        lazyflag[2 * k + 1] = lazyflag[2 * k + 2] = true;
      }
      lazyflag[k] = false;
    }
  }
  void lazychange(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyflag[k] = true;
      eval(k, l, r);
    } else {
      lazychange(a, b, x, 2 * k + 1, l, (l + r) / 2);
      lazychange(a, b, x, 2 * k + 2, (l + r) / 2, r);
      data[k] = operation(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  void change(int i, T x) { lazychange(i, i + 1, x); }
  void add(int i, T x) { change(i, (data[i + n - 1] + x) % mod); }
  T get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return id;
    if (a <= l && r <= b) return data[k];
    int vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
    return operation(vl, vr);
  }
  T operator[](int i) { return get(i, i + 1); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > p(n);
  for (long long i = 0; i < (n); i++) {
    int a;
    cin >> a;
    p[i] = {a, i};
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  lazysegtree<long long> seg(n), seg2(n);
  long long ans = 0, cnt = 1, coeff = 1;
  for (long long i = 0; i < (n); i++) {
    int num = p[i].first;
    int idx = p[i].second;
    int pat = num - (n - i) + 1;
    if (pat <= 0) {
      cout << "0\n";
      return 0;
    }
    long long start = ans + cnt * seg2.get(0, idx);
    start %= mod;
    seg2.add(idx, 1);
    long long d = cnt - 2 * seg.get(0, idx) * coeff;
    d %= mod;
    if (d < 0) d += mod;
    coeff *= pat - 1;
    coeff %= mod;
    cnt *= pat;
    cnt %= mod;
    if (coeff)
      seg.add(idx, cnt * modinv(coeff) % mod * modinv(pat) % mod);
    else {
      seg.lazychange(0, n, 0);
      seg.add(idx, cnt);
      coeff = 1;
    }
    ans = start * pat + pat * (pat - 1) / 2 % mod * d % mod;
    ans %= mod;
  }
  cout << ans << '\n';
}
