#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
const int P = 1e9+7;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

inline int pow_mod(int x,int k){
   int res = 1,base = x;
   while(k){
      if(k&1)res = 1LL*res*base%P;
      k >>= 1; base = 1LL*base*base%P;
   }
   return res;
}

inline void add(int &x,int y){
   x += y; if(x >= P)x -= P;
}

struct bit{
   int val[N],cnt[N];
   void init(){
      memset(val,0,sizeof val); memset(cnt,0,sizeof cnt);
   }
   inline void modify(int x,int v){
      while(x < N){
         cnt[x]++; add(val[x],v); x += x&(-x);
      }
   }
   inline int query(int x){
      int res = 0;
      while(x){
         add(res,val[x]); x -= (x&(-x));
      }
      return res;
   }
   inline int q2(int x){
      int res = 0;
      while(x){
         res += cnt[x]; x -= (x&(-x));
      }
      return res;
   }
}s;

int a[N],n,cnt[N],d[N];
int b[N];
int pre[N],pos[N];
int ans;

int main(){
   scanf("%d",&n);
   fr(i,1,n)scanf("%d",a+i),a[i] = min(a[i],n),b[i] = a[i];

   sort(b+1,b+n+1);
   int j = 1,tot = 1;

   fr(i,1,n){
      while(j <= n && b[j] < i) ++j;
      cnt[i] = n-j+1-(n-i); d[i] = 1LL*(cnt[i]-1)*pow_mod(cnt[i],P-2)%P;
      tot = 1LL*tot*cnt[i]%P;
   }

   if(tot == 0){
      puts("0");
      return 0;
   }

   pre[0] = 1; pos[0] = 0;
   fr(i,1,n){
      if(d[i] == 0)pre[i] = 1,pos[i] = i;
      else pre[i] = 1LL*pre[i-1]*d[i]%P,pos[i] = pos[i-1];
   }

   s.init();
   ans = 0;
   fr(i,1,n){
      add(ans,1LL*(P+s.query(a[i])-s.query(pos[a[i]]))*pre[a[i]]%P*tot%P);
      s.modify(a[i],pow_mod(pre[a[i]],P-2));
   }
   ans = 1LL*ans*pow_mod(2,P-2)%P;

   s.init();
   nfr(i,n,1){
      if(d[a[i]] != 0){
         add(ans,(P-(1LL*(P+s.query(a[i]-1)-s.query(pos[a[i]]))*pre[a[i]]%P*tot%P
               *pow_mod(2,P-2)%P))%P);
      }
      add(ans,1LL*tot*s.q2(a[i]-1)%P);

      s.modify(a[i],pow_mod(pre[a[i]],P-2));
   }

   printf("%d\n",ans);
   return 0;
}
