#pragma GCC optimize("Ofast,no-stack-protector") #include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; #define F first #define S second const int N=3e5+10; const ll mod=998244353; int p[N]; ll fpow(ll x, int k){ ll res=1; while(k){ if (k&1) res=res*x%mod; x=x*x%mod; k>>=1; } return res; } ll inv[N<<1], fac[N<<1]; ll C(int n, int k){return fac[n]*inv[k]%mod*inv[n-k]%mod;} int n; int bit[N]; void update(int x){for(;x<=n;x+=x&-x) bit[x]++;} int query(int x){ int res=0; for(;x;x-=x&-x) res+=bit[x]; return res; } void _solve(){ cin >> n; fac[0]=inv[0]=1; for(int i=1;i<=(n<<1);i++){ fac[i]=fac[i-1]*i%mod; inv[i]=fpow(fac[i], mod-2); } for(int i=1;i<=n;i++) cin >> p[i]; ll ans=0; for(int i=1;i<=n;i++){ int b=query(p[i]); int a=i-b-1; int d=p[i]-b-1; int c=n-i-d; ans+=C(a+d, min(a, d))*C(b+c, min(b, c))%mod; ans%=mod; update(p[i]); } cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int _t=1; //cin >> _t; while(_t--) _solve(); }