#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)-sum(l-1); } }; 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); int j = i+1; while (j < N) { if(A[j-1].first != A[j].first) { break; } cnt1[A[j].second] = bt.sum(A[j].second,N); cnt2[A[j].second] = bt2.sum(A[j].second,N); j++; } bt.add(A[i].second,A[i].first); bt2.add(A[i].second,1); j = i+1; while (j < N) { if(A[j-1].first != A[j].first) { break; } i = j; bt.add(A[j].second,A[j].first); bt2.add(A[j].second,1); j++; } } int ans = 0; for(int i = 0; i < N; i++) { ans += cnt2[B[i].second]%mod*bt3.sum(1,B[i].second)%mod; ans %= mod; ans += cnt1[B[i].second]%mod*bt4.sum(1,B[i].second)%mod; ans %= mod; ans += cnt2[B[i].second]%mod*bt4.sum(1,B[i].second)%mod*B[i].first%mod; ans %= mod; int j = i+1; while (j < N) { if(B[j-1].first != B[j].first) { break; } ans += cnt2[B[j].second]%mod*bt3.sum(1,B[j].second)%mod; ans %= mod; ans += cnt1[B[j].second]%mod*bt4.sum(1,B[j].second)%mod; ans %= mod; ans += cnt2[B[j].second]%mod*bt4.sum(1,B[j].second)%mod*B[j].first%mod; ans %= mod; j++; } bt3.add(B[i].second,B[i].first); bt4.add(B[i].second,1); j = i+1; while (j < N) { if(B[j-1].first != B[j].first) { break; } i = j; bt3.add(B[j].second,B[j].first); bt4.add(B[j].second,1); j++; } } cout << ans << endl; }