#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\n';
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld 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 ll mod = 1000000007;
constexpr ll modsize = 0;
vector<ll> fac(modsize);
vector<ll> inv(modsize);
vector<ll> facinv(modsize);

void modcalc() {
	if (modsize == 0) abort();
	fac[0] = 1; fac[1] = 1; inv[1] = 1;
	facinv[0] = 1; facinv[1] = 1;
	for (ll 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;
	}
}

ll modinv(ll a) {
	if (a == 0) abort();
	ll b = mod, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= mod;
	if (u < 0) u += mod;
	return u;
}

ll modpow(ll a, ll b) {
	ll ans = 1;
	a %= mod;
	while (b) {
		if (b & 1) ans = ans * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}

ll modcomb(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}

ll modperm(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[n - k] % mod;
}

ll modhom(ll n, ll 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);

	ll n;
	cin >> n;
	vector<P> p(n);
	rep(i, n) {
		ll a;
		cin >> a;
		p[i] = { a,i };
	}
	sort(all(p)); reverse(all(p));
	lazysegtree<ll> seg(n), seg2(n);
	ll ans = 0, cnt = 1, coeff = 1;
	rep(i, n) {
		ll num = p[i].first; ll idx = p[i].second;
		ll pat = num - (n - i) + 1;
		if (pat <= 0) {
			cout << "0\n";
			return 0;
		}
		ll start = ans + cnt * seg2.get(0, idx); start %= mod;
		seg2.add(idx, 1);
		ll 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';
}

Measured memory_bytes: 23100