#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
const u32 P=1e9+7;
inline u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
inline u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
inline u32 sum(u32 a, u32 b) {return (a+=b)<P?a:a-P;}
inline u32 dif(u32 a, u32 b) {return (a-=b)&0x80000000?a+P:a;}
u64 power(u64 a, int b) {
	u64 ans=1;
	for(; b; a=a*a%P, b/=2) if(b&1) ans=ans*a%P;
	return ans;
}
const int N=2e5+1;
int n, a[N], b[N], cnt[N], rank[N];
u32 bit[N], p[N], invp[N], q[N], invq[N], x[N];
u32 query(int x) {
	u32 ans=0;
	for(; x; x&=x-1) inc(ans, bit[x]);
	return ans;
}
void add(int x, u32 v) {
	for(; x<=n; x+=x&-x) inc(bit[x], v);
}
void clear(int x) {
	for(; x<=n; x+=x&-x) bit[x]=0;
}
int main() {
	n=rd();
	for(int i=1; i<=n; ++i) ++cnt[a[i]=rd()];
	for(int i=1; i<=n; ++i) cnt[i]+=cnt[i-1];
	for(int i=n; i>=1; --i) rank[cnt[a[i]]]=i, b[cnt[a[i]]--]=a[i];
	memset(cnt+1, 0, n*sizeof(int));
	p[0]=q[0]=1;
	for(int i=1; i<=n; ++i) p[i]=p[i-1]*(b[i]-i+1llu)%P;
	if(p[n]==0) puts("0"), exit(0);
	for(int i=1; i<=n; ++i) x[i]=std::max(b[i]-i, 1), q[i]=(u64)q[i-1]*x[i]%P;
	invp[n]=power(p[n], P-2), invq[n]=power(q[n], P-2);
	for(int i=n; i>=1; --i) invp[i-1]=invp[i]*(b[i]-i+1llu)%P;
	for(int i=n; i>=1; --i) invq[i-1]=(u64)invq[i]*x[i]%P;
	u64 invcnt=0;
	for(int i=1; i<=n; ++i) {
		for(int j=a[i]+1; j<=n; j+=j&-j) invcnt+=bit[j];
		for(int j=a[i]; j; j&=j-1) ++bit[j];
	}
	u32 ans=invcnt*2%P;
	memset(bit+1, 0, n*sizeof(int));
	for(int i=1, j=0; i<=n; ++i) {
		if(b[i-1]==i-1) {
			while(j<i-1) clear(rank[++j]);
		} else {
			ans=(ans+(2llu*query(rank[i]-1)+P-query(n))*q[i-1]%P*invp[i])%P;
		}
		add(rank[i], (b[i]-i+1llu)*p[i-1]%P*invq[i-1]%P);
	}
	ans=(ans+(ans&1?P:0llu))/2*p[n]%P;
	printf("%u\n", ans);
	return 0;
}
