#include using namespace std; #define ALL(x) (x).begin(),(x).end() #define IO ios::sync_with_stdio(false),cin.tie(nullptr); #define LB(v, x) (ll)(lower_bound(ALL(v),x)-(v).begin()) #define UQ(v) sort(ALL(v)),(v).erase(unique(ALL(v)),v.end()) #define REP(i, n) for(ll i=0; i<(ll)(n); i++) #define FOR(i, a, b) for(ll i=(ll)(a); (a)<(b) ? i<(b) : i>(b); i+=((a)<(b) ? 1 : -1)) #define chmax(a, b) ((a)<(b) ? ((a)=(b), 1) : 0) #define chmin(a, b) ((a)>(b) ? ((a)=(b), 1) : 0) template using rpriority_queue=priority_queue,greater>; using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using ull=uint64_t; using VI=vector; using VVI=vector; using VL=vector; using VVL=vector; using PL=pair; using VP=vector; using WG=vector>>; /// @file modint.hpp /// @brief ModInt template struct ModInt{ ModInt(ll x=0){ value=(x>=0?x%MOD:MOD-(-x)%MOD); } ModInt operator-() const { return ModInt(-value); } ModInt operator+() const { return ModInt(*this); } ModInt& operator+=(const ModInt& other) { value+=other.value; if(value>=MOD) value-=MOD; return*this; } ModInt& operator-=(const ModInt& other) { value+=MOD-other.value; if(value>=MOD) value-=MOD; return*this; } ModInt& operator*=(const ModInt other) { value=value*other.value%MOD; return*this; } ModInt& operator/=(ModInt other) { (*this)*=other.inv(); return*this; } ModInt operator+(const ModInt& other) const { return ModInt(*this)+=other; } ModInt operator-(const ModInt& other) const { return ModInt(*this)-=other; } ModInt operator*(const ModInt& other) const { return ModInt(*this)*=other; } ModInt operator/(const ModInt& other) const { return ModInt(*this)/=other; } bool operator==(const ModInt& other) const { return value==other.value; } bool operator!=(const ModInt& other) const { return value!=other.value; } friend ostream& operator<<(ostream& os, const ModInt& x) { return os<>(istream& is, ModInt& x) { ll v; is>>v; x=ModInt(v); return is; } ModInt pow(ll x) const { ModInt ret(1),mul(value); while(x) { if(x&1) ret*=mul; mul*=mul; x>>=1; } return ret; } ModInt inv() const { return pow(MOD-2); } ll val() {return value; } static constexpr ll get_mod() { return MOD; } private: ll value; }; using Mod998=ModInt<998244353>; using Mod107=ModInt<1000000007>; using VM=vector; using mint=Mod998; /// @brief 二項係数・階乗計算 template struct Combinatorics{ Combinatorics()=default; /// @brief 二項係数の前計算 /// @note O(N) Combinatorics(int n){ fac=vector(n+1); finv=vector(n+1); fac[0]=1; for(int i=1; i<=n; i++) fac[i]=fac[i-1]*i; finv[n]=fac[n].inv(); for(int i=n; i>=1; i--) finv[i-1]=finv[i]*i; } /// @brief nCr を返す /// @note n < 0, r < 0, n < r のときは 0 を返す T comb(ll n, ll r){ if(n<0||r<0||n-r<0) return 0; return fac[n]*finv[r]*finv[n-r]; } /// @brief nPr を返す /// @note n < 0, r < 0, n < r のときは 0 を返す T perm(ll n, ll r){ if(n<0||r<0||n-r<0) return 0; return fac[n]*finv[n-r]; } /// @brief n! を返す T factrial(int n){ return fac[n]; } /// @brief (n!)^-1 を返す T factinv(int n){ return finv[n]; } /// @brief nCr を返す T operator()(ll n, ll r){ return comb(n,r); } private: vector fac,finv; }; /// @brief マージソート木 template struct MergeSortTree{ MergeSortTree()=default; /// @brief 配列 v からマージソート木を構築する MergeSortTree(const vector& v){ n=v.size(); mx=*max_element(ALL(v)),mn=*min_element(ALL(v)); dat=vector>(n<<1); REP(i,n) dat[n+i]={v[i]}; for(int i=n-1; i>0; i--){ merge(ALL(dat[i<<1]),ALL(dat[i<<1|1]),back_inserter(dat[i])); } } /// @brief 区間 [l, r) に存在する val 未満の値の個数を返す /// @note O(log(N)^2) int count_lt(int l, int r, T val){ l+=n,r+=n; int ret=0; while(l>=1,r>>=1; } return ret; } /// @brief 区間 [l, r) に存在する val 以下の値の個数を返す /// @note O(log(N)^2) int count_le(int l, int r, T val){ return count_lt(l,r,val+1); } /// @brief 区間 [l, r) に存在する val より大きい値の個数を返す /// @note O(log(N)^2) int count_gt(int l, int r, T val){ return r-l-count_le(l,r,val); } /// @brief 区間 [l, r) に存在する val 以上の値の個数を返す /// @note O(log(N)^2) int count_ge(int l, int r, T val){ return r-l-count_lt(l,r,val); } /// @brief 区間 [l, r) の小さい方から k(1-indexed) 番目の値を返す /// @note O(log(N)^3) T kth(int l, int r, int k){ T lo=mn-1,hi=mx+1; while(hi-lo>1){ T mid=(hi+lo)/2; if(count_lt(l,r,mid)>=k) hi=mid; else lo=mid; } return lo; } private: int n; vector> dat; T mx,mn; }; //---------------------------------------------------------- int main() { int N; cin>>N; VI P(N); REP(i,N) cin>>P[i]; Combinatorics com(4e5); MergeSortTree mst(P); mint ans=0; REP(i,N) { int p=P[i]; int A=mst.count_gt(0,i,p),B=mst.count_lt(0,i,p),C=mst.count_lt(i,N,p),D=mst.count_gt(i,N,p); ans+=com(A+C,A)*com(B+D,B); } cout<