using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Numerics; // class Myon { public Myon() { } public static int Main() { new Myon().calc(); return 0; } Scanner cin; void calc() { cin = new Scanner(); int N = cin.nextInt(); int[] ar = cin.ArrayInt(N); //setFact(N + 3); BIT bit = new BIT(N + 3); BigInteger ans = 1; BigInteger fact = 1; for (int i = 1; i < N; i++) { fact *= i; } for (int i = 0; i < N; i++) { ans += fact * (ar[i] - bit.bitCal(ar[i]) - 1); //ans %= mod; bit.bitUpdate(ar[i], 1); if (i != N - 1) fact /= N - i - 1; } Console.WriteLine(ans); } //long mod = 1000000007; //BigInteger[] fact; //階乗 //void setFact(int N) //{ // fact = new BigInteger[N]; // fact[0] = 1; // for (int i = 1; i < N; i++) // { // fact[i] = fact[i - 1] * i; // //fact[i] %= mod; // } //} class BIT { int bitA; int[] bit; const int INF = int.MaxValue - 10; public BIT(int maxa) { bitA = maxa; bit = new int[bitA + 1]; for (int i = 0; i < bitA; i++) { bit[i] = 0; } } public void bitUpdate(int a, int val) { for (int i = a; i <= bitA; i += i & -i) { bit[i] += val; } } public int bitCal(int max, int min) { return bitCal(max) - bitCal(min - 1); } public int bitCal(int a) { int ret = 0; for (int i = a; i > 0; i &= i - 1) { ret += bit[i]; } return ret; } } } class Scanner { string[] s; int i; char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public int nextInt() { return int.Parse(next()); } public int[] ArrayInt(int N, int add = 0) { int[] Array = new int[N]; for (int i = 0; i < N; i++) { Array[i] = nextInt() + add; } return Array; } public long nextLong() { return long.Parse(next()); } public long[] ArrayLong(int N, long add = 0) { long[] Array = new long[N]; for (int i = 0; i < N; i++) { Array[i] = nextLong() + add; } return Array; } public double nextDouble() { return double.Parse(next()); } public double[] ArrayDouble(int N, double add = 0) { double[] Array = new double[N]; for (int i = 0; i < N; i++) { Array[i] = nextDouble() + add; } return Array; } }