/*
Author: QAQAutomaton
LANG: C++
PROG: e.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
	__INIT__(){
		memset(&inf,0x3f,sizeof(inf));
	}
}__INIT___;
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	inline bool read (signed &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}

	inline bool read (long long &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}
	inline bool read (char &x) {
		x=gc();
		return x!=EOF;
	}
	inline bool read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
		while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
		*x=0;
		return 1;
	}
	template<typename A,typename ...B>
	inline bool read(A &x,B &...y){
		return read(x)&&read(y...);
	}
	// print a signed integer
	inline bool write (signed x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}

	inline bool write (long long x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}
	inline bool write (char x) {
		putc(x);
		return 0;
	}
	inline bool write(const char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	inline bool write(char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	template<typename A,typename ...B>
	inline bool write(A x,B ...y){
		return write(x)||write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
pii a[200005];
int w[200005];
int dp[200005][3];
const int p=1000000007;
int inv[200005];
int iw[200005];
int cw[200005],cntw[200005];
struct smt{
	int ls,rs;
	int hv;
	int sum,at,mt;
	smt *l,*r;
	smt(int la,int ra){
		hv=0;
		at=sum=0;mt=1;
		ls=la;rs=ra;
		if(ls==rs){l=r=0;return;}
		int mid=(ls+rs)>>1;
		l=new smt(ls,mid);
		r=new smt(mid+1,rs);
	}
	void put_mul(int x){
		at=at*x%p;
		mt=mt*x%p;
		sum=sum*x%p;
	}
	void put_add(int x){
		at=(at+x)%p;
		sum=(sum+x*hv)%p;
	}
	void push_down(){
		l->put_mul(mt);
		l->put_add(at);
		r->put_mul(mt);
		r->put_add(at);
		mt=1;at=0;
	}
	int Sum(int la,int ra){
		if(la<=ls && rs<=ra)return sum;
		push_down();
		return 
			((la<=l->rs?l->Sum(la,ra):0)+
			(ra>=r->ls?r->Sum(la,ra):0))%p;
	}
	void upd(int x){
		++hv;
		if(ls==rs){sum=0;return;}
		push_down();
		if(x<=l->rs)l->upd(x);
		else r->upd(x);
	}
};
smt *rt;
signed main(){
#ifdef QAQAutoMaton 
	freopen("e.in","r",stdin);
	freopen("e.out","w",stdout);
#endif
	int n;
	read(n);
	for(int i=1;i<=n;++i){
		read(a[i].x);
		a[i].y=i;
	}
	inv[1]=1;
	for(int i=2;i<=n;++i)inv[i]=(p-p/i)*inv[p%i]%p;
	sort(a+1,a+n+1);
	int s=0;	
	for(int i=1;i<=n;++i)if(s>=a[i].x)return write("0\n");
	else {
		w[i]=a[i].x-s;
		++s;
	}
	dp[0][0]=1;
	for(int i=1;i<=n;++i){
		dp[i][0]=dp[i-1][0]*w[i]%p;
		dp[i][1]=dp[i-1][1]*(w[i]-1)%p;
		dp[i][2]=dp[i-1][2]*w[i]%p;
		dp[i][1]=(dp[i][1]+dp[i-1][0]*((w[i]-1)*w[i]/2%p))%p;
		dp[i][2]=(dp[i][2]+dp[i-1][1])%p;
	}
	int ans=0;
	for(int i=1;i<=n;++i)iw[i]=inv[w[i]]*(w[i]-1);
	for(int i=1;i<=n;++i)cw[i]=1;
/*	for(int i=1;i<=n;++i){
		for(int j=1;j<=a[i].x;++j){
			int cnt=0,s=1;
			for(int k=1;k<i;++k)if(a[k].x<j && a[k].y>a[i].y)++cnt;	
			for(int k=1;k<=n;++k){
				if(k==i)continue;
				if(a[k].x<j||k>i)s=s*w[k]%p;
				else s=(s*(w[k]-2))%p;
			}
//			write(i,' ',j,' ',cnt,' ',s,'\n');
			ans=(ans+cnt*s)%p;
		}
	}*/

/*	for(int i=1;i<=n;++i){
		int x=1;
		for(int j=a[i].x;j;--j){
			x=x*cw[j]%p;
			cntw[j]=(cntw[j+1]+x)%p;
		}
		for(int j=1;j<i;++j)if(a[j].y>a[i].y){
			ans=(ans+cntw[a[j].x+1]*inv[w[i]])%p;
		}
		cw[a[i].x]=cw[a[i].x]*(w[i]-1)%p*inv[w[i]]%p;	
	}*/

	rt=new smt(1,n);
	for(int i=1,l=1;i<=n;++i){
		int r=l;
		rt->put_add(1);
		while(r<=n && a[r].x==i){
			if(a[r].y<n){
				ans=(ans+rt->Sum(a[r].y+1,n)*inv[w[r]])%p;
			}
			rt->put_mul((w[r]-1)*inv[w[r]]%p);
			++r;
		}
		while(l<r){
			rt->upd(a[l].y);
			++l;
		}
	}

	for(int i=1;i<=n;++i)ans=ans*w[i]%p;
	write((ans+dp[n][2])%p,'\n');
	return 0;
}



Measured memory_bytes: 48896