#include<bits/stdc++.h>
#define LL long long
using namespace std;
static char gc(){
	static char buf[1<<6],*p1=buf,*p2=buf;
	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<5,stdin),p1==p2)?EOF:*p1++;
}
template <class T>
inline void read(T&data){
	data=0;
	register char ch=0;
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch<='9'&&ch>='0'){
		data=(data<<3)+(data<<1)+(ch&15);
		ch=getchar();
	}
	return;
}
const int _ = 200101,mod = 1e9+ 7;
int  aa[_],n,Data[_],sum1[_],sum2[_],loc[_],LOC[_],qz[_],hz[_],sum[_];
inline int poww(register int ax,register int cc){
	register int ret=1,Base=ax;
	for(;cc;cc>>=1,Base=1LL*Base*Base%mod)if(cc&1)ret=1LL*ret*Base%mod;
	return ret;
}
inline int Que1(register int l,register int r){	return 1LL*sum1[r]*poww(sum1[l-1],mod-2)%mod;}
inline int Que2(register int l,register int r){	return 1LL*sum2[r]*poww(sum2[l-1],mod-2)%mod;}
inline int lowbit(register int k){return k&(-k);}
inline int query1(register int l,register int r){
	if(r<l)return 0;
	register int ret1=0,ret2=0;
	for(register int i=l-1;i;i-=lowbit(i))ret1=(ret1+qz[i])%mod;
	for(register int i=r;i;i-=lowbit(i))ret2=(ret2+qz[i])%mod;
	return (ret2-ret1+mod)%mod;
}
inline int query2(register int l,register int r){
	if(r<l)return 0;
	register int ret1=0,ret2=0;
	for(register int i=l-1;i;i-=lowbit(i))ret1=(ret1+hz[i])%mod;
	for(register int i=r;i;i-=lowbit(i))ret2=(ret2+hz[i])%mod;
	return (ret2-ret1+mod)%mod;
}
inline int query3(register int l,register int r){
	if(r<l)return 0;
	register int ret1=0,ret2=0;
	for(register int i=l-1;i;i-=lowbit(i))ret1=(ret1+sum[i])%mod;
	for(register int i=r;i;i-=lowbit(i))ret2=(ret2+sum[i])%mod;
	return (ret2-ret1+mod)%mod;
}
inline void fix(register int loc,register int zh1,register int zh2){
	for(register int i=loc;i<=n;i+=lowbit(i))qz[i]=(qz[i]+zh1)%mod,hz[i]=(hz[i]+zh2)%mod,sum[i]++;
	return;
}
int main(){
	//freopen("belief.in","r",stdin);
	//freopen("belief.out","w",stdout);
	read(n);
	for(register int i=1;i<=n;++i)read(aa[i]),aa[i]=aa[i]>n?n:aa[i],++Data[aa[i]];
	for(register int i=n-1;i>=1;--i)Data[i]=Data[i]+Data[i+1];
	for(register int i=1;i<=n;++i)Data[i]=Data[i]-n+i;
	for(register int i=1;i<=n;++i)if(Data[i]<0){cout<<0<<endl;return 0;}
	sum1[0]=sum2[0]=1;
	for(register int i=1;i<=n;++i)sum1[i]=1LL*sum1[i-1]*Data[i] %mod;
	if(!sum1[n]){puts("0");return 0;}
	for(register int i=1;i<=n;++i){
		--Data[i];
		if(Data[i]==0)loc[i]=i,sum2[i]=sum2[i-1];
		else loc[i]=loc[i-1],sum2[i]=1LL*sum2[i-1]*Data[i]%mod;
	}
	LOC[n+1]=n+1;
	for(register int i=n;i>=1;--i)
		if(!Data[i]) LOC[i]=i;
		else LOC[i]=LOC[i+1];
	register int ans=0,ni2=poww(2,mod-2);
	for(register int i=1;i<=n;++i){
		register int f=1LL*query1(loc[aa[i]]+1,aa[i])*poww(Que2(aa[i]+1,n),mod-2)%mod*Que1(aa[i]+1,n)%mod;
		ans=(ans+1LL*f*ni2%mod)%mod;
		f=1LL*query2(aa[i]+1,LOC[aa[i]+1]-1)*poww(Que2(1,aa[i]),mod-2)%mod*Que1(1,aa[i])%mod;
		f=1LL*f*ni2%mod;
		ans=(ans-f+mod)%mod;
		f=query3(aa[i]+1,n);
		ans=(ans+1LL*f*sum1[n]%mod)%mod;
		fix(aa[i],1LL*Que1(1,aa[i])*Que2(aa[i]+1,n)%mod,1LL*Que2(1,aa[i])*Que1(aa[i]+1,n)%mod);
	}
	cout<<ans;
}
