#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define MOD 1000000007
LL powmod(LL a, LL n){
    if(n == 0) return 1;
    if(n % 2) return (a*powmod(a,n-1)) % MOD;
    LL c = powmod(a, n/2);
    return (c*c) % MOD;
}
LL inv(LL a){
    return powmod(a, MOD-2);
}
LL fact[210000];
LL invfact[210000];

LL ncr(LL n, LL r){
    if(r < 0 || n < 0) return 0;
    if(n < r) return 0;
    LL a = fact[n];
    a = (a * invfact[r]) % MOD;
    a = (a * invfact[n-r]) % MOD;
    return a;
}
void init(){
    fact[0] = 1;
    invfact[0] = 1;
    LL inv2 = inv(2);
    for(LL i = 1; i < 210000; i++){
        fact[i] = (i*fact[i-1]) % MOD;
        invfact[i] = inv(fact[i]);
    }
}

struct FTplus {
	vector<ll> s;
	FTplus(int n) : s(n) {}
	void update(int pos, ll dif) { // a[pos] += dif
		for (; pos < sz(s); pos |= pos + 1) s[pos] = (s[pos] + dif) % MOD;
	}
	ll query(int pos) { // sum of values in [0, pos)
		ll res = 0;
		for (; pos > 0; pos &= pos - 1) res = (res + s[pos-1]) % MOD;
		return res;
	}
};

int main(){
	init();
	LL n;
	cin >> n;
	vector<LL> s(n);
	for(int i = 0; i < n; i++){
		cin >> s[i];
	}
	vector<LL> d = s;
	vector<LL> e = d;
	sort(d.begin(), d.end());
	vector<LL> ss = d;
	map<LL,LL> dfind;
	for(int i = 0; i < n; i++){
		dfind[ss[i]] = i;
	}

	LL ok = 1;
	for(int i = 0; i < n; i++){
		d[i] -= i;
		if(d[i] <= 0) ok = 0;
		e[i] = d[i] - 1;
		if(e[i] == 0) e[i] = 123456789;
	}

	if(ok == 0){
		cout << 0 << endl;
		return 0;
	}

	LL dtotal = 1;
	LL etotal = 1;
	for(LL i = 0; i < n; i++){
		dtotal *= d[i];
		dtotal %= MOD;
		etotal *= e[i];
		etotal %= MOD;
	}


	LL finalans = 0;

	vector<LL> psum1(n);
	vector<LL> psum2(n);
	vector<LL> psum3(n);

	for(LL i = 0; i < n; i++){
		if(i == 0){
			psum1[i] = 1;
		} else {
			psum1[i] = ( ((d[i-1] * inv(e[i-1])) % MOD) * psum1[i-1] ) % MOD;
		}
		psum2[i] = (d[i] * psum1[i]) % MOD;
	}
	for(LL i = n-1; i >= 0; i--){
		if(i == n-1){
			psum3[i] = ((d[i] * inv(e[i])) % MOD);
		} else {
			psum3[i] = ( ((d[i] * inv(e[i])) % MOD) * psum3[i+1] ) % MOD;
		}
	}

	vector<LL> lastnonzero(n);
	for(LL i = 0; i < n; i++){
		if(i == 0 || d[i-1] == 1){
			lastnonzero[i] = i;
		} else {
			lastnonzero[i] = lastnonzero[i-1];
		}
	}


	if(1){
		FTplus t2(210000);


		for(LL i = 0; i < n; i++){
			LL b = s[i];
			LL l = dfind[b];
			LL k = lastnonzero[l];
			LL sum2 = t2.query(l+1) - t2.query(k);

			LL mult = (etotal * ((psum3[l] * inv(d[l])) % MOD) ) % MOD;

			finalans += (sum2) * mult;
			//cout << i << " " << l << " " << ((sum2) * mult) % MOD << endl;
			finalans %= MOD;

			t2.update(l, psum2[l]);
		}
	}
	if(1){
		reverse(s.begin(), s.end());
		FTplus smaller(210000);
		FTplus t2(210000);

		for(LL i = 0; i < n; i++){
			LL b = s[i];
			LL l = dfind[b];
			LL k = lastnonzero[l];
			LL sum2 = t2.query(l) - t2.query(k);

			LL mult = (etotal * ((psum3[l] * inv(d[l])) % MOD) ) % MOD;

			finalans += ((dtotal * smaller.query(l)) % MOD) * 2 - (sum2) * mult;
			//cout << i << " " << l << " " << " " << k << " " << (dtotal * smaller.query(l)- (sum2) * mult) % MOD << endl;
			finalans %= MOD;

			t2.update(l, psum2[l]);
			smaller.update(l, 1);
		}

	}

	finalans *= inv(2);
	finalans %= MOD;
	if(finalans < 0) finalans += MOD;
	cout << finalans << endl;

}

Measured memory_bytes: 31764