#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pi;

const int mod = 1e9+7;
const int maxn = 200010;

int n, C = 1, ans = 0;
pi a[maxn];
int b[maxn], sum[maxn<<2], cnt[maxn<<2], K[maxn<<2];

int qpow(int x, int y) {
	int ret = 1;
	while (y) {
		if (y & 1) ret = 1LL * ret * x % mod;
		x = 1LL * x * x % mod;
		y >>= 1;
	}
	return ret;
}

void pushUp(int rt) {
	sum[rt] = (sum[rt<<1] + sum[rt<<1|1]) % mod;
	cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1];
}

void modify(int rt, int k) {
	sum[rt] = 1LL * sum[rt] * k % mod;
	K[rt] = 1LL * K[rt] * k % mod;
}

void pushDown(int rt) {
	if (K[rt] != 1) {
		modify(rt<<1, K[rt]);
		modify(rt<<1|1, K[rt]);
		K[rt] = 1;
	}
}

void update(int p, int v, int l, int r, int rt) {
	if (l == r) {
		sum[rt] = (sum[rt] + v) % mod;
		cnt[rt] ++;
		return;
	}
	pushDown(rt);
	int m = (l + r) >> 1;
	if (p <= m) update(p, v, l, m, rt<<1);
	else update(p, v, m+1, r, rt<<1|1);
	pushUp(rt);
}

int query(int L, int R, int l, int r, int rt) {
	if (L > R) return 0;
	if (L <= l && r <= R) return sum[rt];
	int ret = 0;
	pushDown(rt);
	int m = (l + r) >> 1;
	if (L <= m) ret = (ret + query(L, R, l, m, rt<<1)) % mod;
	if (R > m) ret = (ret + query(L, R, m+1, r, rt<<1|1)) % mod;
	return ret;
}

int query_cnt(int L, int R, int l, int r, int rt) {
	if (L > R) return 0;
	if (L <= l && r <= R) return cnt[rt];
	int ret = 0;
	int m = (l + r) >> 1;
	if (L <= m) ret = ret + query_cnt(L, R, l, m, rt<<1);
	if (R > m) ret = ret + query_cnt(L, R, m+1, r, rt<<1|1);
	return ret;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i].first);
		a[i].second = i;
	}
	sort(a+1, a+n+1);
	for (int i = 1; i <= n; i++) {
		if (a[i].first-i+1 <= 0) {
			puts("0");
			return 0;
		}
		b[i] = 1LL*(a[i].first-i)*qpow(a[i].first-i+1, mod-2)%mod;
	}
	for (int i = 1; i <= n; i++) C = 1LL * C * (a[i].first-i+1) % mod;
	for (int i = 1; i <= n; i++) {
		int v = 1LL*(mod+1)/2*C%mod*qpow(a[i].first-i+1, mod-2)%mod;
		ans = (ans + 1LL*v*query(1, a[i].second-1, 1, n, 1)%mod)%mod;
		ans = ((ans + 1LL*C*query_cnt(a[i].second+1, n, 1, n, 1)%mod)%mod+mod-1LL*v*query(a[i].second+1, n, 1, n, 1)%mod)%mod;
		K[1] = 1LL * K[1] * b[i] % mod;
		update(a[i].second, a[i].first-i, 1, n, 1);
	}
	printf("%d\n", ans);
	return 0;
}