#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;++x)
#define Red(x,y,z) for(int x=y;x>=z;--x)
using namespace std;
const int MAXN=2e5+5,Mod=1e9+7,Inv2=Mod+1>>1;
char buf[1<<12],*p1=buf,*p2=buf,nc;int ny;
inline char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
//inline char gc(){return getchar();}
inline int read(){
	int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
	x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}
inline void Add(int&x,int y){x+=y,x>=Mod?x-=Mod:0;}
int n,a[MAXN],val1[MAXN],val2[MAXN],cnt[MAXN],D[MAXN],InvD[MAXN],zero[MAXN],S[MAXN],tot,ans;
inline int lowbit(int x){return x&-x;}
inline void add(int x,int v){for(;x<=n;x+=lowbit(x))Add(val1[x],v),Add(val2[x],1);}
inline int ask1(int x){int ans=0;for(;x;x-=lowbit(x))Add(ans,val1[x]);return ans;}
inline int ask2(int x){int ans=0;for(;x;x-=lowbit(x))Add(ans,val2[x]);return ans;}
inline int Fp(int x,int k){int ans=1;for(;k;k>>=1,x=1ll*x*x%Mod)if(k&1)ans=1ll*ans*x%Mod;return ans;}
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	n=read();Rep(i,1,n)cnt[a[i]=read()]++;
	Red(i,n,1)cnt[i]+=cnt[i+1];S[0]=D[0]=1;
	tot=1;Rep(i,1,n)tot=1ll*tot*(cnt[i]-=n-i)%Mod;
	if(!tot){puts("0");return 0;}
	Rep(i,1,n){
		int x=1ll*(cnt[i]-1)*Fp(cnt[i],Mod-2)%Mod;
		if(!x)S[zero[i]=zero[i-1]+1]=i,D[i]=D[i-1];
		else zero[i]=zero[i-1],D[i]=1ll*D[i-1]*x%Mod;
		InvD[i]=Fp(D[i],Mod-2);
	}
	Rep(i,1,n)ans=(ans+1ll*(ask1(a[i])-ask1(S[zero[a[i]]]-1)+Mod)*D[a[i]]%Mod*tot%Mod*Inv2)%Mod,add(a[i],InvD[a[i]]);
	memset(val1,0,sizeof(val1)),memset(val2,0,sizeof(val2));
	Red(i,n,1)ans=(ans-1ll*(ask1(a[i]-1)-ask1(S[zero[a[i]]]-1)+Mod)*D[a[i]]%Mod*tot%Mod*Inv2%Mod+Mod)%Mod,ans=(ans+1ll*ask2(a[i]-1)*tot)%Mod,add(a[i],InvD[a[i]]);
	cout<<ans<<'\n';
	return 0;
}


