#include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0;i<(n);i++) const ULL M = 998244353; template< class S, S(*op)(S a, S b), S(*e)() > struct segtree { private: int N; vector V; public: segtree(int n) { N = 1; while (N < n) N *= 2; V.assign(N * 2, e()); } segtree(vector A) { N = 1; while (N < A.size()) N *= 2; V.assign(N * 2, e()); rep(i, A.size()) V[N + i] = A[i]; for (int i = N - 1; i >= 1; i--) V[i] = op(V[i * 2], V[i * 2 + 1]); } void set(int p, S v) { p += N; V[p] = v; while (p != 1) { p /= 2; V[p] = op(V[p * 2], V[p * 2 + 1]); } } S get(int p) { p += N; return V[p]; } S prod(int l, int r) { S ans_l = e(), ans_r = e(); l += N; r += N; while (l < r) { if (l & 1) ans_l = op(ans_l, V[l++]); if (r & 1) ans_r = op(V[--r], ans_r); l /= 2; r /= 2; } return op(ans_l, ans_r); } }; struct S{ ULL s,c; }; S operator+(S l, S r){ S res = { l.s+r.s, l.c+r.c }; if(res.s>=M) res.s-=M; if(res.c>=M) res.c-=M; return res; } S operator*(S l, S r){ return { (l.s*r.c+l.c*r.s)%M, l.c*r.c%M}; } S op(S l, S r) { return l+r; } S e() { return {0,0}; } using RQ = segtree; int lower_bound_idx(const vector>& ref, pair v){ int l,r; l=0; r=ref.size()+1; while(l+1>N; vector A(N); rep(i,N) cin>>A[i]; vector> xA(N); rep(i,N) xA[i]={-A[i],i}; sort(xA.begin(),xA.end()); vector I(N); rep(i,N) I[xA[i].second]=i; vector lb(N); rep(i,N) lb[i]=lower_bound_idx(xA,{-A[i],-1}); rep(i,N) A[i]%=M; RQ G1(N); RQ G2(N); S ans=e(); rep(i,N){ ans = ans + G2.prod(0,lb[i]) * S{A[i],1}; G2.set(I[i], G2.get(I[i])+G1.prod(0,lb[i])*S{A[i],1}); G1.set(I[i], S{A[i],1}); } cout<