#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int inv2=(mod+1)/2;
int read()
{
	char c=getchar();
	int res=0;
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') res=(res<<1)+(res<<3)+(c^48),c=getchar();
	return res;
}
int n,a[200005],tree[200005],tree2[200005],num[200005],pos[200005];
long long sum=1,S[200005],D[200005],invD[200005];
long long ksm(long long x,long long n)
{
	long long ans=1;
	while(n)
	{
		if(n&1) ans=ans*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return ans;
}
void add(int x,int v)
{
	while(x<=n) tree[x]=(tree[x]+v)%mod,tree2[x]++,x+=(x&-x);
}
int ask(int x)
{
	int ans=0;
	while(x) ans=(ans+tree[x])%mod,x-=(x&-x);
	return ans;
}
int ask2(int x)
{
	int ans=0;
	while(x) ans+=tree2[x],x-=(x&-x);
	return ans;
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++) a[i]=read(),num[a[i]]++;
	for(int i=n;i>=1;i--) num[i]+=num[i+1];
	for(int i=1;i<=n;i++) num[i]-=(n-i);
	for(int i=1;i<=n;i++) sum=sum*num[i]%mod;
	if(!sum){puts("0");return 0;}
	S[0]=D[0]=1;
	for(int i=1;i<=n;i++)
	{
		long long x=1ll*(num[i]-1)*ksm(num[i],mod-2)%mod;
		if(!x) pos[i]=pos[i-1]+1,S[pos[i]]=i,D[i]=D[i-1];
		else pos[i]=pos[i-1],D[i]=D[i-1]*x%mod;
		invD[i]=ksm(D[i],mod-2);
	}
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		ans=(ans+1ll*(ask(a[i])-ask(S[pos[a[i]]]-1)+mod)*D[a[i]]%mod*sum%mod*inv2%mod)%mod;
		add(a[i],invD[a[i]]);
	}
	memset(tree,0,sizeof(tree));memset(tree2,0,sizeof(tree2));
	for(int i=n;i;i--)
	{
		ans-=1ll*(ask(a[i]-1)-ask(S[pos[a[i]]]-1)+mod)*D[a[i]]%mod*sum%mod*inv2%mod;ans=(ans%mod+mod)%mod;
		ans=(ans+1ll*ask2(a[i]-1)*sum%mod)%mod;
		add(a[i],invD[a[i]]);
	}
	ans=(ans%mod+mod)%mod;
	printf("%lld\n",ans);
}