#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
using namespace std;
inline int read(){
	int v=0,f=1;
	char c=getchar();
	while (c<'0' || c>'9'){
		if (c=='-') f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9'){
		v=v*10+c-'0';
		c=getchar();
	}
	return v*f;
}
const int Maxn=200005;
const LL mod=1e9+7;
int n,ord[Maxn],tmpA[Maxn];
pair<int,int> a[Maxn];
LL V[Maxn],V1[Maxn];
LL preV[Maxn],sufV[Maxn],P[Maxn];
int pre0[Maxn],nxt0[Maxn];
LL bit[Maxn],ans[Maxn];
LL bit2[Maxn],ans2[Maxn];
void Add2(int pos,LL val){
	while (pos<Maxn){
		bit2[pos]+=val;
		if (bit2[pos]>=mod) bit2[pos]-=mod;
		pos+=pos&-pos;
	}
}
LL query2(int pos){
	LL ret=0;
	while (pos){
		ret+=bit2[pos];pos-=pos&-pos;
	}
	return ret%mod; 
}
LL Q2(int l,int r){
	if (l>r) return 0;
	LL v=query2(r)-query2(l-1);
	if (v<0) v+=mod;
	return v;
}
void Add(int pos,LL val){
	while (pos<Maxn){
		bit[pos]+=val;
		if (bit[pos]>=mod) bit[pos]-=mod;
		pos+=pos&-pos;
	}
}
LL query(int pos){
	LL ret=0;
	while (pos){
		ret+=bit[pos];pos-=pos&-pos;
	}
	return ret%mod; 
}
LL Q(int l,int r){
	if (l>r) return 0;
	LL v=query(r)-query(l-1);
	if (v<0) v+=mod;
	return v;
}
LL qp(LL x,LL p){
	LL ret=1;
	while (p){
		if (p&1) ret=ret*x%mod;
		x=x*x%mod;
		p>>=1;
	}
	return ret;
}
LL inv(LL x){
	return qp(x,mod-2);
}
bool cmp1(pair<int,int> x,pair<int,int> y){
	if (x.first!=y.first) return x.first<y.first;
	return x.second<y.second;
}
bool cmp2(pair<int,int> x,pair<int,int> y){
	if (x.first!=y.first) return x.first<y.first;
	return x.second>y.second;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i].first);
		tmpA[i]=a[i].first; 
		a[i].second=i;
	}
	sort(a+1,a+1+n,cmp1);
	for (int i=1;i<=n;i++){
		ord[a[i].second]=i;
		if (a[i].first<i){
			printf("0\n");
			return 0;
		}
	}
	for (int i=1;i<=n;i++){
		V[i]=a[i].first-i+1;
		V1[i]=a[i].first-i;
	}
	preV[0]=1;
	for (int i=1;i<=n;i++){
		preV[i]=preV[i-1]*V[i]%mod;
	}
	sufV[n+1]=1;
	for (int i=n;i>=1;i--){
		sufV[i]=sufV[i+1]*V[i]%mod;
	}
	pre0[0]=0;
	for (int i=1;i<=n;i++){
		if (V1[i]==0){
			pre0[i]=i;
		}
		else{
			pre0[i]=pre0[i-1];
		}
	}
	nxt0[n+1]=n+1;
	for (int i=n;i>=1;i--){
		if (V1[i]==0){
			nxt0[i]=i;
		}
		else{
			nxt0[i]=nxt0[i+1];
		}
	}
	P[0]=1;
	for (int i=1;i<=n;i++){
		if (V1[i]==0){
			P[i]=1;
		}
		else{
			P[i]=P[i-1]*V1[i]%mod;
		}
	}
	for (int i=n;i>=1;i--){
		int pos=ord[i];
		int lo=pos+1,hi=nxt0[pos];
		LL tmp=Q(lo,hi);
		
		if (tmp){
			ans[pos]=tmp*inv(P[pos-1])%mod*preV[pos]%mod;
		}
		else ans[pos]=0;
		LL dlt=sufV[pos+1]*P[pos-1]%mod;
		Add(pos,dlt);
	}
	LL res=0;
	for (int i=1;i<=n;i++) res+=ans[i];
	res%=mod;
	res=res*inv(2)%mod;
	//cout<<res<<endl;
	//return 0; 
	memset(bit,0,sizeof(bit));
	for (int i=1;i<=n;i++){
		a[i].first=tmpA[i];a[i].second=n+1-i;
	}
	sort(a+1,a+1+n,cmp2);
	for (int i=1;i<=n;i++){
		ord[a[i].second]=i;
	}
	for (int i=1;i<=n;i++){
		V[i]=a[i].first-i+1;
		V1[i]=a[i].first-i;
	}
	preV[0]=1;
	for (int i=1;i<=n;i++){
		preV[i]=preV[i-1]*V[i]%mod;
	}
	sufV[n+1]=1;
	for (int i=n;i>=1;i--){
		sufV[i]=sufV[i+1]*V[i]%mod;
	}
	pre0[0]=0;
	for (int i=1;i<=n;i++){
		if (V1[i]==0){
			pre0[i]=i;
		}
		else{
			pre0[i]=pre0[i-1];
		}
	}
	nxt0[n+1]=n+1;
	for (int i=n;i>=1;i--){
		if (V1[i]==0){
			nxt0[i]=i;
		}
		else{
			nxt0[i]=nxt0[i+1];
		}
	}
	P[0]=1;
	for (int i=1;i<=n;i++){
		if (V1[i]==0){
			P[i]=1;
		}
		else{
			P[i]=P[i-1]*V1[i]%mod;
		}
	}
	for (int i=n;i>=1;i--){
		int pos=ord[i];
		int lo=pos+1,hi=nxt0[pos];
		LL tmp=Q(lo,hi);ans2[i]=Q2(lo,n);
		
		if (tmp){
			ans[pos]=tmp*inv(P[pos-1])%mod*preV[pos]%mod;
		}
		else ans[pos]=0;
		LL dlt=sufV[pos+1]*P[pos-1]%mod;
		Add(pos,dlt);Add2(pos,1);
	}
	LL res2=0;
	for (int i=1;i<=n;i++) res2+=ans[i];
	res2%=mod;
	res2=res2*inv(2)%mod;
	LL totper=preV[n];
	//cout<<totper<<endl;
	for (int i=1;i<=n;i++){
		//cout<<i<<' '<<ans2[i]<<endl;
		res2+=ans2[i]*totper-ans[i];
		res2=(res2%mod+mod)%mod;
	}
	res+=res2;
	res%=mod;
	printf("%lld\n",res);
	return 0;
}