#include using namespace std; #include using namespace atcoder; using mint=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; return {mint(x.len)*f,x.len}; } int composition(int f,int g){ if(f==-1)return f; return g; } 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(N),seg_dec(N); mint ans=0,sum=0; for(int i=0;i=A[i]){ auto[x,l]=dec.top();dec.pop(); sum-=x*seg_inc.prod(l,decl).sum; decl=l; } dec.push({A[i],decl}); seg_dec.apply(decl,i,A[i]); sum+=A[i]*seg_inc.prod(decl,i).sum; seg_inc.set(i,{A[i],1}); seg_dec.set(i,{A[i],1}); sum+=mint(A[i])*A[i]; ans+=sum; } cout<