#include<bits/stdc++.h>
#define fir first
#define sec second
#define db double
#define ll long long
#define ev eg[i].v
#define pb push_back
#define mod 1000000007
#define pir pair<int,int>
#define Rep(i,l,r) for(int i=(l);i<=(r);++i)
#define RepD(i,r,l) for(int i=(r);i>=(l);--i)
#define RepG(i,x) for(int i=hd[x];i;i=eg[i].nxt)
using namespace std;

const int N=2e5+5;

ll n,inv2=(mod+1)/2;
ll a[N],c[N],s[N],lst[N],t[N];

ll ksm(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod,b>>=1;
	}
	return res;
}

ll add(ll x,ll y){for(;x<=n;x+=x&(-x)) t[x]+=y;}
ll qry(ll x){ll res=0;for(;x;x-=x&(-x)) res=(res+t[x])%mod;return res;}

int main(){
	ll sum=1,ans=0,res=0;
	scanf("%lld",&n);
	Rep(i,1,n) scanf("%lld",&a[i]),c[a[i]]++;
	RepD(i,n,1) c[i]+=c[i+1];
	Rep(i,1,n) c[i]-=(n-i),sum=sum*c[i]%mod;
	if(sum<=0){puts("0");return 0;}
	Rep(i,1,n){
		if(c[i]>1){
			lst[i]=lst[i-1];
			s[i]=(c[i]-1)*ksm(c[i],mod-2)%mod;
			if(s[i-1]>0) s[i]=s[i]*s[i-1]%mod;
		}
		else lst[i]=i;		
	}
	Rep(i,1,n){
		ans=(ans+(qry(a[i])-qry(lst[a[i]])+mod)%mod*s[a[i]]%mod*sum%mod)%mod;
		add(a[i],ksm(s[a[i]],mod-2));
	}
	ans=ans*inv2%mod;
	memset(t,0,sizeof(t));
	Rep(i,1,n){
		ans=(ans+(i-1-qry(a[i]))*sum%mod)%mod;
		add(a[i],1);	
	}
	memset(t,0,sizeof(t));
	RepD(i,n,1)
		if(s[a[i]]){
			res=(res+(qry(a[i]-1)-qry(lst[a[i]])+mod)%mod*s[a[i]]%mod*sum%mod)%mod;
			add(a[i],ksm(s[a[i]],mod-2));
		}
	res=res*inv2%mod;
	printf("%lld\n",(ans-res+mod)%mod);
	return 0;
}
