#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=200000+100;
const db pi=acos(-1.0);
#define lowbit(x) ((x)&(-x))
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 1000000007
#define inv2 500000004
#define eps 1e-8
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;
}

inline ll readll()
{
    ll x=0;int 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;
}

namespace My_Math{
    #define N 100000

    int fac[N+100],invfac[N+100];

    int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
    int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
    int mul(int x,int y) {return 1ll*x*y%maxd;}
    ll qpow(ll x,int y)
    {
        ll ans=1;
        while (y)
        {
            if (y&1) ans=mul(ans,x);
            x=mul(x,x);y>>=1;
        }
        return ans;
    }
    int getinv(int x) {return qpow(x,maxd-2);}

    int C(int n,int m)
    {
        if ((n<m) || (n<0) || (m<0)) return 0;
        return mul(mul(fac[n],invfac[m]),invfac[n-m]);
    }

    void math_init()
    {
        fac[0]=invfac[0]=1;
        rep(i,1,N) fac[i]=mul(fac[i-1],i);
        invfac[N]=getinv(fac[N]);
        per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
    }
    #undef N
}
using namespace My_Math;

int n,a[N],c[N],d[N],pos0[N],invd[N],cnt0[N];

struct Fenwick_Tree{

    int tr[N];

    void clr()
    {
        rep(i,1,n) tr[i]=0;
    }

    void modify(int p,int x)
    {
        for (int i=p;i<=n;i+=lowbit(i)) tr[i]=add(tr[i],x);
    }

    int query(int p)
    {
        int ans=0;
        for (int i=p;i;i-=lowbit(i)) ans=add(ans,tr[i]);
        return ans;
    }
}tr0,tr1;

int main()
{
    n=read();
    rep(i,1,n) 
    {
        a[i]=read();c[a[i]]++;
    }
    per(i,n,1) c[i]+=c[i+1];
    int sum=1;
    rep(i,1,n)
    {
        if (c[i]<n-i) {puts("0");return 0;}
        c[i]-=(n-i);
        sum=mul(sum,c[i]);
    }
    //cout << sum << endl;
    d[0]=1;pos0[0]=1;
    rep(i,1,n)
    {
        int x=mul(c[i]-1,getinv(c[i]));cnt0[i]=cnt0[i-1];
        if (!x)
        {
            cnt0[i]++;pos0[cnt0[i]]=i;
            d[i]=d[i-1];
        }
        else
        {
            d[i]=mul(d[i-1],x);
        }
        invd[i]=getinv(d[i]);
    }
    //rep(i,1,n) cout << d[i] << " ";cout << endl;
    int ans=0;
    rep(i,1,n)
    {
        int tmp=dec(tr1.query(a[i]),tr1.query(pos0[cnt0[a[i]]]-1));
        ans=add(ans,mul(mul(d[a[i]],tmp),mul(sum,inv2)));
        tr1.modify(a[i],invd[a[i]]);
    }
    tr1.clr();
    per(i,n,1)
    {
        int tmp=dec(tr1.query(a[i]-1),tr1.query(pos0[cnt0[a[i]]]-1));
        ans=dec(ans,mul(mul(d[a[i]],tmp),mul(sum,inv2)));
        tmp=tr0.query(a[i]-1);
        ans=add(ans,mul(tmp,sum));
        tr1.modify(a[i],invd[a[i]]);
        tr0.modify(a[i],1);
    }
    printf("%d\n",ans);
    return 0;
}
        

