#include using namespace std; #define int long long typedef pair P; int INF = 3e18+7; int mod = 998244353; int dx[] = {1, 0,-1, 0, 1, 1,-1,-1}; int dy[] = {0, 1, 0,-1, 1,-1, 1,-1}; struct BIT{ int N; vectorbit; BIT(int x) { N = x; bit.resize(x+1); } void add(int x,int y) { while(x <= N) { bit[x] += y; x += x&-x; } } int sum(int x) { int res = 0; while(x) { res += bit[x]; x-=x&-x; } return res; } int sum(int l, int r) { return sum(r+1)-sum(l); } }; signed main() { int N; cin >> N; vector

A(N); for(int i = 0; i < N; i++) { cin >> A[i].first; A[i].second = i+1; } sort(A.begin(),A.end()); vector

B = A; reverse(B.begin(),B.end()); BIT bt(N+5),bt2(N+5),bt3(N+5),bt4(N+5); vectorcnt1(N+5),cnt2(N+5); for(int i = 0; i < N; i++) { cnt1[A[i].second] = bt.sum(A[i].second,N); cnt2[A[i].second] = bt2.sum(A[i].second,N); bt.add(A[i].second,A[i].first); bt2.add(A[i].second,1); } int ans = 0; for(int i = 0; i < N; i++) { ans += cnt2[B[i].second]%mod*bt3.sum(0,B[i].second-1)%mod; ans %= mod; ans += cnt1[B[i].second]%mod*bt4.sum(0,B[i].second-1)%mod; ans %= mod; ans += cnt2[B[i].second]%mod*bt4.sum(0,B[i].second-1)%mod*B[i].first%mod; ans %= mod; bt3.add(B[i].second,B[i].first); bt4.add(B[i].second,1); } cout << ans << endl; }