#include<bits/stdc++.h>
const int N=200005,M=1000000007;
using namespace std;
int n,b[N],bi[N],
m0=1,m1=1,am[N],ans,ss,tp,al;
struct dat{int x,v;}a[N];
inline int ksm(long long x,int t){
	int y=1;
	for(;t;t>>=1)
	t&1?y=y*x%M:0,x=x*x%M;
	return y;
}
inline void qry(int x){
	for(;x<=n;x+=x&-x)
	ans=(ans-2ll*m1*b[x])%M,(ss+=bi[x])%=M;
}
inline void ins(int x,int v){
	int y=am[x]*v;x=a[x].x;
	(al+=y)%=M;
	for(;x;x-=x&-x)(b[x]+=y)%=M,v>0?bi[x]++:0;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i].v),a[i].x=i+1;
	sort(a,a+n,[](dat x,dat y){
		return x.v<y.v;
	});
	for(int i=0,j=0;i<n;i++){
		if((tp=a[i].v-i)<1)return puts("0"),0;
		ans=(1ll*ans*tp+1ll*m1*al)%M;
		qry(a[i].x);
		m0=1ll*m0*tp%M;
		am[i]=m0*1ll*ksm(m1,M-2)%M;
		if(tp>1)m1=m1*(tp-1ll)%M;
		ins(i,1);
		if(tp==1)
		for(;j<=i;j++)ins(j,-1);
	}
	printf("%d",((ans*1ll*ksm(2,M-2)+1ll*ss*m0)%M+M)%M);
}