#include using namespace std; using ll = long long; template using vec = vector; template using vvec = vector>; template bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template bool chmax(T& a,T b){if(a=0;i--) #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; constexpr ll mod = 998244353; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} friend ostream &operator<<(ostream& os,const mint& a){ return os << a.x; } friend istream &operator>>(istream& is,mint& a){ ll t; is >> t; a = mint(t); return (is); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } bool operator==(const mint a)const{ return x==a.x; } }; template class SegmentTree{ private: int sz; vector seg; const F op; const Monoid e; public: SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){ sz = 1; while(sz<=n) sz <<= 1; seg.assign(2*sz,e); } void set(int k, const Monoid &x){ seg[k+sz] = x; } void build(){ for(int i=sz-1;i>0;i--){ seg[i] = op(seg[2*i],seg[2*i+1]); } } void update(int k,const Monoid &x){ k += sz; seg[k] += x; while(k>>=1){ seg[k] = op(seg[2*k],seg[2*k+1]); } } Monoid query(int l,int r){ Monoid L = e,R = e; for(l+=sz,r+=sz;l>=1,r>>=1){ if(l&1) L = op(L,seg[l++]); if(r&1) R = op(seg[--r],R); } return op(L,R); } Monoid operator[](const int &k)const{ return seg[k+sz]; } }; template class Compress { int N; map value; vec v; vec cmp; public: Compress(){} void add(T x){v.push_back(x);} void build(){ for(auto &x:v) cmp.push_back(x); sort(cmp.begin(),cmp.end()); cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end()); N = cmp.size(); } int id(T val) {return lower_bound(cmp.begin(),cmp.end(),val)-cmp.begin();} T val(int id) {return cmp[id];} int size(){return N;} }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec A(N); Compress cmp; rep(i,N){ cin >> A[i]; cmp.add(A[i]); } cmp.build(); int M = cmp.size(); auto op = [](mint a,mint b){return a+b;}; SegmentTree seg(M,op,(mint) 0); vec> L; vec> R; rep(i,3){ L.push_back(seg); R.push_back(seg); } mint ans = 0; rep(i,N){ int id = cmp.id(A[i]); drep(j,3){ if(j) L[j].update(id,L[j-1].query(id+1,M)); else L[j].update(id,1); } ans += L[2][id]*A[i]; } drep(i,N){ int id = cmp.id(A[i]); drep(j,3){ if(j) R[j].update(id,R[j-1].query(0,id)); else R[j].update(id,1); } ans += R[2][id]*A[i]; } rep(i,M) L[0].set(i,0); L[0].build(); rep(i,N){ int id = cmp.id(A[i]); ans += L[0].query(id+1,M)*R[0].query(0,id)*A[i]; L[0].update(id,1); R[0].update(id,-1); } cout << ans << "\n"; }