#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, static_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, dynamic_modint& a) {long long x; is >> x; a = x; return is;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} using mint = modint998244353; int main(){ int n; cin >> n; vector a(n); cin >> a; mint ans = 0; auto dfs = [&](auto self, int left, int right) -> void { if(right == left + 1){ ans += mint(a[left]) * a[left]; return; } int mid = (right + left) / 2; self(self, left, mid); self(self, mid, right); int ln = mid - left; int rn = right - mid; vector lmax(ln); vector lmin(ln); vector rmax(rn); vector rmin(rn); { int tmp = 0; rep(i, ln){ int idx = (mid - 1) - i; chmax(tmp, a[idx]); lmax[i] = tmp; } } { int tmp = 1001001001; rep(i, ln){ int idx = (mid - 1) - i; chmin(tmp, a[idx]); lmin[i] = tmp; } } { int tmp = 0; rep(i, rn){ int idx = mid + i; chmax(tmp, a[idx]); rmax[i] = tmp; } } { int tmp = 1001001001; rep(i, rn){ int idx = mid + i; chmin(tmp, a[idx]); rmin[i] = tmp; } } vector lmax_sum(ln + 1); vector rmax_sum(rn + 1); rep(li, ln) lmax_sum[li + 1] = lmax_sum[li] + lmax[li]; rep(ri, rn) rmax_sum[ri + 1] = rmax_sum[ri] + rmax[ri]; /* cout << lmax; cout << lmin; cout << rmax; cout << rmin; */ int li = 0; int ri = 0; mint plus = 0; auto right_up = [&](){ int idx = upper_bound(lmax.begin(), lmax.end(), rmax[ri]) - lmax.begin(); if(idx < li) plus += (mint)(rmax[ri] * idx + (lmax_sum[li] - lmax_sum[idx])) * rmin[ri]; else plus += (mint)(rmax[ri] * li) * rmin[ri]; ri++; }; auto left_up = [&](){ int idx = upper_bound(rmax.begin(), rmax.end(), lmax[li]) - rmax.begin(); if(idx < ri) plus += (mint)(lmax[li] * idx + (rmax_sum[ri] - rmax_sum[idx])) * lmin[li]; else plus += (mint)(lmax[li] * ri) * lmin[li]; li++; }; while(li < ln or ri < rn){ if(li == ln){ right_up(); continue; } if(ri == rn){ left_up(); continue; } if(lmin[li] < rmin[ri]){ right_up(); }else{ left_up(); } } ans += plus; // cout << plus << "\n"; }; dfs(dfs, 0, n); cout << ans << "\n"; return 0; }