/* -*- coding: utf-8 -*- * * 2250.cc: No.2250 Split Permutation - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MOD = 998244353; /* typedef */ template struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; } MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } bool operator==(const MI m) const { return v == m.v; } bool operator!=(const MI m) const { return v != m.v; } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; using mi = MI; template struct BIT { int n; vector bits; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; bits.assign(n + 1, 0); } T sum(int x) { x = min(x, n); T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { if (x <= 0) return; while (x <= n) { bits[x] += v; x += (x & -x); } } int lower_bound(T v) { int k = 1; while ((k << 1) <= n) k <<= 1; int x = 0; for (; k > 0; k >>= 1) if (x + k <= n && bits[x + k] < v) { x += k; v -= bits[x]; } return x + 1; } }; /* global variables */ int ps[MAX_N]; mi es[MAX_N * 2 + 1], inves[MAX_N * 2 + 1]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", ps + i); int n2 = n * 2; es[0] = inves[0] = 1; mi inv2 = mi(2).inv(); for (int i = 0; i < n2; i++) { es[i + 1] = es[i] * 2; inves[i + 1] = inves[i] * inv2; } BIT bit0; BIT bit1; bit0.init(n); bit1.init(n); mi sum = 0; for (int i = 0; i < n; i++) { int c = bit0.sum(n) - bit0.sum(ps[i]); mi s = bit1.sum(n) - bit1.sum(ps[i]); sum += es[n - 1] * c - s * inves[i]; bit0.add(ps[i], 1); bit1.add(ps[i], es[n - 1 + i]); } printf("%d\n", (int)sum); return 0; }