#include <bits/stdc++.h>
#define rep(i,n) for ((i)=1;(i)<=(n);(i)++)
using namespace std;
const int mod=1e9+7;
int n,i,a[200005],d[200005],z[200005],c[200005],inv[200005],lst[200005],ans;
int s1[200005],s2[200005];
int pw(int x,int y){
	int z=1;
	while(y){
		if(y&1) z=1ll*z*x%mod;
		x=1ll*x*x%mod;y/=2;
	}
	return z;
}
int lowbit(int x){
	return (x&-x);
}
void add(int x,int y){
	while(x<=n){
		s1[x]=(s1[x]+y)%mod;
		s2[x]++;
		x+=lowbit(x);
	}
}
int query1(int x){
	int s=0;
	while(x){
		s=(s+s1[x])%mod;
		x-=lowbit(x);
	}
	return s;
}
int query2(int x){
	int s=0;
	while(x){
		s=s+s2[x];
		x-=lowbit(x);
	}
	return s;
}
int main(){
	inv[1]=1;
	for(i=2;i<=200000;i++){
		inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
	}
	cin>>n;
	rep(i,n){
		cin>>a[i];
		c[a[i]]++;
	}
	for(i=n-1;i;i--)c[i]+=c[i+1];
	int all=1;
	rep(i,n){
		c[i]-=n-i;
		all=1ll*all*c[i]%mod;
	}
	if(all==0){
		puts("0");
		return 0;
	}
	d[0]=1;z[0]=0;
	rep(i,n){
		int x=1ll*(c[i]-1)*inv[c[i]]%mod;
		if(x){
			z[i]=z[i-1];
			d[i]=1ll*d[i-1]*x%mod;
		}
		else{
			d[i]=d[i-1];
			z[i]=z[i-1]+1;
			lst[z[i]]=i;
		}
	}
	rep(i,n){
		ans=(ans+1ll*(query1(a[i])-query1(lst[z[a[i]]]-1)+mod)%mod*d[a[i]]%mod*all%mod*inv[2])%mod;
		add(a[i],pw(d[a[i]],mod-2));
	}
	rep(i,n) s1[i]=s2[i]=0;
	for(i=n;i>=1;i--){
		ans=(ans-1ll*(query1(a[i]-1)-query1(lst[z[a[i]]]-1)+mod)%mod*d[a[i]]%mod*all%mod*inv[2])%mod;
		if(ans<0)ans+=mod;
		ans=(ans+1ll*query2(a[i]-1)*all)%mod;
		add(a[i],pw(d[a[i]],mod-2));
	}
	cout<<ans<<endl;
	return 0;
}