#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define maxn 200000
#define mod 1000000007
#define ls i<<1
#define rs i<<1|1

using namespace std;
typedef long long ll;
ll inv[maxn+5];
int a[maxn+5];
struct data
{
    int a,id;
    data(int x=0,int y=0):a(x),id(y){}
    bool operator < (const data &x) const
    {
        return a<x.a;
    }
}d[maxn+5];
struct node
{
    int l,r;
    ll sum,mark;
}t[maxn*4+5];
void pushup(int i)
{
    t[i].sum=(t[ls].sum+t[rs].sum)%mod;
}
void pushdown(int i)
{
    if(t[i].mark!=1)
    {
        if(t[i].l!=t[i].r)
        {
            t[ls].mark=t[ls].mark*t[i].mark%mod;
            t[rs].mark=t[rs].mark*t[i].mark%mod;
            t[ls].sum=t[ls].sum*t[i].mark%mod;
            t[rs].sum=t[rs].sum*t[i].mark%mod;
        }
        t[i].mark=1;
    }
}
void build(int i,int l,int r)
{
    t[i].l=l;
    t[i].r=r;
    t[i].mark=1;
    if(l==r) t[i].sum=0;
    else
    {
        int mid=l+r>>1;
        build(ls,l,mid);
        build(rs,mid+1,r);
        pushup(i);
    }
}
void mult(int i,int l,int r,ll v)
{
    if(l<=t[i].l && t[i].r<=r)
    {
        t[i].sum=(t[i].sum*v)%mod;
        t[i].mark=(t[i].mark*v)%mod;
        return;
    }
    if(t[i].r<l || r<t[i].l) return;
    pushdown(i);
    mult(ls,l,r,v);
    mult(rs,l,r,v);
    pushup(i);
}
void change(int i,int l,int r,ll v)
{
    //printf("?%d %d\n",l,r);
    if(l<=t[i].l && t[i].r<=r)
    {
        t[i].sum=v%mod;
        t[i].mark=1;
        //printf("!!!!!!!!!!!!!!!!!! %d, %d: %lld\n",t[i].l,t[i].r,t[i].sum);
        return;
    }
    if(t[i].r<l || r<t[i].l) return;
    pushdown(i);
    change(ls,l,r,v);
    change(rs,l,r,v);
    pushup(i);
    //printf("$$$$%d %d: %lld, %lld, %lld\n",t[i].l,t[i].r,t[i].sum,t[ls].sum,t[rs].sum);
}
ll query(int i,int l,int r)
{
    if(l<=t[i].l && t[i].r<=r) return t[i].sum;
    if(t[i].r<l || r<t[i].l) return 0;
    pushdown(i);
    return (query(ls,l,r)+query(rs,l,r))%mod;
}
int bt[maxn+5];
inline int lowbit(int x) {return x&-x;}
inline void add(int x,int v)
{
    for(;x<=maxn;x+=lowbit(x)) bt[x]+=v;
}
inline int ask(int x)
{
    int ret=0;
    for(;x;x-=lowbit(x)) ret+=bt[x];
    return ret;
}
int main()
{
    inv[1]=1;
    rep(i,2,maxn) inv[i]=inv[mod%i]*(mod-mod/i)%mod;
    int n;
    scanf("%d",&n);
    rep(i,1,n) scanf("%d",&a[i]);
    rep(i,1,n) d[i]=data(a[i],i);
    sort(d+1,d+n+1);
    ll A=1;
    rep(i,1,n) A=A*max(0,d[i].a-i+1)%mod;
    if(A==0)
    {
        printf("0\n");
        return 0;
    }
    build(1,1,n);
    ll ans=0;
    per(i,1,n)
    {
        ll tmp=A*inv[d[i].a-i+1]%mod;
        //printf("!%d: %d,%d ???%lld\n",i,d[i].a,d[i].id,tmp);
        mult(1,1,n,inv[d[i].a-i+1]*(d[i].a-i)%mod);
        if(d[i].id<n) ans=(ans+query(1,d[i].id+1,n)*(d[i].a-i+1)%mod*inv[2])%mod;
        //if(d[i].id<n) printf("!!!!!%d: %lld\n",i,query(1,d[i].id+1,n));
        if(d[i].id>1) ans=(ans+A*ask(d[i].id-1)-query(1,1,d[i].id-1)*(d[i].a-i+1)%mod*inv[2]%mod+mod)%mod;
        //if(d[i].id>1) printf("!!!!!%d: %d, %lld\n",i,ask(d[i].id-1),query(1,1,d[i].id-1));

        change(1,d[i].id,d[i].id,tmp);
        //printf("?????????%lld\n",query(1,1,n));
        add(d[i].id,1);
    }
    printf("%lld\n",ans);
    return 0;
}
