#include using namespace std; #include using namespace atcoder; using mint=atcoder::modint998244353; struct S{ mint sum; int len; }; S op(S x,S y){ return {x.sum+y.sum,x.len+y.len}; } S e(){ return {0,0}; } S mapping(int f,S x){ if(f==-1)return x; else return {mint(f)*x.len,x.len}; } int composition(int f,int g){ if(f==-1)return g; else return f; } int id(){ return -1; } int main(){ int n;cin>>n; vector a(n);for(auto&e:a)cin>>e; stack> inc,dec; lazy_segtree seg_inc(vector(n,{0,1})); lazy_segtree seg_dec(vector(n,{0,1})); mint ans=0,sum=0; for(int i=0;i(inc.top())<=a[i]){ auto[v,l,r]=inc.top(); incl=l; sum-=mint(v)*seg_dec.prod(l,r).sum; inc.pop(); } inc.push({a[i],incl,i+1}); sum+=mint(a[i])*seg_dec.prod(incl,i+1).sum; seg_inc.apply(incl,i+1,a[i]); int decl=i; while(dec.size()&&get<0>(dec.top())>=a[i]){ auto[v,l,r]=dec.top(); decl=l; sum-=mint(v)*seg_inc.prod(l,r).sum; dec.pop(); } dec.push({a[i],decl,i+1}); sum+=mint(a[i])*seg_inc.prod(decl,i+1).sum; seg_dec.apply(decl,i+1,a[i]); ans+=sum; } cout<