/** 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}; multiset> 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(); if (val == 998244353){ bad_points.erase(bad_points.find(pair(tar, now))); }else{ seg.apply(tar, now, F{mint(val).inv(), 0}); } max_st.pop_back(); now = tar; } if (a[i] == 998244353){ bad_points.insert(pair(now, i+1)); }else{ 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(); if (val == 998244353){ bad_points.erase(bad_points.find(pair(tar, now))); }else{ seg.apply(tar, now, F{mint(val).inv(), 0}); } min_st.pop_back(); now = tar; } if (a[i] == 998244353){ bad_points.insert(pair(now, i+1)); }else{ seg.apply(now, i+1, F{a[i], 0}); } min_st.push_back(a[i]); min_ind.push_back(i+1); } vector v = {0, i+1}; for(auto [l,r]: bad_points){ v.push_back(l); v.push_back(r); } sort(v.begin(), v.end()); vector imos((int)v.size()); for(auto [l,r]: bad_points){ imos[lower_bound(v.begin(), v.end(), l) - v.begin()] += 1; imos[lower_bound(v.begin(), v.end(), r) - v.begin()] -= 1; } rep(j,0,(int)v.size()-1){ imos[j+1] += imos[j]; } rep(j,0,(int)v.size()-1){ if (imos[j] == 0){ ans += seg.prod(v[j], v[j+1]).val; } } } cout << ans.val() << '\n'; }