#include #include #include using namespace atcoder; using namespace std; using ll = long long; using mint = modint998244353; int main() { int N;cin >> N; set value; vector A(N + 1, 0); for (int i = 1;i <= N;i++) { cin >> A[i]; value.insert(A[i]); } map tos; int K = 0; for (auto v : value) { tos[v] = K +1; K++; } fenwick_tree bits(K + 10); fenwick_tree bits2(K + 10); fenwick_tree cnt(K + 10); fenwick_tree V(K + 100); vector T(N + 1, 0); mint ans = 0; for (int i = N;i >= 1;i--) { int now = tos[A[i]]; T[i] = (A[i] * cnt.sum(0, now)) + bits.sum(0, now); bits.add(now, A[i]); bits2.add(now, T[i]); cnt.add(now, 1); mint nd = V.sum(0, now); ans += (A[i] * nd) + bits2.sum(0, now); V.add(now, cnt.sum(0, now)); } cout << ans.val() << endl; }