//一个奇怪的做法...QwQ
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define qwq(x) cout<<"# "<<#x<<" = "<<x<<endl;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
 
template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

namespace{
	const int mod=1e9+7;
	int power(int x,int p){
		int res=1;
		for(;p;p>>=1,x=(ll)x*x%mod)
			if(p&1)res=(ll)res*x%mod;
		return res;
	}
	int mul(int x,int y){
		return static_cast<ll>(x)*y%mod;
	}
	int fix(int x){
		return x>=mod?x-mod:x;
	}
	void add(int &x,int y){
		x=(x+y>mod?x+y-mod:x+y);
	}
}

const int N=200005;
int a[N],b[N],tmp[N],cur[N],n,ans;

namespace Part1{
	int f[N][5];
	int g(int x,int y){
		int res=b[x]-x+1-y;
		return res<=0?0:res;
	}
	int C2(int x){
		return (ll)x*(x-1)/2%mod;
	}
	void rmain(){
		f[0][0]=1;
		rep(i,1,n){
			REP(t,5)
				add(f[i][t],mul(f[i-1][t],g(i,2-abs(t-2))));
			add(f[i][1],mul(mul(f[i-1][0],g(i,1)),b[i]-b[i-1]));
			add(f[i][2],mul(mul(f[i-1][1],g(i,2)),b[i]-b[i-1]));
			add(f[i][2],mul(mul(f[i-1][0],g(i,2)),C2(b[i]-b[i-1])));
			add(f[i][3],f[i-1][2]);
			add(f[i][3],mul(f[i-1][1],b[i]-b[i-1]));
			add(f[i][3],mul(f[i-1][0],C2(b[i]-b[i-1])));
			add(f[i][4],f[i-1][3]);
		}
		add(ans,f[n][4]);
	}
}

namespace Part2{
	int S[N<<2],C[N<<2],s[N],pre[N],suf[N],Suf[N],las[N];
	#define lc (o<<1)
	#define rc (o<<1|1)
	#define mid ((l+r)>>1)
	void app(int o,int l,int r,int x){
		add(S[o],mul(x,s[r]-s[l-1]+mod));
		add(C[o],x);
	}
	void down(int o,int l,int r){
		if(C[o]){
			app(lc,l,mid,C[o]);
			app(rc,mid+1,r,C[o]);
			C[o]=0;
		}
	}
	void upd(int o,int l,int r,int x,int y,int z){
		if(l==x&&y==r){
			app(o,l,r,z);
			return;
		}
		down(o,l,r);
		if(x<=mid)upd(lc,l,mid,x,min(y,mid),z);
		if(mid<y)upd(rc,mid+1,r,max(mid+1,x),y,z);
		S[o]=fix(S[lc]+S[rc]);
	}
	int qry(int o,int l,int r,int x,int y){
		if(l==x&&y==r)return S[o];
		down(o,l,r);
		if(y<=mid)return qry(lc,l,mid,x,y);
		if(mid<x)return qry(rc,mid+1,r,x,y);
		return fix(qry(lc,l,mid,x,mid)+qry(rc,mid+1,r,mid+1,y));
	}
	void rmain(){
		pre[0]=1;
		rep(i,1,n)
			pre[i]=mul(pre[i-1],b[i]-i+1);
		Suf[n+1]=suf[n+1]=1;
		per(i,n,1){
			suf[i]=mul(suf[i+1],max(1,b[i]-i));
			Suf[i]=mul(Suf[i+1],b[i]-i+1);
		}
		rep(i,1,n){
			int p=lower_bound(b+1,b+n+1,i)-b;
			s[i]=mul(pre[p-1],suf[p]);
			add(s[i],s[i-1]);
//			printf("%d:  %d %d %d\n",i,s[i],pre[i],suf[i]);
		}
		rep(i,1,n)
			if(b[i]==i)las[i]=i;
			else las[i]=las[i-1];
		rep(i,1,n){
			if(a[i]<n){
				add(ans,qry(1,1,n,a[i]+1,n));
//				printf("# %d %d\n",i,qry(1,1,n,a[i]+1,n));
			}
			int coef=mul(power(suf[cur[i]],mod-2),Suf[cur[i]+1]);
			if(las[cur[i]]==cur[i]&&b[cur[i]-1]!=a[i]){
				upd(1,1,n,a[i],a[i],coef);
			}
			else{
//				printf("@ %d %d %d\n",i,b[las[cur[i]-1]],coef);
				upd(1,1,n,b[las[cur[i]-1]]+1,a[i],coef);
			}
		}
	}
}
/*
5
4 5 4 1 2 

4 5 3 1 2

3 5 4 1 2
*/
int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		tmp[i]=i;
	}
	sort(tmp+1,tmp+n+1,[&](int x,int y){
		return a[x]<a[y];
	});
	rep(i,1,n)cur[tmp[i]]=i;
	rep(i,1,n)b[cur[i]]=a[i];
	rep(i,1,n)
		if(b[i]<i)return puts("0"),0;
	Part1::rmain();
	Part2::rmain();
	printf("%d\n",ans);
	return 0;
}