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

typedef long long LL;

const int MAXN=200005,MOD=1000000007;

int n,a[MAXN],b[MAXN],cnt[MAXN],inv[MAXN],C[MAXN],pre[MAXN][2];

void add(int x,int d)
{
	for (;x<=n;x+=x&-x)
		(C[x]+=d)%=MOD;
}

int sum(int x)
{
	int ret=0;
	for (;x;x-=x&-x)
		(ret+=C[x])%=MOD;
	return ret;
}

int work(int s)
{
	for (int i=1;i<=n;i++)
		C[i]=0;
	int ret=0;
	for (int i=1;i<=n;i++)
	{
		add(a[i]+1,pre[a[i]][1]);
		int k=(sum(a[i])-sum(b[a[i]])+MOD)%MOD;
		ret=(ret+(LL)k*pre[a[i]][0])%MOD;
	}
	return (LL)ret*s%MOD*inv[2]%MOD;
}

int main()
{
	#ifdef KeyID
		freopen("read.txt","r",stdin);
	#endif
	scanf("%d",&n);
	inv[1]=1;
	for (int i=2;i<=n;i++)
		inv[i]=(LL)(MOD-MOD/i)*inv[MOD%i]%MOD;
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		cnt[a[i]]++;
	}
	int all=1;
	for (int i=1,sum=0;i<=n;i++)
	{
		if (i<=sum)
		{
			printf("0");
			return 0;
		}
		if (i-sum==1)
		{
			pre[i][0]=pre[i][1]=1;
			b[i]=i;
		}
		else
		{
			pre[i][0]=(LL)pre[i-1][0]*inv[i-sum]%MOD*(i-sum-1)%MOD;
			pre[i][1]=(LL)pre[i-1][1]*(i-sum)%MOD*inv[i-sum-1]%MOD;
			b[i]=b[i-1];
		}
		all=(LL)all*(i-sum)%MOD;
		(sum+=cnt[i])%=MOD;
	}
	int ans=0;
	for (int i=1;i<=n;i++)
		ans=(ans+(LL)cnt[i]*(cnt[i]-1)/2)%MOD;
	ans=(LL)ans*inv[2]%MOD;
	for (int i=n;i>=1;i--)
	{
		add(a[i],1);
		(ans+=sum(a[i]-1))%=MOD;
	}
	ans=(LL)ans*all%MOD;
	(ans+=work(all))%=MOD;
	reverse(a+1,a+n+1);
	(ans+=MOD-work(all))%=MOD;
	printf("%d",ans);
	return 0;
}