#include using namespace std; #include using namespace atcoder; using mint = modint998244353; int N; using Vi = std::vector ; using VVi = vector; using Vm = vector; int main () { cin >> N; vector A(N); for (auto& a : A) cin >> a; vector ID(N); iota(ID.begin(), ID.end(), 0); sort(ID.begin(), ID.end(), [&](int a, int b){return A[a] > A[b];}); set st; st.insert(-1); st.insert(N); vector sum(N + 1, 0); for (int i = 0; i < N; i ++) { sum[i + 1] = sum[i] + A[i]; } mint ans = 0; for (auto& i : ID) { mint pl = A[i]; int l = *prev(st.lower_bound(i)) + 1, r = *st.lower_bound(i); ans += pl * (sum[r] - sum[l]); st.insert(i); } cout << ans.val() << endl; }