/** author: shobonvip created: 2024.10.25 22:03:11 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } struct S{ mint val; ll len; }; struct F{ mint b; mint c; }; S op(S x, S y){ return {x.val + y.val, x.len + y.len}; } S e(){ return {0, 0}; } S mapping(F f, S x){ return {f.b * x.val + f.c * x.len, x.len}; } F composition(F g, F f){ return {f.b * g.b, f.c * g.b + g.c}; } F id(){ return {1, 0}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector a(n); rep(i,0,n){ cin >> a[i]; } vector max_st = {(int)1e9+20}; vector max_ind = {0}; vector min_st = {-1}; vector min_ind = {0}; set> bad_points; lazy_segtree seg(vector(n,S{1,1})); mint ans = 0; rep(i,0,n){ { int now = i; while(a[i] >= max_st.back()) { int val = max_st.back(); max_ind.pop_back(); int tar = max_ind.back(); seg.apply(tar, now, F{mint(val).inv(), 0}); max_st.pop_back(); now = tar; } seg.apply(now, i+1, F{a[i], 0}); max_st.push_back(a[i]); max_ind.push_back(i+1); } { int now = i; while(a[i] <= min_st.back()) { int val = min_st.back(); min_ind.pop_back(); int tar = min_ind.back(); seg.apply(tar, now, F{mint(val).inv(), 0}); min_st.pop_back(); now = tar; } seg.apply(now, i+1, F{a[i], 0}); min_st.push_back(a[i]); min_ind.push_back(i+1); } ans += seg.prod(0, i+1).val; //cout << ans.val() << '\n'; } cout << ans.val() << '\n'; }