#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include using namespace std; const int MOD=998244353; template vector compress(vector &X){ vector vals=X; sort(vals.begin(),vals.end()); vals.erase(unique(vals.begin(), vals.end()),vals.end()); for(int i=0;i<(int)X.size(); i++){ X[i]=lower_bound(vals.begin(),vals.end(),X[i])-vals.begin()+1; } return vals; } template struct BIT{ private: vector A; const int n; public: BIT(int _n) : A(_n+1,0), n(_n){} T sum(int i){ T s=0; while(i>0){ s=(s+A[i])%MOD; i-=i&-i; } return s; } T sum(int i,int j){ return (sum(j)-sum(i-1)+MOD)%MOD; } void add(int i,T x){ while(i<=n){ A[i]=(A[i]+x)%MOD; i+=i&-i; } } }; int main(){ int n; cin>>n; vector A(n),B(n); rep(i,n) cin>>A[i]; B=A; compress(B); BIT bitlcnt(n),bitlsum(n),bitrcnt(n),bitrsum(n); vector Lcnt(n),Rcnt(n),Lsum(n),Rsum(n); rep(i,n){ Lcnt[i]=bitlcnt.sum(B[i]+1,n); bitlcnt.add(B[i],1); Lsum[i]=bitlsum.sum(B[i]+1,n); bitlsum.add(B[i],A[i]); } for(int i=n-1;i>=0;i--){ Rcnt[i]=bitrcnt.sum(B[i]-1); bitrcnt.add(B[i],1); Rsum[i]=bitrsum.sum(B[i]-1); bitrsum.add(B[i],A[i]); } int ans=0; rep(i,n){ ans=(ans+Rcnt[i]*Lcnt[i]%MOD*A[i]%MOD)%MOD; ans=(ans+Rsum[i]*Lcnt[i]%MOD)%MOD; ans=(ans+Lsum[i]*Rcnt[i]%MOD)%MOD; } cout<