#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i <  B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std;   

const int mo = 1e9 + 7;

ll ksm(ll x, ll y) {
	ll s = 1;
	for(; y; y /= 2, x = x
			* x % mo)
		if(y & 1) s = s * x % mo;
	return s;
}

const ll ni2 = ksm(2, mo - 2);

const int N = 2e5 + 5;

int n, a[N], d[N];
int cnt[N];
ll b[N], c[N];

int cmp(int x, int y) {
	if(a[x] == a[y]) return x < y;
	return a[x] < a[y];
}

ll w[N];

ll sum, ans;
void dg(int x, int y, int u, int v) {
	if(u >= v) return;
	if(x == y) {
		ans = (ans + sum * (v - u + 1) % mo * (v - u) % mo * ni2 % mo * ni2) % mo;
		return;
	}
	int mi = x + y >> 1, m = u - 1;
	while(m < v && a[d[m + 1]] <= mi) m ++;
	dg(x, mi, u, m); dg(mi + 1, y, m + 1, v);

	w[mi] = 1;
	fd(i, mi - 1, x) w[i] = w[i + 1] * c[i + 1] % mo;
	w[mi + 1] = c[mi + 1];
	fo(i, mi + 2, y) w[i] = w[i - 1] * c[i] % mo;

	int l = u; ll s = 0;
	fo(i, m + 1, v) {
		while(l <= m && d[l] <= d[i]) s = (s + w[a[d[l]]]) % mo, l ++;
		ans = (ans + sum * s % mo * w[a[d[i]]] % mo * ni2) % mo;
	}
	l = m, s = 0;
	fd(i, v, m + 1) {
		while(l >= u && d[l] > d[i]) s = (s + w[a[d[l]]]) % mo, l --;
		ans = (ans + sum * (m - l) - sum * s % mo * w[a[d[i]]] % mo * ni2) % mo;
	}
	sort(d + u, d + v + 1);
}

int main() {
	scanf("%d", &n);
	fo(i, 1, n) {
		scanf("%d", &a[i]);
		if(a[i] > n) a[i] = n;
		cnt[a[i]] ++;
	}
	fd(i, n, 1) cnt[i] += cnt[i + 1];
	sum = 1;
	fo(i, 1, n) {
		b[i] = cnt[i] - n + i;
		sum = sum * max(0ll, b[i]) % mo;
	}
	fo(i, 1, n) {
		c[i] = ksm(b[i], mo - 2) * (b[i] - 1) % mo;
	} 
	fo(i, 1, n) d[i] = i;
	sort(d + 1, d + n + 1, cmp);
	dg(1, n, 1, n);
	ans = (ans % mo + mo) % mo;
	pp("%lld\n", ans);
}
