#include<bits/stdc++.h>usingnamespacestd;// Template starts
#include<codeforces/modint.hpp>// Template ends
constexprintmd=998244353;usingmint=Modular<std::integral_constant<decay<decltype(md)>::type,md>>;#define endl "\n"
intmaxn=(int)1e6+1;vector<mint>fac;constmintp=mint(1)/2;voidprecompute_factorial(){fac.resize(maxn+1);fac[0]=1;for(inti=1;i<maxn;i++){fac[i]=i*fac[i-1];}}mintcomb(intn,intr){if(r<0||r>n){return0;}returnfac[n]/(fac[n-r]*fac[r]);}voidsolve(){intn,q;cin>>n>>q;vector<int>a(n),b(n);for(inti=0;i<n;i++){cin>>a[i];}for(inti=0;i<n;i++){cin>>b[i];}intta=accumulate(a.begin(),a.end(),0);inttb=accumulate(b.begin(),b.end(),0);for(intzz=0;zz<q;zz++){intl,r;cin>>l>>r;l--,r--;intla=0,lb=0;for(inti=l;i<=r;i++){la+=a[i],lb+=b[i];}intra=ta-la;intrb=tb-lb;// dp[i] is the number of ways to have value = i in the rb silver pile.
vector<mint>dp(rb+1);for(inti=0;i<=rb;i++){dp[i]=comb(rb,i)*power(p,rb)+(i?dp[i-1]:0);}mintans=0;for(intx=0;x<=lb;x++){mintlhs=comb(lb,x)*power(p,lb);intlim=min(la-ra+x-1,rb);if(lim<0){continue;}mintrhs=dp[lim];ans+=lhs*rhs;}cout<<ans<<" ";}cout<<endl;}intmain(){precompute_factorial();solve();return0;}
#include<bits/stdc++.h>usingnamespacestd;// Template starts
// ...
template<typenameT>Tinverse(Ta,Tm){Tu=0,v=1;while(a!=0){Tt=m/a;m-=t*a;swap(a,m);u-=t*v;swap(u,v);}assert(m==1);returnu;}template<typenameT>classModular{public:usingType=typenamedecay<decltype(T::value)>::type;constexprModular():value(){}template<typenameU>Modular(constU&x){value=normalize(x);}template<typenameU>staticTypenormalize(constU&x){Typev;if(-mod()<=x&&x<mod())v=static_cast<Type>(x);elsev=static_cast<Type>(x%mod());if(v<0)v+=mod();returnv;}constType&operator()()const{returnvalue;}template<typenameU>explicitoperatorU()const{returnstatic_cast<U>(value);}constexprstaticTypemod(){returnT::value;}Modular&operator+=(constModular&other){if((value+=other.value)>=mod())value-=mod();return*this;}Modular&operator-=(constModular&other){if((value-=other.value)<0)value+=mod();return*this;}template<typenameU>Modular&operator+=(constU&other){return*this+=Modular(other);}template<typenameU>Modular&operator-=(constU&other){return*this-=Modular(other);}Modular&operator++(){return*this+=1;}Modular&operator--(){return*this-=1;}Modularoperator++(int){Modularresult(*this);*this+=1;returnresult;}Modularoperator--(int){Modularresult(*this);*this-=1;returnresult;}Modularoperator-()const{returnModular(-value);}template<typenameU=T>typenameenable_if<is_same<typenameModular<U>::Type,int>::value,Modular>::type&operator*=(constModular&rhs){value=normalize(static_cast<int64_t>(value)*static_cast<int64_t>(rhs.value));return*this;}template<typenameU=T>typenameenable_if<is_same<typenameModular<U>::Type,longlong>::value,Modular>::type&operator*=(constModular&rhs){longlongq=static_cast<longlong>(static_cast<longdouble>(value)*rhs.value/mod());value=normalize(value*rhs.value-q*mod());return*this;}template<typenameU=T>typenameenable_if<!is_integral<typenameModular<U>::Type>::value,Modular>::type&operator*=(constModular&rhs){value=normalize(value*rhs.value);return*this;}Modular&operator/=(constModular&other){return*this*=Modular(inverse(other.value,mod()));}friendconstType&abs(constModular&x){returnx.value;}template<typenameU>friendbooloperator==(constModular<U>&lhs,constModular<U>&rhs);template<typenameU>friendbooloperator<(constModular<U>&lhs,constModular<U>&rhs);template<typenameV,typenameU>friendV&operator>>(V&stream,Modular<U>&number);private:Typevalue;};template<typenameT>booloperator==(constModular<T>&lhs,constModular<T>&rhs){returnlhs.value==rhs.value;}template<typenameT,typenameU>booloperator==(constModular<T>&lhs,Urhs){returnlhs==Modular<T>(rhs);}template<typenameT,typenameU>booloperator==(Ulhs,constModular<T>&rhs){returnModular<T>(lhs)==rhs;}template<typenameT>booloperator!=(constModular<T>&lhs,constModular<T>&rhs){return!(lhs==rhs);}template<typenameT,typenameU>booloperator!=(constModular<T>&lhs,Urhs){return!(lhs==rhs);}template<typenameT,typenameU>booloperator!=(Ulhs,constModular<T>&rhs){return!(lhs==rhs);}template<typenameT>booloperator<(constModular<T>&lhs,constModular<T>&rhs){returnlhs.value<rhs.value;}template<typenameT>Modular<T>operator+(constModular<T>&lhs,constModular<T>&rhs){returnModular<T>(lhs)+=rhs;}template<typenameT,typenameU>Modular<T>operator+(constModular<T>&lhs,Urhs){returnModular<T>(lhs)+=rhs;}template<typenameT,typenameU>Modular<T>operator+(Ulhs,constModular<T>&rhs){returnModular<T>(lhs)+=rhs;}template<typenameT>Modular<T>operator-(constModular<T>&lhs,constModular<T>&rhs){returnModular<T>(lhs)-=rhs;}template<typenameT,typenameU>Modular<T>operator-(constModular<T>&lhs,Urhs){returnModular<T>(lhs)-=rhs;}template<typenameT,typenameU>Modular<T>operator-(Ulhs,constModular<T>&rhs){returnModular<T>(lhs)-=rhs;}template<typenameT>Modular<T>operator*(constModular<T>&lhs,constModular<T>&rhs){returnModular<T>(lhs)*=rhs;}template<typenameT,typenameU>Modular<T>operator*(constModular<T>&lhs,Urhs){returnModular<T>(lhs)*=rhs;}template<typenameT,typenameU>Modular<T>operator*(Ulhs,constModular<T>&rhs){returnModular<T>(lhs)*=rhs;}template<typenameT>Modular<T>operator/(constModular<T>&lhs,constModular<T>&rhs){returnModular<T>(lhs)/=rhs;}template<typenameT,typenameU>Modular<T>operator/(constModular<T>&lhs,Urhs){returnModular<T>(lhs)/=rhs;}template<typenameT,typenameU>Modular<T>operator/(Ulhs,constModular<T>&rhs){returnModular<T>(lhs)/=rhs;}template<typenameT,typenameU>Modular<T>power(constModular<T>&a,constU&b){assert(b>=0);Modular<T>x=a,res=1;Up=b;while(p>0){if(p&1)res*=x;x*=x;p>>=1;}returnres;}template<typenameT>boolIsZero(constModular<T>&number){returnnumber()==0;}template<typenameT>stringto_string(constModular<T>&number){returnto_string(number());}// U == std::ostream? but done this way because of fastoutput
template<typenameU,typenameT>U&operator<<(U&stream,constModular<T>&number){returnstream<<number();}// U == std::istream? but done this way because of fastinput
template<typenameU,typenameT>U&operator>>(U&stream,Modular<T>&number){typenamecommon_type<typenameModular<T>::Type,longlong>::typex;stream>>x;number.value=Modular<T>::normalize(x);returnstream;}// ...
// Template ends
constexprintmd=998244353;usingmint=Modular<std::integral_constant<decay<decltype(md)>::type,md>>;#define endl "\n"
intmaxn=(int)1e6+1;vector<mint>fac;constmintp=mint(1)/2;voidprecompute_factorial(){fac.resize(maxn+1);fac[0]=1;for(inti=1;i<maxn;i++){fac[i]=i*fac[i-1];}}mintcomb(intn,intr){if(r<0||r>n){return0;}returnfac[n]/(fac[n-r]*fac[r]);}voidsolve(){intn,q;cin>>n>>q;vector<int>a(n),b(n);for(inti=0;i<n;i++){cin>>a[i];}for(inti=0;i<n;i++){cin>>b[i];}intta=accumulate(a.begin(),a.end(),0);inttb=accumulate(b.begin(),b.end(),0);for(intzz=0;zz<q;zz++){intl,r;cin>>l>>r;l--,r--;intla=0,lb=0;for(inti=l;i<=r;i++){la+=a[i],lb+=b[i];}intra=ta-la;intrb=tb-lb;mintans=0;for(intx=0;x<=lb;x++){mintlhs=comb(lb,x)*power(p,lb);for(inty=0;y<la-ra+x;y++){mintrhs=comb(rb,y)*power(p,rb);ans+=lhs*rhs;}}cout<<ans<<" ";}cout<<endl;}intmain(){precompute_factorial();solve();return0;}