#include<iostream>
#include<cstdio>
#define MN 200000
#define N 262144
#define mod 1000000007
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,a[MN+5],num[MN+5],b[MN+5],T1[N*2+5],T2[N*2+5],T3[N*2+5],T4[N*2+5],ans,la[MN+5],ne[MN+5];
int suf0[MN+5],pre1[MN+5],inv[MN+5],pinv1[MN+5],pre0[MN+5],sinv1[MN+5],suf1[MN+5],S[MN+5];
int Query(int*T,int l,int r)
{
    int res=0;if(l>r) return 0;
    for(l+=N-1,r+=N+1;l^r^1;l>>=1,r>>=1)
    {
        if(~l&1) res=(res+T[l+1])%mod;
        if( r&1) res=(res+T[r-1])%mod;
    }
    return res;
}
void Renew(int*T,int x,int v){for((T[x+=N]+=v)%=mod;x>>=1;)T[x]=(T[x<<1]+T[x<<1|1])%mod;}
/*
struct Tree{int l,r,val,x,s;}T[MN*4+5];
void Build(int x,int l,int r)
{
	if((T[x].l=l)==(T[x].r=r)){T[x].s=(S[l]-S[l-1]+mod)%mod;return;}
	int mid=l+r>>1;
	Build(x<<1,l,mid);Build(x<<1|1,mid+1,r);
	T[x].s=(T[x<<1].s+T[x<<1|1].s)%mod;
}
inline void Mark(int x,int v){T[x].x=(T[x].x+1LL*v*T[x].s)%mod;T[x].val=(T[x].val+v)%mod;}
inline void pushdown(int x){Mark(x<<1,T[x].val),Mark(x<<1|1,T[x].val),T[x].val=0;}
void Modify(int x,int l,int r,int v)
{
	if(T[x].l==l&&T[x].r==r) return Mark(x,v);
	if(T[x].val) pushdown(x);
	int mid=T[x].l+T[x].r>>1;
	if(r<=mid) Modify(x<<1,l,r,v);
	else if(l>mid) Modify(x<<1|1,l,r,v);
	else Modify(x<<1,l,mid,v),Modify(x<<1|1,mid+1,r,v);
	T[x].x=(T[x<<1].x+T[x<<1|1].x)%mod;
}
int Query(int x,int l,int r)
{
	if(l>r) return 0;
	if(T[x].l==l&&T[x].r==r) return T[x].x;
	if(T[x].val) pushdown(x);
	int mid=T[x].l+T[x].r>>1;
	if(r<=mid) return Query(x<<1,l,r);
	else if(l>mid) return Query(x<<1|1,l,r);
	else return (Query(x<<1,l,mid)+Query(x<<1|1,mid+1,r))%mod;
}*/
int main()
{
    n=read();suf0[n+1]=pre1[0]=pinv1[0]=inv[0]=inv[1]=pre0[0]=sinv1[n+1]=suf1[n+1]=1;ne[n+1]=n+1;
    for(int i=2;i<=n;++i) inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
    for(int i=1;i<=n;++i) a[i]=read(),++num[a[i]];
    for(int i=n,j=0;i;b[i--]=j--)
    {
        j+=num[i];
        if(j<1) return 0*puts("0");
    }
    for(int i=1;i<=n;++i)
        pre1[i]=1LL*pre1[i-1]*max(1,b[i]-1)%mod,
        pinv1[i]=1LL*pinv1[i-1]*inv[b[i]-1]%mod,
        pre0[i]=1LL*pre0[i-1]*b[i]%mod,
		la[i]=b[i]==1?i:la[i-1];
    for(int i=n;i;--i)
        suf0[i]=1LL*suf0[i+1]*b[i]%mod,
        suf1[i]=1LL*suf1[i+1]*max(1,b[i]-1)%mod,
        sinv1[i]=1LL*sinv1[i+1]*inv[b[i]-1]%mod,
		ne[i]=b[i]==1?i:ne[i+1];
    for(int i=1;i<=n;++i) S[i]=(S[i-1]+1LL*suf1[i+1]*pre0[i-1])%mod;
   // Build(1,1,n);
    for(int i=n;i;--i)
    {
        int x=1LL*Query(T1,a[i],ne[a[i]+1]-1)*pinv1[a[i]]%mod*pre0[a[i]]%mod*(mod+1)/2%mod;
        ans=(ans+x)%mod;
      //  cout<<i<<" x= "<<x<<endl;
        if(a[i]>1)
        {
            int y=1LL*Query(T2,la[a[i]],a[i]-1)*sinv1[a[i]+1]%mod*suf0[a[i]+1]%mod*(mod+1)/2%mod;
            ans=(ans+y)%mod;
          //  cout<<i<<" y= "<<y<<endl;
            int z=(1LL*(S[a[i]]-S[la[a[i]]-1]+mod)%mod*Query(T3,1,la[a[i]]-1)
				  +1LL*Query(T3,la[a[i]],a[i]-1)*S[a[i]]+Query(T4,la[a[i]],a[i]-1))%mod;
            ans=(ans+1LL*sinv1[a[i]+1]*z%mod*suf0[a[i]+1])%mod;
          //  cout<<i<<" z= "<<1LL*sinv1[a[i]+1]*z%mod*suf0[a[i]+1]%mod<<endl;
        }
        Renew(T1,a[i],1LL*pre1[a[i]]*suf0[a[i]+1]%mod);
        Renew(T2,a[i],1LL*pre0[a[i]]*suf1[a[i]+1]%mod);
        Renew(T3,a[i],1);
        Renew(T4,a[i],mod-S[a[i]]);
    }
    cout<<ans;
    return 0;
}