#include<cstdio>
#include<algorithm>
const int mod = 1000000007;
int n;
int a[200100];
struct st {
	int loc, val;
	bool operator<(const st&r)const {
		return val < r.val;
	}
};
st s[200100];
const int itsz = 1 << 18;
int ita[2 * itsz];
int itb[2 * itsz];
long long int mpow(long long int x, long long int y) {
	if (y == 0)return 1;
	long long int r = mpow(x, y / 2);
	r *= r;
	r %= mod;
	if (y % 2 == 1) {
		r *= x;
		r %= mod;
	}
	return r;
}
long long int minv(long long int x) {
	return mpow(x, mod - 2);
}
void itupd(int *it, int loc, int val) {
	loc += itsz;
	it[loc] = val;
	loc /= 2;
	while (loc > 0) {
		it[loc] = (it[loc * 2] + it[loc * 2 + 1]) % mod;
		loc /= 2;
	}
}
int itcalc(int *it, int start, int end) {
	int res = 0;
	start += itsz;
	end += itsz;
	while (start <= end) {
		if (start % 2 == 1) {
			res += it[start];
			res %= mod;
			start++;
		}
		if (end % 2 == 0) {
			res += it[end];
			res %= mod;
			end--;
		}
		start /= 2;
		end /= 2;
	}
	return res;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		s[i].loc = i;
		s[i].val = a[i];
	}
	std::sort(s, s + n);
	int rs = n - 1;
	int cnt = 0;
	long long int permn = 1;
	for (int i = n; i > 0; i--) {
		while (rs >= 0 && s[rs].val == i) {
			rs--;
			cnt++;
		}
		permn *= cnt;
		permn %= mod;
		cnt--;
	}
	if (permn == 0) {
		printf("0");
		return 0;
	}
	rs = n - 1;
	cnt = 0;
	long long int avgd = 0;
	long long int cadd = 1;
	int lz = n;
	for (int i = n; i > 0; i--) {
		while (rs >= 0 && s[rs].val == i) {
			avgd += itcalc(itb, 0, s[rs].loc - 1);
			avgd += mod - (itcalc(ita, 0, s[rs].loc - 1)*cadd%mod*minv(2)) % mod;
			avgd += itcalc(ita, s[rs].loc + 1, n)*cadd%mod*minv(2) % mod;
			avgd %= mod;
			itupd(ita, s[rs].loc, minv(cadd));
			itupd(itb, s[rs].loc, 1);
			rs--;
			cnt++;
		}
		cadd *= minv(cnt);
		cadd %= mod;
		cadd *= (cnt - 1);
		cadd %= mod;
		if (cnt == 1) {
			cadd = 1;
			for (int j = rs + 1; j < lz; j++) {
				itupd(ita, s[j].loc, 0);
			}
			lz = rs + 1;
		}
		cnt--;
	}
	long long int ans = permn*avgd%mod;
	printf("%lld", ans);
	return 0;
}
