import java.io.*; import java.text.NumberFormat; import java.util.*; /** * Created by VIET ANH on 27/04/2015. */ public class Main { private static int T; private static long oo=Long.MAX_VALUE; private static long base=1000000007; public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (in.hasNextInt()) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i]=in.nextInt(); } long[] gt = new long[n+1]; gt[0]=1; for (int i = 1; i <= n; i++) { gt[i]=(gt[i-1]*i)%base; } long[] t = new long[n+1]; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { t[i]+=t[i-1]+j*gt[i-1]; t[i]%=base; } } long res=0; int[] dau=new int[n+1]; int[] freq=new int[n+1]; for (int i = 0; i < n; i++) { for (int j = 1; j < a[i]; j++) if (dau[j]==0){ res+=t[n-i-1]; res%=base; int cnt=0; for (int k = 1; k <= a[i]; k++) if (dau[k]==0&&ka[j]) cnt++; } res+=1L*freq[i]*cnt; res%=base; } System.out.println(res); } out.close(); } }