#include #include #include using namespace std; const int mod = 469762049; const int proot = 2187; int powinv[29], roots[29]; void fourier_transform(int s, vector &v, bool inverse) { if (v.size() != s) v.resize(s); for (int i = 0, j = 1; j < s - 1; ++j) { for (int k = s >> 1; k > (i ^= k); k >>= 1); if (i < j) swap(v[i], v[j]); } int sc = 0, sz = 1; while (sz < s) sz *= 2, ++sc; vector pw(s + 1); pw[0] = 1; for (int i = 1; i <= s; i++) pw[i] = 1LL * pw[i - 1] * roots[sc] % mod; int qs = s; for (int b = 1; b < s; b <<= 1) { qs >>= 1; for (int i = 0; i < s; i += b * 2) { for (int j = i; j < i + b; ++j) { int f0 = v[j], f1 = 1LL * pw[(inverse ? b * 2 - j + i : j - i) * qs] * v[j + b] % mod; v[j] = (f0 + f1 < mod ? f0 + f1 : f0 + f1 - mod); v[j + b] = (f0 - f1 >= 0 ? f0 - f1 : f0 - f1 + mod); } } } if (inverse) { for (int i = 0; i < s; ++i) v[i] = 1LL * v[i] * powinv[sc] % mod; } } vector convolve(vector vx, vector vy) { int s = max(vx.size(), vy.size()) * 2; fourier_transform(s, vx, false); fourier_transform(s, vy, false); for (int i = 0; i < s; ++i) vx[i] = 1LL * vx[i] * vy[i] % mod; fourier_transform(s, vx, true); int mx = 1; for (int i = 0; i < s; ++i) { if (vx[i] >= 10) { vx[i + 1] += vx[i] / 10; vx[i] %= 10; } while (mx * 2 <= i + 1 && vx[i] > 0) mx *= 2; } vx.resize(mx); return vx; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); powinv[1] = (mod + 1) / 2; for (int i = 2; i <= 26; ++i) powinv[i] = 1LL * powinv[i - 1] * powinv[i - 1] % mod; roots[26] = proot; for (int i = 25; i >= 0; --i) roots[i] = 1LL * roots[i + 1] * roots[i + 1] % mod; int n; cin >> n; vector p(n), bit(n); for (int i = 0; i < n; ++i) cin >> p[i]; vector > a(n), b(n); for (int i = n - 1; i >= 0; --i) { int x = 0; for (int j = p[i] - 1; j >= 1; j -= j & (-j)) x += bit[j]; for (int j = p[i]; j < n; j += j & (-j)) bit[j]++; int sa = 1, sb = 1, cb = i + 1; while (x > 0) { a[n - i - 1].push_back(x % 10); x /= 10; if (sa < a[n - i - 1].size()) sa *= 2; } a[n - i - 1].resize(sa); while (cb > 0) { b[i].push_back(cb % 10); cb /= 10; if (sb < b[i].size()) sb *= 2; } b[i].resize(sb); } int s = 1; while (2 << s < n) s++; for (int i = 0; i <= s; ++i) { for (int j = 0; j + (1 << i) < n; j += (2 << i)) { vector c = convolve(b[j], a[j + (1 << i)]); b[j] = convolve(b[j], b[j + (1 << i)]); if(a[j].size() < c.size()) a[j].resize(c.size()); for (int k = 0; k < c.size(); ++k) { a[j][k] += c[k]; if (a[j][k] >= 10 && k + 1 != c.size()) a[j][k] -= 10, ++a[j][k + 1]; } if (a[j].back() >= 10) { a[j].back() += 10; a[j].resize(a[j].size() * 2); ++a[j][a[j].size() >> 1]; } } } ++a[0][0]; for (int i = 0; i + 1 < a[0].size(); ++i) { if (a[0][i] >= 10) a[0][i] -= 10, ++a[0][i + 1]; } if (a[0].back() >= 10) { a[0].back() += 10; a[0].resize(a[0].size() * 2); ++a[0][a[0].size() >> 1]; } int digits = 1; for (int i = 0; i < a[0].size(); ++i) { if (a[0][i] > 0) digits = i + 1; } for (int i = digits - 1; i >= 0; --i) cout << a[0][i]; cout << '\n'; return 0; }