#include #include using namespace std; using namespace atcoder; using ll=long long; using ld=long double; ld pie=3.14159265359; ll inf=10000000000000001; ll mod=998244353; template vector compress(vector &X) { // ソートした結果を vals に vector vals = X; sort(vals.begin(), vals.end()); // 隣り合う重複を削除(unique), 末端のゴミを削除(erase) 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(); } return vals; } ll op(ll a,ll b){ return (a+b)%mod; } ll e(){ return 0; } int main(){ ll n; cin >> n; vectora(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vectoraa=a; vectorb=compress(aa); mapmemo; for (ll i = 0; i < b.size(); i++) { memo[b[i]]=i; } vectorc(b.size(),0),d(b.size(),0),ee(b.size(),0),f(b.size(),0); for (ll i = 0; i < a.size(); i++) { c[memo[a[i]]]+=1; ee[memo[a[i]]]+=a[i]; ee[memo[a[i]]]%=mod; } ll last=b.size(); segtreeseg1(c),seg2(d),seg3(ee),seg4(f); ll ans=0; for (ll i = 0; i < n; i++) { ll y=memo[a[i]]; ll syoko=seg1.prod(0,y); ll syo=seg3.prod(0,y); ll daiko=seg2.prod(y+1,last); ll dai=seg4.prod(y+1,last); ans+=syoko*dai; ans%=mod; ans+=daiko*syo; ans%=mod; ans+=((syoko*daiko)%mod)*a[i]; seg1.set(y,seg1.get(y)-1); seg3.set(y,seg3.get(y)-a[i]); seg2.set(y,seg2.get(y)+1); seg4.set(y,seg4.get(y)+a[i]); } cout << ans << endl; }