#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+3;
const int mod=1e9+7;
int mul(int x,int y){
	return (1ll*x*y)%mod;
}
void add(int &x,int y){
	x+=y;
	if(x>=mod){
		x-=mod;
	}
}
void sub(int &x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
}
int sub1(int x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
	return x;
}
int binpow(int x,int y){
	int tich=1;
	while(y){
		if(y&1){
			tich=mul(tich,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return tich;
}
int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
void upd2(int idx,int val){
	while(idx<N){
		add(bit2[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum2(int idx){
	int sum=0;
	while(idx){
		add(sum,bit2[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get2(int l,int r){
	return sub1(getsum2(r),getsum2(l-1));
}
void upd1(int idx,int val){
	while(idx<N){
		add(bit1[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum1(int idx){
	int sum=0;
	while(idx){
		add(sum,bit1[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get1(int l,int r){
	return sub1(getsum1(r),getsum1(l-1));
}
void upd(int idx,int val){
	while(idx<N){
		add(bit[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum(int idx){
	int sum=0;
	while(idx){
		add(sum,bit[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get(int l,int r){
	return sub1(getsum(r),getsum(l-1));
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,tich=1,ans=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>ar[i];
		cnt[ar[i]]++;
	}
	for(i=n;i>0;i--){
		cnt[i]+=cnt[i+1];
	}
	for(i=1;i<=n;i++){
		cnt[i]-=n-i;
		tich=mul(tich,cnt[i]);
		if(cnt[i]<=0){
			cout<<0;
			return 0;
		}
		j=mul(cnt[i]-1,binpow(cnt[i],mod-2));
		if(pref[i-1]){
			pref[i]=mul(pref[i-1],j);
		}
		else{
			pref[i]=j;
		}
		lst[i]=lst[i-1];
		if(!pref[i]){
			lst[i]=i;
		}
	}
	nxt[n]=n+1;
	for(i=n-1;i>0;i--){
		nxt[i]=nxt[i+1];
		if(!pref[i+1]){
			nxt[i]=i+1;
		}
	}
	for(i=1;i<=n;i++){
		add(ans,mul(mul(mul(tich,get(lst[ar[i]],ar[i])),pref[ar[i]]),500000004));	//a(j)<=a(i), (a(j),a(i)]
		add(ans,mul(tich,get1(ar[i]+1,n)));			//a(j)>a(i) (a(i),a(j)]
		if(pref[ar[i]]){
			sub(ans,mul(mul(mul(tich,binpow(pref[ar[i]],mod-2)),get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		else{
			sub(ans,mul(mul(tich,get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		upd1(ar[i],1);
		if(pref[ar[i]]){
			upd(ar[i],binpow(pref[ar[i]],mod-2));
		}
		else{
			upd(ar[i],1);
		}
		upd2(ar[i],pref[ar[i]]);
	}
	cout<<ans;
}#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+3;
const int mod=1e9+7;
int mul(int x,int y){
	return (1ll*x*y)%mod;
}
void add(int &x,int y){
	x+=y;
	if(x>=mod){
		x-=mod;
	}
}
void sub(int &x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
}
int sub1(int x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
	return x;
}
int binpow(int x,int y){
	int tich=1;
	while(y){
		if(y&1){
			tich=mul(tich,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return tich;
}
int ar[N],cnt[N],pref[N],bit[N],bit1[N],lst[N],nxt[N],bit2[N];
void upd2(int idx,int val){
	while(idx<N){
		add(bit2[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum2(int idx){
	int sum=0;
	while(idx){
		add(sum,bit2[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get2(int l,int r){
	return sub1(getsum2(r),getsum2(l-1));
}
void upd1(int idx,int val){
	while(idx<N){
		add(bit1[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum1(int idx){
	int sum=0;
	while(idx){
		add(sum,bit1[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get1(int l,int r){
	return sub1(getsum1(r),getsum1(l-1));
}
void upd(int idx,int val){
	while(idx<N){
		add(bit[idx],val);
		idx+=(idx&-idx);
	}	
}
int getsum(int idx){
	int sum=0;
	while(idx){
		add(sum,bit[idx]);
		idx-=(idx&-idx);
	}
	return sum;
}
int get(int l,int r){
	return sub1(getsum(r),getsum(l-1));
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,tich=1,ans=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>ar[i];
		cnt[ar[i]]++;
	}
	for(i=n;i>0;i--){
		cnt[i]+=cnt[i+1];
	}
	for(i=1;i<=n;i++){
		cnt[i]-=n-i;
		tich=mul(tich,cnt[i]);
		if(cnt[i]<=0){
			cout<<0;
			return 0;
		}
		j=mul(cnt[i]-1,binpow(cnt[i],mod-2));
		if(pref[i-1]){
			pref[i]=mul(pref[i-1],j);
		}
		else{
			pref[i]=j;
		}
		lst[i]=lst[i-1];
		if(!pref[i]){
			lst[i]=i;
		}
	}
	nxt[n]=n+1;
	for(i=n-1;i>0;i--){
		nxt[i]=nxt[i+1];
		if(!pref[i+1]){
			nxt[i]=i+1;
		}
	}
	for(i=1;i<=n;i++){
		add(ans,mul(mul(mul(tich,get(lst[ar[i]],ar[i])),pref[ar[i]]),500000004));	//a(j)<=a(i), (a(j),a(i)]
		add(ans,mul(tich,get1(ar[i]+1,n)));			//a(j)>a(i) (a(i),a(j)]
		if(pref[ar[i]]){
			sub(ans,mul(mul(mul(tich,binpow(pref[ar[i]],mod-2)),get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		else{
			sub(ans,mul(mul(tich,get2(ar[i]+1,nxt[i]-1)),500000004));
		}
		upd1(ar[i],1);
		if(pref[ar[i]]){
			upd(ar[i],binpow(pref[ar[i]],mod-2));
		}
		else{
			upd(ar[i],1);
		}
		upd2(ar[i],pref[ar[i]]);
	}
	cout<<ans;
}