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

const int N = 200005, P = 1000000007;
int n, pro[N], t1[N], t2[N];
vector<tuple<int*, int, int>> v;
pair<int, int> A[N];

int power(int a, int x) {
	int ans = 1;
	for (; x; x >>= 1, a = 1LL * a * a % P)
		if (x & 1) ans = 1LL * ans * a % P;
	return ans;
}

void upd(int *t, int x, int y, bool record = true) {
	if (record)
		v.emplace_back(t, x, y);
	for (; x <= n; x += x & -x)
		t[x] = (t[x] + y) % P;
}
int qry(int *t, int x) {
	int s = 0;
	for (; x; x &= x - 1)
		s = (s + t[x]) % P;
	return s;
}
void clear() {
	for (const auto &p : v)
		upd(get<0>(p), get<1>(p), -get<2>(p), false);
	v.clear();
}

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 + 1 + n);
	int all = pro[0] = 1;
	for (int i = 1; i <= n; ++i) {
		all = 1ll * all * (A[i].first - i + 1) % P;
		if (all <= 0) return puts("0"), 0;
		pro[i] = 1ll * pro[i - 1] * power(A[i].first - i + 1, P - 2) % P;
		if (A[i].first - i)
			pro[i] = 1ll * pro[i] * (A[i].first - i) % P;
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i)
		ans = (ans + 2 * (i - 1 - qry(t1, A[i].second))) % P, upd(t1, A[i].second, 1);
	clear();
	for (int i = 1; i <= n; ++i) {
		ans = (ans + (2ll * qry(t2, A[i].second) - qry(t2, n)) * power(A[i].first - i + 1, P - 2) % P * pro[i - 1]) % P;
		if (A[i].first == i)
			clear();
		else
			upd(t2, A[i].second, (A[i].first - i + 1ll) * power(pro[i - 1], P - 2) % P);
	}
	ans = (P + 1ll) / 2 * all % P * ans % P;
	printf("%d\n", (ans + P) % P);
	return 0;
}
