
#include<cstdio>
#include<algorithm>
#define N_ 201000
using namespace std;
struct point {
	int num, x;
	bool operator < (const point &p)const {
		return x < p.x;
	}
}w[N_];
int n;
long long BIT[N_], P[N_], R[N_], S[N_], Mod = 1000000007, res, L[N_];
int C[N_];

void Put(int a, int ck) {
	long long t = R[a + 1] * P[a - 1] % Mod;
	int t2 = (ck + 1) / 2;
	t *= ck;
	a = w[a].num;
	while (a <= n) {
		BIT[a] += t;
		C[a] += t2;
		a += (a&-a);
	}
}
long long Sum(int a) {
	long long r = 0;
	while (a) {
		r += BIT[a];
		a -= (a&-a);
	}
	return r % Mod;
}
int Count(int a) {
	int r = 0;
	while (a) {
		r += C[a];
		a -= (a&-a);
	}
	return r;
}
long long Pow(long long a, long long b) {
	long long r = 1;
	while (b) {
		if (b & 1)r = r * a%Mod;
		a = a * a%Mod; b >>= 1;
	}
	return r;
}
int main() {
	int i, j, k;
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		scanf("%d", &w[i].x);
		w[i].num = i;
	}
	sort(w + 1, w + n + 1);
	long long tot = 1;
	L[0] = P[0] = 1;
	for (i = 1; i <= n; i++) {
		S[i] = w[i].x - i + 1;
		if (S[i] <= 0) {
			puts("0");
			return 0;
		}
		tot = 1ll * tot*S[i] % Mod;
		P[i] = P[i - 1];
		L[i] = tot;
		if (S[i] > 1)P[i] = P[i] * (S[i] - 1) % Mod;
	}
	R[n + 1] = 1;
	for (i = n; i >= 1; i--)R[i] = R[i + 1] * S[i] % Mod;
	int pv = n;
	for (i = n; i >= 1; i--) {

		long long t = (1ll * S[i] * (S[i] - 1) / 2) % Mod;
		int c = Count(n) - Count(w[i].num);
		long long s1 = Sum(n);
		long long s2 = Sum(w[i].num);
		s1 = (s1 - s2 + Mod) % Mod;


		long long inv = Pow(P[i], Mod - 2) * L[i - 1] % Mod * t % Mod;
		res = (res + inv * s1 + tot * (n - i - c) - inv * s2%Mod + Mod) % Mod;

		if (S[i] == 1) {
			while (pv > i) {
				Put(pv, -1);
				pv--;
			}
		}
		Put(i, 1);
	}
	printf("%lld\n", res);
}