#include #include #include #include #include using namespace std; using ll = long long; const ll p = 998244353; struct BIT { vector a; BIT(int n) { a.resize(n + 1); } void add(int k, int v) { v %= p; while (k < a.size()) { a[k] += v; a[k] %= p; k += k & -k; } } ll sum(int k) { ll ret = 0; while (k > 0) { ret += a[k]; ret %= p; k -= k & -k; } return ret; } }; int main() { int N; cin >> N; vector> ps(N); for (int i=0;i> a; ps[i] = make_pair(-a, -i); } sort(ps.begin(), ps.end()); BIT bit0(N); BIT bit1(N); BIT bit2(N); BIT bit3(N); ll ans = 0; for (auto e:ps) { int id = 1 + -e.second; ll cost = -e.first; bit0.add(id, 1); bit1.add(id, cost); bit2.add(id, bit1.sum(id - 1) + bit0.sum(id - 1) * cost % p); bit3.add(id, bit0.sum(id - 1)); ans += bit2.sum(id - 1) + bit3.sum(id - 1) * cost % p; ans %= p; } cout << ans << endl; }