//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn = 200111;
struct SGT
{
	struct node
	{
		ll lz;
		ll sum;
		void addlz(ll L)
		{
			lz = lz*L%mod;
			sum = sum*L%mod;
		}
	}a[maxn*4];
	#define ls p<<1
	#define rs p<<1|1
	void pushdown(int p)
	{
		if (a[p].lz!=1)
		{
			a[ls].addlz(a[p].lz);
			a[rs].addlz(a[p].lz);
			a[p].lz = 1;
		}
	}
	void update(int p)
	{
		a[p].sum = (a[ls].sum+a[rs].sum)%mod;
	}
	void modify(int x, ll v, int l, int r, int p)
	{
		if (r<x||x<l) return;
		if (l==r)
		{
			a[p].sum = v;
			return;
		}
		pushdown(p);
		int m = l+r>>1;
		modify(x, v, l, m, ls);
		modify(x, v, m+1, r, rs);
		update(p);
	}
	ll query(int x, int y, int l, int r, int p)
	{
		if (r<x||y<l) return 0;
		if (x<=l&&r<=y) return a[p].sum;
		pushdown(p);
		int m = l+r>>1;
		return (query(x, y, l, m, ls)+query(x, y, m+1, r, rs))%mod;
	}
}yxg;
int n;
pair<int,int> a[maxn];
ll sprod[maxn], prod[maxn];
int tab[maxn];
void inc(int x)
{
	for (int i=x; i<maxn; i+=i&(-i)) tab[i]++;
}
int query(int x)
{
	int ret = 0;
	for (int i=x; i; i-=i&(-i)) ret += tab[i];
	return ret;
}
int main()
{
	geti(n);
	for (int i=1; i<=n; i++) geti(a[i].FF), a[i].SS = i;
	sort(a+1, a+n+1);
	sprod[n+1] = 1;
	for (int i=n; i>=1; i--) sprod[i] = sprod[i+1]*(a[i].FF-i+1)%mod;
	prod[0] = 1;
	for (int i=1; i<=n; i++) prod[i] = prod[i-1]*(a[i].FF-i+1)%mod;
	ll tot = 1;
	for (int i=1; i<=n; i++) tot = tot*(a[i].FF-i+1)%mod;
	ll dans = 0;
	for (int i=1; i<=n; i++)
	{
		ll tmp = query(a[i].SS);
		dans += (-tmp*tot+(i-1-tmp)*tot)%mod;
		inc(a[i].SS);
		
		dans -= -1ll*yxg.query(1, a[i].SS-1, 1, n, 1)*sprod[i+1]%mod;
		dans += -1ll*yxg.query(a[i].SS+1, n, 1, n, 1)*sprod[i+1]%mod;
		yxg.modify(a[i].SS, prod[i], 1, n, 1);
		yxg.a[1].addlz(a[i].FF-i);
	}
	cout<<((tot*((1ll*n*(n-1)/2)%mod)%mod+dans%mod)*qpow(2, mod-2)%mod+mod)%mod<<endl;
	return 0;
}


Measured memory_bytes: 14592