#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum_large_array"
#include"segment_tree/sparse_lazy_segtree.hpp"
#include<atcoder/modint>
#include<bits/stdc++.h>usingZ=atcoder::modint998244353;usingS=Z;Sop(Sa,Sb){returna+b;}Se(){return0;}usingF=std::pair<Z,Z>;Smapping(Ff,Sx,intl,intr){auto[a,b]=f;returna*x+b*(r-l);}Fcomposition(Ff,Fg){auto[a,b]=f;auto[c,d]=g;return{a*c,a*d+b};}Fid(){return{1,0};}intmain(){std::cin.tie(0)->sync_with_stdio(0);intN,Q;std::cin>>N>>Q;sparse_lazy_segtree<int,S,op,e,F,mapping,composition,id>seg(N);while(Q--){intt,l,r;std::cin>>t>>l>>r;if(t==0){intb,c;std::cin>>b>>c;seg.apply(l,r,{b,c});}else{autoans=seg.prod(l,r);std::cout<<ans.val()<<"\n";}}}
#line 1 "test/segment_tree/sparse_lazy_segtree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum_large_array"
#line 1 "segment_tree/sparse_lazy_segtree.hpp"
#include<array>
#include<cassert>
#include<cstdint>
#include<vector>// https://suisen-cp.github.io/cp-library-cpp/library/datastructure/segment_tree/sparse_lazy_segment_tree.hpptemplate<typenameIndexType,// type of indextypenameS,// type of elementautoop,// S op(S a, S b)autoe,// S e()typenameF,// type of operator which acts on Sautomapping,// S mapping(F f, S x, IndexType l, IndexType r)autocomposition,// F composition(F f, F g)autoid,// F id()autoinit=[](IndexType,IndexType){returne();}// initializes product of segment [l, r)>structsparse_lazy_segtree{usingindex_type=IndexType;usingvalue_type=S;usingoperator_type=F;private:usingpool_index_type=uint32_t;structNode{pool_index_typech[2]{0,0};value_typedat;operator_typelaz;Node(constvalue_type&dat_):dat(dat_),laz(id()){}};staticinlinestd::vector<Node>pool{Node{e()}};staticpool_index_typenew_node(constvalue_type&dat){constpool_index_typeres=static_cast<pool_index_type>(pool.size());returnpool.emplace_back(dat),res;}public:sparse_lazy_segtree():sparse_lazy_segtree(0){}explicitsparse_lazy_segtree(IndexTypen_):n(n_),root(new_node(init(0,n))){}staticvoidreserve(intsiz){pool.reserve(siz);}value_typeget(index_typei)const{assert(0<=iandi<n);operator_typef=id();pool_index_typecur=root;for(std::array<index_type,2>lr{0,n};curandlr[1]-lr[0]>1;){index_typem=lr[0]+(lr[1]-lr[0])/2;boolb=i>=m;f=composition(f,pool[cur].laz);cur=pool[cur].ch[b],lr[notb]=m;}returnmapping(f,cur?pool[cur].dat:init(i,i+1),i,i+1);}template<typenameFun>voidapply_fun(index_typei,Fun&&fun){assert(0<=iandi<n);staticstd::vector<pool_index_type>path;pool_index_typecur=root;for(std::array<index_type,2>lr{0,n};lr[1]-lr[0]>1;){path.push_back(cur);index_typem=lr[0]+(lr[1]-lr[0])/2;boolb=i>=m;push(cur,lr[0],lr[1]);cur=pool[cur].ch[b],lr[notb]=m;}pool[cur].dat=fun(pool[cur].dat);while(path.size())update(path.back()),path.pop_back();}voidset(index_typei,constvalue_type&val){apply_fun(i,[&val](constvalue_type&){returnval;});}voidapply(index_typei,constoperator_type&f){apply_fun(i,[&f,i](constvalue_type&val){returnmapping(f,val,i,i+1);});}value_typeoperator()(index_typel,index_typer){assert(0<=landl<=randr<=n);if(l==r)returne();returnquery(root,l,r,0,n);}value_typeprod(index_typel,index_typer){return(*this)(l,r);}value_typeall_prod(){returnpool[root].dat;}voidapply(index_typel,index_typer,constoperator_type&f){assert(0<=landl<=randr<=n);if(l==r)return;apply(root,f,l,r,0,n);}voidapply_all(constoperator_type&f){apply_all(root,f,0,n);}private:index_typen;pool_index_typeroot;pool_index_typeget_or_create_child(pool_index_typenode,intindex,index_typetl,index_typetr){if(pool[node].ch[index])returnpool[node].ch[index];constpool_index_typech=new_node(init(tl,tr));returnpool[node].ch[index]=ch;}voidapply_all(pool_index_typenode,constoperator_type&f,index_typetl,index_typetr){pool[node].dat=mapping(f,pool[node].dat,tl,tr);pool[node].laz=composition(f,pool[node].laz);}voidpush(pool_index_typenode,index_typetl,index_typetr){constindex_typetm=tl+(tr-tl)/2;constoperator_typelaz=pool[node].laz;apply_all(get_or_create_child(node,0,tl,tm),laz,tl,tm);apply_all(get_or_create_child(node,1,tm,tr),laz,tm,tr);pool[node].laz=id();}voidupdate(pool_index_typenode){pool_index_typelch=pool[node].ch[0],rch=pool[node].ch[1];pool[node].dat=op(pool[lch].dat,pool[rch].dat);}value_typequery(pool_index_typenode,index_typeql,index_typeqr,index_typetl,index_typetr){if(tr<=qlorqr<=tl)returne();if(ql<=tlandtr<=qr)returnpool[node].dat;push(node,tl,tr);constindex_typetm=tl+(tr-tl)/2;returnop(query(pool[node].ch[0],ql,qr,tl,tm),query(pool[node].ch[1],ql,qr,tm,tr));}voidapply(pool_index_typenode,constoperator_type&f,index_typeql,index_typeqr,index_typetl,index_typetr){if(tr<=qlorqr<=tl)return;if(ql<=tlandtr<=qr)returnapply_all(node,f,tl,tr);constindex_typetm=tl+(tr-tl)/2;push(node,tl,tr);apply(pool[node].ch[0],f,ql,qr,tl,tm),apply(pool[node].ch[1],f,ql,qr,tm,tr);update(node);}};#line 4 "test/segment_tree/sparse_lazy_segtree.test.cpp"
#include<atcoder/modint>
#include<bits/stdc++.h>usingZ=atcoder::modint998244353;usingS=Z;Sop(Sa,Sb){returna+b;}Se(){return0;}usingF=std::pair<Z,Z>;Smapping(Ff,Sx,intl,intr){auto[a,b]=f;returna*x+b*(r-l);}Fcomposition(Ff,Fg){auto[a,b]=f;auto[c,d]=g;return{a*c,a*d+b};}Fid(){return{1,0};}intmain(){std::cin.tie(0)->sync_with_stdio(0);intN,Q;std::cin>>N>>Q;sparse_lazy_segtree<int,S,op,e,F,mapping,composition,id>seg(N);while(Q--){intt,l,r;std::cin>>t>>l>>r;if(t==0){intb,c;std::cin>>b>>c;seg.apply(l,r,{b,c});}else{autoans=seg.prod(l,r);std::cout<<ans.val()<<"\n";}}}