#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; #include using namespace atcoder; using mint = modint998244353; int main(){ int n; cin >> n; vector a(n); rep(i, n) cin >> a.at(i); map mp; rep(i, n){ mp[a.at(i)] = -1; } int c = 0; repit(itr, mp){ itr->second = c; c++; } vector b(n); rep(i, n){ b.at(i) = mp[a.at(i)]; } int m = mp.size(); fenwick_tree fwi(m), fwi2(m); fenwick_tree fwm(m), fwm2(m); vector vil(n, 0), vir(n, 0); vector vml(n, 0), vmr(n, 0); rep(i, n){ vil.at(i) = fwi.sum(b.at(i)+1, m); vml.at(i) = fwm.sum(b.at(i)+1, m); fwi.add(b.at(i), 1); fwm.add(b.at(i), a.at(i)); } rrep(i, n){ vir.at(i) = fwi2.sum(0, b.at(i)); vmr.at(i) = fwm2.sum(0, b.at(i)); fwi2.add(b.at(i), 1); fwm2.add(b.at(i), a.at(i)); } mint ans = 0; rep(i, n){ if(vil.at(i) != 0 && vir.at(i) != 0){ mint add = (mint)vil.at(i) * vmr.at(i); add += (mint)vir.at(i) * vml.at(i); add += (mint)vir.at(i) * vil.at(i) * a.at(i); ans += add; } } cout << ans.val() << endl; return 0; }