#include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long int MOD = 1000000007; template struct fenwick_tree { vector x; fenwick_tree(int n) : x(n, 0) { } T sum(int i, int j) { if (i == 0) { T S = 0; for (j; j >= 0; j = (j & (j + 1)) - 1) S += x[j]; return S; } else return sum(0, j) - sum(0, i - 1); } void add(int k, T a) { for (; k < x.size(); k |= k + 1) x[k] += a; } }; vector res; int m = (int)10000000000LL; void add(int a) { int c = 0; res[0] += a - 1; int i = 0; while(true) { if ((int)res.size() <= i) { res.push_back(i); break; } res[i]++; if (res[i] >= m) { res[i] -= m; i++; } else { break; } } } void mul(int a) { for (int i = 0; i < res.size(); i++) { res[i] *= a; } int c = 0; int i = 0; while (true) { if ((int)res.size() <= i) { res.push_back(c); break; } res[i] += c; if (res[i] >= m) { c = res[i] / m; res[i] %= m; i++; } else { break; } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; //N = 100000; vector A(N); res.resize(1, 0); for (int i = 0; i < N; i++) { cin >> A[i]; //A[i] = N - i; A[i]--; } fenwick_tree ft(N); for (int i = 0; i < N; i++) { ft.add(i, 1); } for (int i = 0; i < N - 1; i++) { int k = ft.sum(0, A[i] - 1); ft.add(A[i], -1); //cerr << k << endl; add(k); mul(N - i - 1); } //cout << ((res + 1) - res) << endl; //bigint x = 1; add(1); for (int i = res.size() - 1; i >= 0; i--) { cout << res[i]; } cout << endl; }