#include <bits/stdc++.h>

typedef long long int int64;
static const int64 mod = 1000000007LL;

int64 exgcd(int64 a,int64 b,int64 &c,int64 &d)
{
	if(b == 0) {
		c = 1, d = 0;
		return a;
	}
	int64 x = exgcd(b,a % b,d,c);
	d -= c * (a / b);
	return a;
}
int64 finv(int64 x)
{
	int64 c,d;
	exgcd(x,mod,c,d);
	if(c < 0) c += mod;
	return c;
}

int a[200003];
int n;
int cnt[200003];
struct pair {
	int a,b;
	pair():a(0),b(0){}
	pair(int a,int b):a(a),b(b){}
	pair operator +(const pair &c) const {
		if(b > c.b || c.a == 0) return *this;
		if(c.b > b || a == 0) return c;
		return pair((a+c.a)%mod,b);
	}
	pair operator *(const pair &c) const {
		return pair((int64)a*c.a%mod,b+c.b);
	}
} arr[200003];
void add(int i,pair v)
{
	while(i <= n) {
		arr[i] = arr[i] + v;
		i += i&-i;
	}
}
pair sum(int i)
{
	pair ans = arr[i];i -= i&-i;
	while(i > 0) {
		ans = ans + arr[i];
		i -= i&-i;
	}
	return ans;
}
int arr2[200003];
void add2(int i,int v)
{
	while(i <= n) {
		arr2[i] = arr2[i] + v;
		i += i&-i;
	}
}
int sum2(int i)
{
	int ans = 0;
	while(i > 0) {
		ans += arr2[i];
		i -= i&-i;
	}
	return ans;
}
int64 inv[200003];
std::pair<pair,pair> calpair(int k)
{
	pair a = k == 0 ? pair(1,-1) : pair(k,0);
	pair b = k-1 == 0 ? pair(1,-1) : pair(k-1,0);
	return std::make_pair(pair(a.a*inv[b.a]%mod,a.b-b.b),pair(b.a*inv[a.a]%mod,b.b-a.b));
}
pair suf1[200003],suf2[200003]; // i/(i-1),(i-1)/i
pair pre1[200003],pre2[200003]; // i/(i-1),(i-1)/i
int main()
{
	scanf("%d",&n);
	inv[1] = 1;for(int i = 2;i <= n;++i) inv[i] = (mod-mod/i)*inv[mod%i]%mod;
	for(int i = 0;i < n;++i) {
		scanf("%d",&a[i]);--a[i];
		++cnt[a[i]];
	}
	for(int i = n-2;i >= 0;--i) {
		cnt[i] += cnt[i+1]-1;
	}
	int64 ians = 1;
	for(int i = 0;i < n;++i) {
		if(cnt[i] <= 0) {
			printf("0\n");
			return 0;
		}
		ians = ians * cnt[i] % mod;
	}
	for(int i = n-1;i >= 0;--i) {
		std::pair<pair,pair> p = calpair(cnt[i]);
		if(i == n-1) {
			suf1[i] = p.first;
			suf2[i] = p.second;
		} else {
			suf1[i] = p.first * suf1[i+1];
			suf2[i] = p.second * suf2[i+1];
		}
	}
	for(int i = 0;i < n;++i) {
		std::pair<pair,pair> p = calpair(cnt[i]);
		if(i == 0) {
			pre1[i] = p.first;
			pre2[i] = p.second;
		} else {
			pre1[i] = p.first * pre1[i-1];
			pre2[i] = p.second * pre2[i-1];
		}
	}
	/*
	for(int i = 0;i < n;++i) {
		printf("%d ", cnt[i]);
	}
	printf("\n");
	for(int i = 0;i < n;++i) {
		printf("%d;%d ", pre1[i].a, pre1[i].b);
	}
	printf("\n");
	for(int i = 0;i < n;++i) {
		printf("%d;%d ", pre2[i].a, pre2[i].b);
	}
	printf("\n");
	*/
	int64 ans = 0;
	// a[i] <= a[j] case
	for(int i = 0;i < n;++i) {
		pair val = sum(a[i]+1) * pre2[a[i]];
		assert(val.b <= 0);
		if(val.b == 0) ans += val.a;
		add(a[i]+1,pre1[a[i]]);
	}
	// a[i] > a[j] case
	memset(arr,0,sizeof(arr));
	int64 count = 0;
	for(int i = n-1;i >= 0;--i) {
		pair val = sum(a[i]) * pre2[a[i]];
		count += sum2(a[i]);
		assert(val.a == 0 || val.b <= 0);
		if(val.b == 0) ans -= val.a;
		add(a[i]+1,pre1[a[i]]);
		add2(a[i]+1,1);
	}
	ans = ans % mod * inv[2] % mod;
	ans = ians * (ans + count % mod) % mod;
	if(ans < 0) ans += mod;
	printf("%lld\n", ans);
}
