#include <bits/stdc++.h>

#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

#ifdef __WIN32
#define LLFORMAT "I64"
#define Rand() ((rand() << 15) + rand())
#else
#define LLFORMAT "ll"
#define Rand() (rand())
#endif

using namespace std;

const int maxn = 2e5 + 10, mod = 1e9 + 7;

int pow_mod(int x, int n = mod - 2) {
	int y = 1;
	while(n) {
		if(n & 1) {
			y = (long long) y * x % mod;
		}
		x = (long long) x * x % mod;
		n >>= 1;
	}
	return y;
}

int n, a[maxn], cnt[maxn], d[maxn], way, ans, fen[maxn];
vector<int> p0;

int lowbit(int i) {
	return i & (-i);
}

void M(int i, int x) {
	for(; i <= n; i += lowbit(i)) {
		fen[i] = (fen[i] + x) % mod;
	}
	return;
}

int Q(int i) {
	int x = 0;
	for(; i; i ^= lowbit(i)) {
		x = (fen[i] + x) % mod;
	}
	return x;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", a + i);
		++cnt[a[i]];
	}
	for (int i = n; i; --i) {
		cnt[i] += cnt[i + 1];
	}
	way = 1;
	d[0] = 1;
	for (int i = 1; i <= n; ++i) {
		cnt[i] -= (n - i);
		way = (long long) way * cnt[i] % mod;
		int x = (long long) (cnt[i] - 1) * pow_mod(cnt[i]) % mod;
		if(!x) {
			p0.push_back(i);
			x = 1;
		}
		d[i] = (long long) x * d[i - 1] % mod;
	}
	if(!way) {
		puts("0");
		return 0;
	}
	p0.push_back(n + 1);
	for (int i = n; i; --i) {
		int &ai = a[i], aj = p0[lower_bound(p0.begin(), p0.end(), ai + 1) - p0.begin()] - 1;
		ans = ((long long) (Q(aj) - Q(ai - 1)) * pow_mod(d[ai]) + ans) % mod;
		M(ai, d[ai]);
	}
	memset(fen, 0, sizeof(fen[0]) * (n + 1));
	for (int i = 1; i <= n; ++i) {
		int &ai = a[i], aj = p0[lower_bound(p0.begin(), p0.end(), ai + 1) - p0.begin()] - 1;
		ans = ((long long) ans - (long long) (Q(aj) - Q(ai)) * pow_mod(d[ai])) % mod;
		M(ai, d[ai]);
	}
	ans = (ans + mod) % mod;
	if(ans & 1) {
		ans = ans + mod >> 1;
	}
	else {
		ans >>= 1;
	}
	memset(fen, 0, sizeof(fen[0]) * (n + 1));
	for (int i = n; i; --i) {
		ans = (ans + Q(a[i] - 1)) % mod;
		M(a[i], 1);
	}
	printf("%d\n", (long long) ans * way % mod);
	return 0;
}
