#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long n = 200000; vector BIT(200020); void add(long long i, long long x) { while (i <= n) { BIT[i] += x; i += i & -i; } } long long csum(long long i) { long long count = 0; while (i > 0) { count += BIT[i]; i -= i & -i; } return count; } long long sum(long long l, long long r) { return csum(r) - csum(l - 1); } vector> vec; long long a[200020], o[200020]; long long l[200020] = {}, r[200020] = {}; long long sum1[200020] = {}, sum2[200020] = {}; long long mod = 998244353; int main() { cin >> n; for (int i = 0; i < n; i++) { long long a; cin >> a; vec.emplace_back(make_pair(a, i)); } sort(vec.begin(), vec.end()); int now = 1; for (int i = 0; i < n; i++) { if (i != 0 && vec[i].first != vec[i - 1].first)now++; o[now] = vec[i].first; a[vec[i].second] = now; } add(a[0], 1); for (int i = 1; i < n; i++) { l[i] = sum(a[i] + 1, n); add(a[i], 1); } for (int i = 1; i <= n; i++) { add(i, -BIT[i]); } add(a[n - 1], 1); for (int i = n - 2; i >= 0; i--) { r[i] = sum(1, a[i] - 1); add(a[i], 1); } for (int i = 1; i <= n; i++) { add(i, -BIT[i]); } add(a[0], o[a[0]]); for (int i = 1; i < n; i++) { sum1[i] = sum(a[i] + 1, n); add(a[i], o[a[i]]); } for (int i = 1; i <= n; i++) { add(i, -BIT[i]); } add(a[n - 1], o[a[n - 1]]); for (int i = n - 2; i >= 0; i--) { sum2[i] = sum(1, a[i] - 1); add(a[i], o[a[i]]); } long long ans = 0; for (int i = 0; i < n; i++) { l[i] %= mod, r[i] %= mod, sum1[i] %= mod, sum2[i] %= mod; ans += o[a[i]] * l[i] % mod * r[i] % mod; ans += sum1[i] * r[i] % mod, ans += sum2[i] * l[i] % mod; ans %= mod; } cout << ans << endl; }