#include #include #include using namespace std; #include #include using mint = atcoder::modint998244353; // range set range sum // constexpr int md = 998244353; using S = pair; // (sum, size) using F = int; S op(S a, S b) { return {(a.first + b.first), a.second + b.second}; } S e() { return {0, 0}; } S mapping(F f, S x) { return f == 0 ? x : S(x.second * mint(f), x.second); } F composition(F f, F g) { return f == 0 ? g : f; } F id() { return 0; } using LST = atcoder::lazy_segtree; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); vector> maxi, mini; maxi.emplace_back(1 << 30, -1); mini.emplace_back(0, -1); mint ret = 0; int N; cin >> N; vector init(N, {0, 1}); LST mintree(init), maxtree(init); mint ans = 0; for (int i = 0; i < N; ++i) { int a; cin >> a; ret += (mint)a * a; { int r = i; while (maxi.back().first <= a) { int l = maxi.back().second; ret += (mint)(a - maxi.back().first) * mintree.prod(l, r).first; r = l; maxi.pop_back(); } maxi.emplace_back(a, r); maxtree.apply(r, i + 1, a); } { int r = i; while (mini.back().first >= a) { const int l = mini.back().second; ret += (mint)(a - mini.back().first) * maxtree.prod(l, r).first; r = l; mini.pop_back(); } mini.emplace_back(a, r); mintree.apply(r, i + 1, a); } ans += ret; } cout << ans.val() << '\n'; }