#include "bits/stdc++.h" using namespace std; using ll = long long; ll mod = 998244353; template struct BIT { int n; vectorbit; BIT(int n_) : n(n_ + 1), bit(n, 0) {} void add(int i, T x) { for (int idx = i; idx < n; idx += (idx & -idx)) { bit[idx] += x; bit[idx] %= mod; } } T sum(int i) { T s(0); for (int idx = i; idx > 0; idx -= (idx & -idx)) { s += bit[idx]; bit[idx] %= mod; } return s; } }; signed main() { int N; cin >> N; // 座圧パート vectorA(N), cA(N), zA(N); for (int i = 0; i < N; i++)cin >> A[i]; reverse(A.begin(), A.end()); for (int i = 0; i < N; i++)cA[i] = A[i]; sort(A.begin(), A.end()); A.erase(unique(A.begin(), A.end()), A.end()); for (int i = 0; i < N; i++) { zA[i] = lower_bound(A.begin(), A.end(), cA[i]) - A.begin(); zA[i]++; } A.resize(N); for (int i = 0; i < N; i++) { A[i] = cA[i]; } BIT leftsum(200010), rightsum(200010); for (int i = 0; i < N; i++) { rightsum.add(zA[i], A[i]); } BITleftcnt(200010), rightcnt(200010); for (int i = 0; i < N; i++) { rightcnt.add(zA[i], 1); } // setとBITを用いて答えを求める ll ans = 0; for (int i = 0; i < N; i++) { rightsum.add(zA[i], -A[i]); rightcnt.add(zA[i], -1); ll nowans = 0; ll sum1 = leftsum.sum(zA[i] - 1); ll sum2 = rightsum.sum(200009) - rightsum.sum(zA[i]); ll cnt1 = rightcnt.sum(200009) - rightcnt.sum(zA[i]); ll cnt2 = leftcnt.sum(zA[i] - 1); sum1 %= mod; sum2 %= mod; nowans += sum1 * cnt1; nowans %= mod; nowans += sum2 * cnt2; nowans %= mod; ll tmp = A[i]; tmp *= cnt1; tmp %= mod; tmp *= cnt2; tmp %= mod; nowans += tmp; nowans %= mod; ans += nowans; ans %= mod; leftsum.add(zA[i], A[i]); leftcnt.add(zA[i], 1); } cout << ans << endl; }