#include #ifdef LOCAl #include "../library/misc/debug.h" #else #define debug(...) 42 #endif // LOCAL using namespace std; template struct Combina { int n; std::vector fact, invfact; Combina() {} Combina(int _n) : n(_n), fact(_n + 1), invfact(_n + 1) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; invfact[n] = 1 / fact[n]; for (int i = n; i >= 1; i--) invfact[i - 1] = invfact[i] * i; } T binom(int a, int b) { if (a < b || b < 0) return 0; return fact[a] * invfact[b] * invfact[a - b]; } long long lucas(long long n, long long m, long long p) { if (n > 0 || m > 0) return lucas(n / p, m / p, p) * lucas(n % p, m % p, p) % p; else return 1ll; } }; template T mod_inv_in_range(T a, T m) { // assert(0 <= a && a < m); T x = a, y = m; // coeff of a in x and y T vx = 1, vy = 0; while (x) { T k = y / x; y %= x; vy -= k * vx; std::swap(x, y); std::swap(vx, vy); } assert(y == 1); return vy < 0 ? m + vy : vy; } template struct extended_gcd_result { T gcd; T coeff_a, coeff_b; }; template extended_gcd_result extended_gcd(T a, T b) { T x = a, y = b; // coeff of a and b in x and y T ax = 1, ay = 0; T bx = 0, by = 1; while (x) { T k = y / x; y %= x; ay -= k * ax; by -= k * bx; std::swap(x, y); std::swap(ax, ay); std::swap(bx, by); } return {y, ay, by}; } template T mod_inv(T a, T m) { a %= m; a = a < 0 ? a + m : a; return mod_inv_in_range(a, m); } template struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: int v; public: modnum() : v(0) {} modnum(int64_t v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { int64_t v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = mod_inv_in_range(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v -= MOD-o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator *= (const modnum& o) { v = int(int64_t(v) * int64_t(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } // @param 0-indexed Fenwick (binary indexed tree / Fenwick tree) (i : [0, len)) template struct Fenwick { int n; std::vector data; Fenwick(int len = 0) : n(len), data(len) {} void reset() { std::fill(data.begin(), data.end(), T(0)); } void add(int pos, T v) { // a[pos] += v pos++; while (pos > 0 && pos <= n) data[pos - 1] += v, pos += pos & -pos; } T sum(int k) const { // a[0] + ... + a[k - 1] T res = 0; while (k > 0) res += data[k - 1], k -= k & -k; return res; } T sum(int l, int r) const { return sum(r) - sum(l); } // a[l] + ... + a[r - 1] int kth(T k) { int ret = 0; for (int i = 1 << std::__lg(n); i; i /= 2) if (ret + i <= n && k >= data[ret + i - 1]) ret += i, k -= data[ret - 1]; return ret; } template friend OStream &operator<<(OStream &os, const Fenwick &bit) { T prv = 0; os << '['; for (int i = 1; i <= bit.n; i++) { T now = bit.sum(i); os << now - prv << ',', prv = now; } return os << ']'; } }; int main() { std::cin.tie(nullptr)->sync_with_stdio(false); using num = modnum<998244353>; int N; std::cin >> N; std::vector P(N); for (auto &x : P) std::cin >> x, x--; Fenwick ft(N); num ans = 0; Combina comb(N * 2); for (int i = 0; i < N; i++) { int A = ft.sum(0, P[i]); int B = i - A; int C = N - i - 1 - P[i] + A; int D = P[i] - A; assert(A + B + C + D == N - 1); ans += comb.binom(A + C, A) * comb.binom(B + D, B); ft.add(P[i], 1); } std::cout << ans << "\n"; }