#include #include #define MOD 1000000007 #define maxn 1111 int num[maxn]; long long f[maxn]; long long ff[maxn]; long long vis[maxn]; int Scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } void init() { int i, j; ff[0] = 1; ff[1] = 1; for(i=2;i<=105;i++) ff[i] = ff[i-1]*i%MOD; f[1] = f[0] = 0; f[2] = 1; for(i=3;i<=100;i++) { int flag =0; for(j=0;j<=i-1;j++) { f[i]+=j*ff[i-1]%MOD; f[i]%=MOD; f[i]+=f[i-1]; f[i]%=MOD; } } } int main() { int aaa; init(); long long n, i,j,k; //printf("%I64d\n",f[4]); while(scanf("%I64d",&n)!=EOF) { memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { num[i] = Scan(); } long long res = 0; long long now = 0; for(i=1;i<=n;i++) { long long r = num[i]-1; for(j=1;j<=r;j++) { if(vis[j]) continue; long long first = now; for(k=j+1;k<=n;k++) { if(vis[k]==1) first++; } vis[j] = 1; long long flag = 0; long long zj = 0; for(k=n;k>=1;k--) { if(vis[k]==1){ flag++; continue; } else zj+=flag; } res+= first*ff[n-i]%MOD; res+=zj*ff[n-i]%MOD; res%=MOD; res+=f[n-i]%MOD; res%=MOD; vis[j] = 0; //printf("%d %d %I64d %I64d %I64d\n",i,j,first,zj,f[n-i]); } for(j=num[i]+1;j<=n;j++) { if(vis[j]) now++; } vis[num[i]] = 1; // printf("%I64d\n",res); } printf("%I64d\n",res); } return 0; }