#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; #include #include using mint = atcoder::modint998244353; ll op1(ll a, ll b) { return min(a, b); } ll e1() { return INF; } ll op2(ll a, ll b) { return max(a, b); } ll e2() { return 0; } using RangeMinQuery = atcoder::segtree; using RangeMaxQuery = atcoder::segtree; template struct SegmentTreeDual { using F = function; SegmentTreeDual() = default; SegmentTreeDual(int n, F f, T e) { this->n = n; this->f = f; this->e = e; dat = vector(n << 1, e); } void build(const vector &a) { assert((int)a.size() == n); for (int i = 0; i < (int)a.size(); i++) dat[i + n] = a[i]; } T operator[](int i) { assert(0 <= i && i < n); i += n; T ret = e; while (i) { ret = f(ret, dat[i]); i >>= 1; } return ret; } void apply(int l, int r, T x) { assert(0 <= l && l <= r && r <= n); l += n; r += n; while (l < r) { if (l & 1) dat[l] = f(dat[l], x), l++; if (r & 1) r--, dat[r] = f(dat[r], x); l >>= 1; r >>= 1; } } int size() { return n; } private: int n; vector dat; F f; T e; }; template SegmentTreeDual RangeAddQuery(int n) { return SegmentTreeDual(n, [](T a, T b) { return a + b; }, 0); } template SegmentTreeDual> RangeUpdateQuery(int n) { auto f = [](pair a, pair b) { return a.second > b.second ? a : b; }; return SegmentTreeDual>(n, f, {0, -1}); } int main() { int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; auto raq = RangeAddQuery(N); auto minseg = RangeMinQuery(A); auto maxseg = RangeMaxQuery(A); mint ans = 0; for (int i = 0; i < N; i++) { int lo = i, hi = N; while (hi - lo > 1) { int mid = (hi + lo) / 2; if (minseg.prod(i, mid + 1) == A[i] || maxseg.prod(i, mid + 1) == A[i]) { lo = mid; } else { hi = mid; } } ll mn = A[i], mx = A[i]; for (int j = i + 1; j <= lo; j++) { mn = min(mn, A[j]); mx = max(mx, A[j]); ll cnt = raq[j]; ans += mn * mx * (1 + cnt); if (cnt > 0) raq.apply(j, j + 1, -1); } raq.apply(lo + 1, N, 1); } for (int i = 0; i < N; i++) ans += A[i] * A[i]; cout << ans.val() << endl; }