#line 1 "a.cpp" #include #include #include using namespace std; #line 3 "/home/kotatsugame/library/math/modint.cpp" #include template struct modint{ unsigned int x; constexpr modint()noexcept:x(){} template constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){} constexpr unsigned int val()const noexcept{return x;} constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;} constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;} constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;} constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;} constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;} constexpr modint&operator-=(const modint&a)noexcept{if(x>=1)if(n&1)r*=x; return r; } constexpr modint inv()const noexcept { int s=x,t=m,x=1,u=0; while(t) { int k=s/t; s-=k*t; swap(s,t); x-=k*u; swap(x,u); } return modint(x); } friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;} friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;} friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;} friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;} friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;} friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;} friend ostream&operator<<(ostream&os,const modint&a){return os<>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;} }; #line 2 "/home/kotatsugame/library/datastructure/segtree.cpp" #include #include template struct segtree{ using F=function; const F calcfn,updatefn; int n; T defvalue; vectordat; segtree(int n_=0,T defvalue_=numeric_limits::max(), const F calcfn_=[](T a,T b){return a&v) { for(int i=0;i=0;i--)dat[i]=calcfn(dat[i*2+1],dat[i*2+2]); } void update(int i,T a) { i+=n-1; dat[i]=updatefn(dat[i],a); while(i>0) { i=(i-1)/2; dat[i]=calcfn(dat[2*i+1],dat[2*i+2]); } } T query(int a,int b)//[a,b) { int L=(a<0?0:a>n?n:a)+n-1; int R=(b<0?0:b>n?n:b)+n-1; T lret=defvalue,rret=defvalue; for(;L>=1,R>>=1) { if(!(L&1))lret=calcfn(lret,dat[L]); if(!(R&1))rret=calcfn(dat[--R],rret); } return calcfn(lret,rret); } }; #line 7 "a.cpp" using mint=modint<998244353>; int N; main() { cin>>N; vector >A(N); for(int i=0;i>A[i].first; A[i].second=i; } sort(A.begin(),A.end()); segtreeV1(N,0,[](mint a,mint b){return a+b;},[](mint a,mint b){return b;}); segtreeV2(N,0,[](mint a,mint b){return a+b;},[](mint a,mint b){return b;}); segtreeC1(N,0,[](mint a,mint b){return a+b;},[](mint a,mint b){return b;}); segtreeC2(N,0,[](mint a,mint b){return a+b;},[](mint a,mint b){return b;}); mint ans; for(int i=N;i--;) { int id=A[i].second; mint a=A[i].first; ans+=V2.query(0,id)+C2.query(0,id)*a; V2.update(id,V1.query(0,id)+C1.query(0,id)*a); C2.update(id,C1.query(0,id)); V1.update(id,a); C1.update(id,1); } cout<