#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; struct S { mint cnt, smn, smx, s; }; S op(S x, S y) { return {x.cnt + y.cnt, x.smn + y.smn, x.smx + y.smx, x.s + y.s}; } S e() { return {}; } struct F { int mn, mx; }; F id() { return {-1, -1}; } F composition(F f, F g) { if (f.mn == -1) f.mn = g.mn; if (f.mx == -1) f.mx = g.mx; return f; } S mapping(F f, S x) { if (f.mn != -1) { x.smn = f.mn * x.cnt; x.s = x.smx * f.mn; } if (f.mx != -1) { x.smx = f.mx * x.cnt; x.s = x.smn * f.mx; } return x; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI a(n); rep(i, n) cin >> a[i]; lazy_segtree seg(vector(n + 1, S{1, 0, 0, 0})); mint ans; constexpr int INF = 1001001001; vector

st_mx{{INF, -1}}, st_mn{{-INF, -1}}; rep(i, n) { while (st_mx.back().first <= a[i]) st_mx.pop_back(); while (st_mn.back().first >= a[i]) st_mn.pop_back(); seg.apply(st_mx.back().second + 1, i + 1, {-1, a[i]}); seg.apply(st_mn.back().second + 1, i + 1, {a[i], -1}); ans += seg.prod(0, i + 1).s; st_mx.emplace_back(a[i], i); st_mn.emplace_back(a[i], i); } cout << ans.val() << '\n'; }