#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
#define fi first
#define queue QQQ
#define se second
#define debug(x) cout << #x" = " << x << endl;
#define pp(x,y) cout << "pp: " << x << " " << y << endl;
#define rank __RAnK
inline ll read(){
	register ll x=0,f=1;register char c=gc();
	for(;!isdigit(c);c=gc())if(c=='-')f=-1;
	for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
#define rd read
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
const int maxn = 2e5+233;
const int mod = 1e9+7;
int n;
int c1[maxn],c2[maxn],cnt[maxn],a[maxn],tot,D[maxn],invD[maxn];
int zero[maxn],S[maxn];
#define lowbit(x) ((x) & (-x))
inline int qpow(int a,int b){
	int ans = 1;
	for(;b;b>>=1,a=1ll*a*a%mod)
		if(b&1) ans=1ll*ans*a%mod;
	return ans;
}
void add(int c[],int x,int v){
	for(;x<=n;x+=lowbit(x))
		(c[x] += v) %= mod;
}
int query(int c[],int x){
	int ans = 0;
	for(;x;x-=lowbit(x))
		(ans += c[x]) %= mod;
	return ans;
}
int main(){
	n = rd();
	Rep(i,1,n) cnt[a[i]=rd()]++;
	Dep(i,n-1,1) cnt[i]+=cnt[i+1];
	tot = 1;
	Dep(i,n,1) cnt[i] -= n-i;
	Dep(i,n,1) tot = 1ll * tot * cnt[i] % mod; 
	if(!tot){puts("0");return 0;}
	S[0] = 1;D[0] = 1;
	Rep(i,1,n){
		if(cnt[i]==1) D[i] = D[i-1]; else  
					  D[i] = 1ll * (cnt[i]-1) * qpow(cnt[i],mod-2) % mod * D[i-1] % mod;
		if(cnt[i]==1){
			zero[i] = zero[i-1] + 1;
			S[zero[i]] = i;
		} else{
			zero[i] = zero[i-1];
		}
		invD[i] = qpow(D[i],mod-2);
	}
	mem(c1,0);mem(c2,0);
	ll ans = 0;
	Rep(i,1,n){
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * (query(c1,a[i]) - query(c1,a[i]-1))) % mod;//same
		ans = (ans + 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]) + mod) % mod) % mod;//small
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	}//相同的
//	writeln(ans);
	mem(c1,0);mem(c2,0);
	Dep(i,n,1){
		ans = (ans - 1ll * tot * qpow(2,mod-2) %mod * D[a[i]] %mod * (query(c2,a[i]-1) - query(c2,S[zero[a[i]]]) + mod)% mod + mod) % mod;
		ans = (ans + 1ll * query(c1,a[i]-1) * tot) % mod;
		add(c1,a[i],1);
		add(c2,a[i],invD[a[i]]);
	} 
	writeln(ans);
	return 0;
}
/*
考虑枚举出某个数

需要查询它右边的答案

1、右边和它相同的。 
	倒着做， 
2、右边比它小的。 

3、右边比它大的。 

sum * sum(D[i]) / (sum(D[])) 
*/