#include using namespace std; typedef long long ll; const ll nax = 2 * 1e5 + 7; const ll mod = 998244353; struct bit { const static ll siz = 2e5 + 7; ll bit[siz]; ll get(ll idx) { ll sum = 0; while (idx > 0) { sum += bit[idx]; idx = (idx & (idx + 1)) - 1; } return sum; } void modify(ll idx, ll val) { while (idx < siz) { bit[idx] += val; idx = idx | (idx + 1); } } ll range(ll l, ll r) { return (get(r) - get(l - 1)); } } rightcnt, leftcnt, rightsum, leftsum; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vectorv(n + 1); mapm; for (ll i = 0; i < n; i++) { cin >> v[i]; m[v[i]]++; } vectora; for (int i = 0; i < n; i++) { if (m[v[i]]) { a.push_back(v[i]); m[v[i]] = 0; } } sort(a.begin(), a.end()); vectorb(n); for (ll i = 0; i < n; i++) { b[i] = lower_bound(a.begin(), a.end(), v[i]) - a.begin(); } for (ll i = 0; i < n; i++) { rightcnt.modify(b[i], 1); rightsum.modify(b[i], v[i]); } ll ans = 0; for (ll i = 0; i < n; i++) { rightsum.modify(b[i], -v[i]); rightcnt.modify(b[i], -1); ll cnt1 = rightcnt.range(0, b[i]); ll cnt2 = leftcnt.range(b[i] + 1, nax); ans += ((v[i] % mod) * (cnt1 % mod) * (cnt2 % mod)); ans %= mod; ans += ((rightsum.range(0, b[i]) % mod) * (cnt2 % mod)); ans %= mod; ans += ((leftsum.range(b[i] + 1, nax) % mod) * (cnt1 % mod)); ans %= mod; leftsum.modify(b[i], v[i]); leftcnt.modify(b[i], 1); } cout << ans << endl; }