#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include using namespace std; using namespace atcoder; using ll=long long; void IO(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); } const int MAX = 510000; const int MOD = 998244353; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main(){ IO(); ll n; cin>>n; vector p(n); for(ll i=0;i>p[i]; p[i]--; } vector left_smaller(n),left_larger(n),right_smaller(n),right_larger(n); fenwick_tree fw1(n),fw2(n); for(ll i=0;i=0;i--){ right_smaller[i]=fw2.sum(0,p[i]); right_larger[i]=n-1-i-right_smaller[i]; fw2.add(p[i],1); } COMinit(); ll ans=n; for(ll i=1;ib){ swap(a,b); } ll c=left_larger[i]; ll d=right_smaller[i]; if(c>d){ swap(c,d); } ans+=(b+COM(a,b)-1); ans%=MOD; ans+=(d+COM(c,d)-1); ans%=MOD; } cout<