#define PROBLEM "https://judge.yosupo.jp/problem/point_set_tree_path_composite_sum"
#include"../../../utilities/fast_io.hpp"
#include<vector>#include"../../../ds/dynamic_tree/rake_compress_link_cut_tree.hpp"
#include"../../../math/modint.hpp"usingmint=m1une::math::modint998244353;template<classT>structPointSetTreePathCompositeSum{structPoint{Ts;Tx;staticPointid(){returnPoint{T(0),T(0)};}Pointinv()const{returnPoint{T(0)-s,T(0)-x};}};structPath{Ta;Tb;Ts;Tx;};structVertexValue{Tx;};structEdgeValue{Ta;Tb;};staticPathmake_vertex_path(constPoint&d,constVertexValue&vertex){returnPath{T(1),T(0),d.s+vertex.x,d.x+T(1)};}staticPathmake_edge_path(constPoint&d,constEdgeValue&edge){returnPath{edge.a,edge.b,d.s*edge.a+d.x*edge.b,d.x};}staticPointto_point(constPath&path){returnPoint{path.s,path.x};}staticPointrake(constPoint&a,constPoint&b){returnPoint{a.s+b.s,a.x+b.x};}staticPathcompress(constPath&parent_side,constPath&child_side){returnPath{parent_side.a*child_side.a,parent_side.a*child_side.b+parent_side.b,parent_side.s+parent_side.a*child_side.s+parent_side.b*child_side.x,parent_side.x+child_side.x};}};intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingTreeDP=PointSetTreePathCompositeSum<mint>;usingVertexValue=typenameTreeDP::VertexValue;usingEdgeValue=typenameTreeDP::EdgeValue;usingLCT=m1une::ds::RakeCompressLinkCutTree<TreeDP>;intn,q;fast_input>>n>>q;LCTlct;std::vector<int>vertex_id(n);for(inti=0;i<n;i++){minta;fast_input>>a;vertex_id[i]=lct.add_vertex(VertexValue{a});}std::vector<int>edge_id(n-1);for(inti=0;i+1<n;i++){intu,v;mintb,c;fast_input>>u>>v>>b>>c;edge_id[i]=lct.add_edge(vertex_id[u],vertex_id[v],EdgeValue{b,c});}for(inti=0;i<q;i++){inttype;fast_input>>type;introot;if(type==0){intw;mintx;fast_input>>w>>x>>root;lct.set_vertex(vertex_id[w],VertexValue{x});}else{inte;minty,z;fast_input>>e>>y>>z>>root;lct.set_edge(edge_id[e],EdgeValue{y,z});}fast_output<<lct.component_prod(vertex_id[root]).s<<'\n';}}
#line 1 "verify/ds/dynamic_tree/yosupo_point_set_tree_path_composite_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_set_tree_path_composite_sum"
#line 1 "utilities/fast_io.hpp"
#include<algorithm>
#include<array>
#include<cerrno>
#include<charconv>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstdint>
#include<cstring>
#include<iterator>
#include<string>
#include<sys/stat.h>
#include<type_traits>
#include<utility>
#include<unistd.h>
#include<vector>namespacem1une{namespaceutilities{structFastOutput;namespaceinternal{// Shared with the convenience helpers in template.hpp.inlineFastOutput*standard_output_instance=nullptr;// Detect std::begin(x), std::end(x).template<classT,class=void>structis_range:std::false_type{};template<classT>structis_range<T,std::void_t<decltype(std::begin(std::declval<T&>())),decltype(std::end(std::declval<T&>()))>>:std::true_type{};template<classT>inlineconstexprboolis_range_v=is_range<T>::value;template<classT>usingrange_reference_t=decltype(*std::begin(std::declval<T&>()));template<classT>usingrange_value_t=std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;template<classT,class=void>structrange_stored_value{usingtype=range_value_t<T>;};template<classT>structrange_stored_value<T,std::void_t<typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type>>{usingtype=typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type;};template<classT>usingrange_stored_value_t=typenamerange_stored_value<T>::type;// Treat strings and C strings as scalar output objects, not as ranges.template<classT>structis_char_array:std::false_type{};template<classT,std::size_tN>structis_char_array<T[N]>:std::bool_constant<std::is_same_v<std::remove_cv_t<T>,char>>{};template<classT>structis_string_like:std::bool_constant<std::is_same_v<std::decay_t<T>,std::string>||std::is_same_v<std::decay_t<T>,constchar*>||std::is_same_v<std::decay_t<T>,char*>||is_char_array<std::remove_reference_t<T>>::value>{};template<classT>inlineconstexprboolis_string_like_v=is_string_like<T>::value;// ModInt-like type: x.val() is printable, and x can be assigned from long long.template<classT,class=void>structhas_val_method:std::false_type{};template<classT>structhas_val_method<T,std::void_t<decltype(std::declval<constT&>().val())>>:std::true_type{};template<classT>inlineconstexprboolhas_val_method_v=has_val_method<T>::value;template<classT,class=void>structhas_static_mod_raw:std::false_type{};template<classT>structhas_static_mod_raw<T,std::void_t<decltype(T::mod()),decltype(T::raw(std::declval<uint32_t>()))>>:std::true_type{};template<classT>inlineconstexprboolhas_static_mod_raw_v=has_static_mod_raw<T>::value;// libstdc++ before GCC 16 does not classify __int128 as an integral type in// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent// of that implementation detail.template<classT>inlineconstexprboolis_integral_v=std::is_integral_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>||std::is_same_v<std::remove_cv_t<T>,__uint128_t>;template<classT>inlineconstexprboolis_signed_v=std::is_signed_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>;template<classT>structmake_unsigned{usingtype=std::make_unsigned_t<T>;};template<>structmake_unsigned<__int128_t>{usingtype=__uint128_t;};template<>structmake_unsigned<__uint128_t>{usingtype=__uint128_t;};template<classT>usingmake_unsigned_t=typenamemake_unsigned<std::remove_cv_t<T>>::type;}// namespace internalstructFastInput{staticconstexprintbuffer_size=1<<20;private:std::FILE*_stream;char_buffer[buffer_size];int_position;int_length;int_file_descriptor;bool_streaming;boolrefill(){_position=0;if(_streaming){ssize_tlength;do{length=::read(_file_descriptor,_buffer,buffer_size);}while(length<0&&errno==EINTR);if(length<=0){_length=0;returnfalse;}_length=int(length);}else{_length=int(std::fread(_buffer,1,buffer_size,_stream));}return_length!=0;}template<classT>boolread_integer_from_stream(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'){negative=true;c=read_char_raw();}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){result=negative?result*10-(c-'0'):result*10+(c-'0');c=read_char_raw();}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){result=result*10+T(c-'0');c=read_char_raw();}value=negative?T(0)-result:result;}returntrue;}boolprepare_number(){if(_length-_position>=64)returntrue;constintremaining=_length-_position;if(remaining>0)std::memmove(_buffer,_buffer+_position,remaining);constintadded=int(std::fread(_buffer+remaining,1,buffer_size-remaining,_stream));_position=0;_length=remaining+added;if(_length<buffer_size)_buffer[_length]='\0';return_length!=0;}public:explicitFastInput(std::FILE*stream=stdin):_stream(stream),_position(0),_length(0),_file_descriptor(::fileno(stream)),_streaming([&]{structstatstatus;return_file_descriptor>=0&&::fstat(_file_descriptor,&status)==0&&!S_ISREG(status.st_mode);}()){}FastInput(constFastInput&)=delete;FastInput&operator=(constFastInput&)=delete;intread_char_raw(){if(_position==_length&&!refill())returnEOF;return_buffer[_position++];}boolskip_spaces(){intc=read_char_raw();while(c!=EOF&&c<=' ')c=read_char_raw();if(c==EOF)returnfalse;--_position;returntrue;}boolread(char&value){if(!skip_spaces())returnfalse;value=char(read_char_raw());returntrue;}boolread(std::string&value){if(!skip_spaces())returnfalse;value.clear();while(true){constintbegin=_position;while(_position<_length&&static_cast<unsignedchar>(_buffer[_position])>' '){++_position;}value.append(_buffer+begin,_position-begin);if(_position<_length){++_position;returntrue;}if(!refill())returntrue;}}boolread(bool&value){intx;if(!read(x))returnfalse;value=x!=0;returntrue;}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>,bool>read(T&value){if(_streaming)returnread_integer_from_stream(value);if(!prepare_number())returnfalse;intc=static_cast<unsignedchar>(_buffer[_position++]);while(c<=' ')c=static_cast<unsignedchar>(_buffer[_position++]);boolnegative=false;if(c=='-'){negative=true;c=static_cast<unsignedchar>(_buffer[_position++]);}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){constintfirst=c-'0';constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=negative?result*100-(first*10+second):result*100+(first*10+second);++_position;}else{result=negative?result*10-first:result*10+first;}c=static_cast<unsignedchar>(_buffer[_position++]);}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){constunsignedfirst=unsigned(c-'0');constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=result*100+T(first*10+unsigned(second));++_position;}else{result=result*10+T(first);}c=static_cast<unsignedchar>(_buffer[_position++]);}value=negative?T(0)-result:result;}if(_position>_length)_position=_length;returntrue;}template<classT>std::enable_if_t<std::is_floating_point_v<T>,bool>read(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'||c=='+'){negative=c=='-';c=read_char_raw();}longdoubleresult=0;while('0'<=c&&c<='9'){result=result*10+(c-'0');c=read_char_raw();}if(c=='.'){longdoubleplace=0.1L;c=read_char_raw();while('0'<=c&&c<='9'){result+=(c-'0')*place;place*=0.1L;c=read_char_raw();}}if(c=='e'||c=='E'){c=read_char_raw();boolexponent_negative=false;if(c=='-'||c=='+'){exponent_negative=c=='-';c=read_char_raw();}intexponent=0;while('0'<=c&&c<='9'){exponent=exponent*10+(c-'0');c=read_char_raw();}longdoublescale=1;longdoublepower=10;while(exponent>0){if(exponent&1)scale*=power;power*=power;exponent>>=1;}result=exponent_negative?result/scale:result*scale;}value=static_cast<T>(negative?-result:result);returntrue;}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>,bool>read(T&value){longlongx;if(!read(x))returnfalse;ifconstexpr(internal::has_static_mod_raw_v<T>){if(x>=0&&uint64_t(x)<uint64_t(T::mod())){value=T::raw(uint32_t(x));}else{value=T(x);}}else{value=T(x);}returntrue;}template<classFirst,classSecond>boolread(std::pair<First,Second>&value){if(!read(value.first))returnfalse;returnread(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>,bool>read(Range&range){usingStoredValue=internal::range_stored_value_t<Range>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;for(auto&&value:range){ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){boolx;if(!read(x))returnfalse;value=x;}else{if(!read(value))returnfalse;}}returntrue;}template<classFirst,classSecond,class...Rest>boolread(First&first,Second&second,Rest&...rest){if(!read(first))returnfalse;returnread(second,rest...);}template<classT>FastInput&operator>>(T&value){if(!read(value))std::abort();return*this;}};structFastOutput{staticconstexprintbuffer_size=1<<20;private:inlinestaticconstautodigit_quads=[]{std::array<char,40000>result{};for(inti=0;i<10000;i++){intvalue=i;for(intj=3;j>=0;j--){result[4*i+j]=char('0'+value%10);value/=10;}}returnresult;}();std::FILE*_stream;char_buffer[buffer_size];int_position;int_precision;std::chars_format_float_format;char_range_separator;std::string*_capture=nullptr;template<classT>std::stringformat_cell(constT&value){std::stringresult;structCaptureGuard{std::string*⌖std::string*previous;~CaptureGuard(){target=previous;}}guard{_capture,_capture};_capture=&result;write(value);returnresult;}template<classMatrix>voidwrite_aligned_matrix(constMatrix&matrix){std::vector<std::vector<std::string>>rows;std::vector<std::size_t>widths;for(constauto&row:matrix){auto&cells=rows.emplace_back();std::size_tcolumn=0;for(constauto&value:row){cells.push_back(format_cell(value));if(column==widths.size())widths.push_back(0);widths[column]=std::max(widths[column],cells.back().size());++column;}}boolfirst=true;for(constauto&row:rows){if(!first)write_char('\n');first=false;for(std::size_tcolumn=0;column<row.size();++column){if(column!=0)write_char(_range_separator);for(std::size_tpadding=row[column].size();padding<widths[column];++padding){write_char(' ');}write(row[column]);}}}public:explicitFastOutput(std::FILE*stream=stdout):_stream(stream),_position(0),_precision(6),_float_format(std::chars_format::general),_range_separator(' '){if(_stream==stdout&&internal::standard_output_instance==nullptr){internal::standard_output_instance=this;}}FastOutput(constFastOutput&)=delete;FastOutput&operator=(constFastOutput&)=delete;~FastOutput(){flush();if(internal::standard_output_instance==this){internal::standard_output_instance=nullptr;}}voidflush(){if(_position!=0){std::fwrite(_buffer,1,_position,_stream);_position=0;}std::fflush(_stream);}voidwrite_char(charc){if(_capture!=nullptr){_capture->push_back(c);return;}if(_position==buffer_size)flush();_buffer[_position++]=c;}voidwrite(constchar*s){while(*s!='\0')write_char(*s++);}voidwrite(conststd::string&s){if(_capture!=nullptr){_capture->append(s);return;}std::size_tposition=0;while(position<s.size()){if(_position==buffer_size)flush();conststd::size_tcopied=std::min<std::size_t>(buffer_size-_position,s.size()-position);std::memcpy(_buffer+_position,s.data()+position,copied);_position+=int(copied);position+=copied;}}voidwrite(charc){write_char(c);}voidwrite(boolvalue){write_char(value?'1':'0');}template<classT>std::enable_if_t<std::is_floating_point_v<T>>write(Tvalue){chardigits[128];auto[end,error]=std::to_chars(digits,digits+sizeof(digits),value,_float_format,_precision);if(error!=std::errc())std::abort();for(constchar*pointer=digits;pointer!=end;pointer++){write_char(*pointer);}}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>>write(Tvalue){usingRaw=std::remove_cv_t<T>;usingUnsigned=internal::make_unsigned_t<Raw>;Unsignedmagnitude;ifconstexpr(internal::is_signed_v<Raw>){if(value<0){write_char('-');magnitude=Unsigned(0)-Unsigned(value);}else{magnitude=Unsigned(value);}}else{magnitude=value;}if(magnitude==0){write_char('0');return;}unsignedchunks[16];intcount=0;while(magnitude>=10000){constUnsignedquotient=magnitude/10000;chunks[count++]=unsigned(magnitude-quotient*10000);magnitude=quotient;}if(_capture==nullptr&&_position>buffer_size-64)flush();charcaptured[64];char*constbegin=_capture!=nullptr?captured:_buffer+_position;char*destination=begin;constunsignedleading=unsigned(magnitude);constchar*first=digit_quads.data()+4*leading;intskip=leading<10?3:leading<100?2:leading<1000?1:0;for(;skip<4;skip++)*destination++=first[skip];while(count--){constchar*digits=digit_quads.data()+4*chunks[count];std::memcpy(destination,digits,4);destination+=4;}if(_capture!=nullptr){_capture->append(begin,destination-begin);}else{_position+=int(destination-begin);}}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>>write(constT&value){write(value.val());}template<classFirst,classSecond>voidwrite(conststd::pair<First,Second>&value){write(value.first);write_char(' ');write(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>>write(constRange&range){usingStoredValue=internal::range_stored_value_t<constRange>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;boolfirst=true;for(constauto&value:range){if(!first)write_char(nested?'\n':_range_separator);first=false;ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){write(static_cast<bool>(value));}else{write(value);}}}template<classFirst,class...Rest>voidprint(constFirst&first,constRest&...rest){write(first);((write_char(' '),write(rest)),...);}voidprintln(){write_char('\n');}voidset_precision(intprecision){_precision=precision;}voidset_fixed(intprecision=6){_float_format=std::chars_format::fixed;_precision=precision;}voidset_general(intprecision=6){_float_format=std::chars_format::general;_precision=precision;}voidset_range_separator(charseparator){_range_separator=separator;}template<classMatrix>voidwrite_aligned(constMatrix&matrix){usingRow=internal::range_stored_value_t<constMatrix>;usingCell=internal::range_stored_value_t<constRow>;static_assert(internal::is_range_v<Row>&&!internal::is_string_like_v<Row>,"write_aligned requires a two-dimensional range");static_assert(!internal::is_range_v<Cell>||internal::is_string_like_v<Cell>,"write_aligned requires scalar cells");write_aligned_matrix(matrix);}template<classMatrix>voidprintln_aligned(constMatrix&matrix){write_aligned(matrix);write_char('\n');}template<class...Args>voidprintln(constArgs&...args){print(args...);write_char('\n');}template<classT>FastOutput&operator<<(constT&value){write(value);return*this;}};}// namespace utilities}// namespace m1une#line 5 "verify/ds/dynamic_tree/yosupo_point_set_tree_path_composite_sum.test.cpp"
#line 1 "ds/dynamic_tree/rake_compress_link_cut_tree.hpp"
#include<cassert>
#line 6 "ds/dynamic_tree/rake_compress_link_cut_tree.hpp"
#include<variant>
#line 8 "ds/dynamic_tree/rake_compress_link_cut_tree.hpp"
namespacem1une{namespaceds{// Maintains a dynamic forest whose tree DP uses two different aggregate types.// Point is a commutative group for virtual children, while Path is an ordered// preferred-path cluster and does not need an inverse.template<classTreeDPInfo>structRakeCompressLinkCutTree{usingPoint=typenameTreeDPInfo::Point;usingPath=typenameTreeDPInfo::Path;usingVertexValue=typenameTreeDPInfo::VertexValue;usingEdgeValue=typenameTreeDPInfo::EdgeValue;usingVertexId=int;usingEdgeId=int;private:structNode{intleft=-1;intright=-1;intparent=-1;boolrev=false;std::variant<VertexValue,EdgeValue>value;Pointvirtual_prod;Pathprod;Pathrev_prod;explicitNode(constVertexValue&vertex_value):value(std::in_place_index<0>,vertex_value),virtual_prod(Point::id()),prod(TreeDPInfo::make_vertex_path(virtual_prod,vertex_value)),rev_prod(prod){}explicitNode(std::in_place_index_t<1>,constEdgeValue&edge_value):value(std::in_place_index<1>,edge_value),virtual_prod(Point::id()),prod(TreeDPInfo::make_edge_path(virtual_prod,edge_value)),rev_prod(prod){}};structOriginalEdge{VertexIdu=-1;VertexIdv=-1;intnode=-1;boolalive=false;};std::vector<Node>_nodes;std::vector<int>_vertex_nodes;std::vector<OriginalEdge>_edges;std::vector<int>_path_buffer;boolis_splay_root(intnode)const{intparent=_nodes[node].parent;returnparent==-1||(_nodes[parent].left!=node&&_nodes[parent].right!=node);}voidupdate(intnode){Node&x=_nodes[node];Pathself=x.value.index()==0?TreeDPInfo::make_vertex_path(x.virtual_prod,std::get<0>(x.value)):TreeDPInfo::make_edge_path(x.virtual_prod,std::get<1>(x.value));x.prod=self;x.rev_prod=self;if(x.left!=-1){x.prod=TreeDPInfo::compress(_nodes[x.left].prod,x.prod);x.rev_prod=TreeDPInfo::compress(x.rev_prod,_nodes[x.left].rev_prod);}if(x.right!=-1){x.prod=TreeDPInfo::compress(x.prod,_nodes[x.right].prod);x.rev_prod=TreeDPInfo::compress(_nodes[x.right].rev_prod,x.rev_prod);}}voidadd_virtual_child(intnode,intchild){if(child==-1)return;Pointcontribution=TreeDPInfo::to_point(_nodes[child].prod);_nodes[node].virtual_prod=TreeDPInfo::rake(_nodes[node].virtual_prod,contribution);}voidremove_virtual_child(intnode,intchild){if(child==-1)return;Pointcontribution=TreeDPInfo::to_point(_nodes[child].prod);_nodes[node].virtual_prod=TreeDPInfo::rake(_nodes[node].virtual_prod,contribution.inv());}voidapply_reverse(intnode){if(node==-1)return;Node&x=_nodes[node];std::swap(x.left,x.right);std::swap(x.prod,x.rev_prod);x.rev=!x.rev;}voidpush(intnode){if(node==-1||!_nodes[node].rev)return;apply_reverse(_nodes[node].left);apply_reverse(_nodes[node].right);_nodes[node].rev=false;}voidpush_to(intnode){_path_buffer.clear();intcur=node;_path_buffer.push_back(cur);while(!is_splay_root(cur)){cur=_nodes[cur].parent;_path_buffer.push_back(cur);}for(inti=int(_path_buffer.size())-1;i>=0;i--)push(_path_buffer[i]);}voidrotate(intnode){intparent=_nodes[node].parent;intgrand=_nodes[parent].parent;boolis_right=_nodes[parent].right==node;intmiddle=is_right?_nodes[node].left:_nodes[node].right;if(!is_splay_root(parent)){if(_nodes[grand].left==parent){_nodes[grand].left=node;}else{_nodes[grand].right=node;}}_nodes[node].parent=grand;if(is_right){_nodes[node].left=parent;_nodes[parent].right=middle;}else{_nodes[node].right=parent;_nodes[parent].left=middle;}if(middle!=-1)_nodes[middle].parent=parent;_nodes[parent].parent=node;update(parent);update(node);}voidsplay(intnode){push_to(node);while(!is_splay_root(node)){intparent=_nodes[node].parent;intgrand=_nodes[parent].parent;if(!is_splay_root(parent)){boolzig_zig=(_nodes[parent].left==node)==(_nodes[grand].left==parent);rotate(zig_zig?parent:node);}rotate(node);}}intaccess(intnode){intlast=-1;for(intcur=node;cur!=-1;cur=_nodes[cur].parent){splay(cur);add_virtual_child(cur,_nodes[cur].right);remove_virtual_child(cur,last);_nodes[cur].right=last;if(last!=-1)_nodes[last].parent=cur;update(cur);last=cur;}splay(node);returnlast;}voidcheck_node(intnode)const{assert(0<=node&&node<int(_nodes.size()));}voidcheck_vertex(VertexIdvertex)const{assert(0<=vertex&&vertex<int(_vertex_nodes.size()));}voidcheck_edge(EdgeIdedge_id)const{assert(0<=edge_id&&edge_id<int(_edges.size()));}intvertex_node(VertexIdvertex)const{check_vertex(vertex);return_vertex_nodes[vertex];}intadd_edge_node(constEdgeValue&edge_value){_nodes.emplace_back(std::in_place_index<1>,edge_value);returnint(_nodes.size())-1;}voidset_vertex_node_value(intnode,constVertexValue&vertex_value){check_node(node);access(node);_nodes[node].value.templateemplace<0>(vertex_value);update(node);}voidset_edge_node_value(intnode,constEdgeValue&edge_value){check_node(node);access(node);_nodes[node].value.templateemplace<1>(edge_value);update(node);}voidevert_node(intnode){check_node(node);access(node);apply_reverse(node);}intcomponent_root_node(intnode){check_node(node);access(node);intcur=node;push(cur);while(_nodes[cur].left!=-1){cur=_nodes[cur].left;push(cur);}splay(cur);returncur;}boolconnected_nodes(intu,intv){if(u==v)returntrue;returncomponent_root_node(u)==component_root_node(v);}boollink_nodes(intu,intv){check_node(u);check_node(v);if(u==v)returnfalse;evert_node(u);if(component_root_node(v)==u)returnfalse;access(v);_nodes[u].parent=v;add_virtual_child(v,u);update(v);returntrue;}boolcut_nodes(intu,intv){check_node(u);check_node(v);if(u==v)returnfalse;evert_node(u);access(v);if(_nodes[v].left!=u||_nodes[u].right!=-1)returnfalse;_nodes[v].left=-1;_nodes[u].parent=-1;update(v);returntrue;}public:RakeCompressLinkCutTree()=default;explicitRakeCompressLinkCutTree(conststd::vector<VertexValue>&values){_nodes.reserve(values.size());_vertex_nodes.reserve(values.size());for(constVertexValue&value:values)add_vertex(value);}intvertex_count()const{returnint(_vertex_nodes.size());}boolempty()const{return_vertex_nodes.empty();}VertexIdadd_vertex(constVertexValue&vertex_value){VertexValuevalue=vertex_value;intnode=int(_nodes.size());_nodes.emplace_back(value);_vertex_nodes.push_back(node);returnint(_vertex_nodes.size())-1;}constVertexValue&get_vertex(VertexIdvertex)const{returnstd::get<0>(_nodes[vertex_node(vertex)].value);}voidset_vertex(VertexIdvertex,constVertexValue&vertex_value){set_vertex_node_value(vertex_node(vertex),vertex_value);}intedge_count()const{returnint(_edges.size());}booledge_alive(EdgeIdedge_id)const{check_edge(edge_id);return_edges[edge_id].alive;}std::pair<VertexId,VertexId>edge_endpoints(EdgeIdedge_id)const{check_edge(edge_id);return{_edges[edge_id].u,_edges[edge_id].v};}constEdgeValue&get_edge(EdgeIdedge_id)const{check_edge(edge_id);returnstd::get<1>(_nodes[_edges[edge_id].node].value);}voidset_edge(EdgeIdedge_id,constEdgeValue&edge_value){check_edge(edge_id);set_edge_node_value(_edges[edge_id].node,edge_value);}EdgeIdadd_edge(VertexIdu,VertexIdv,constEdgeValue&edge_value){check_vertex(u);check_vertex(v);if(u==v||connected(u,v))return-1;EdgeValuevalue=edge_value;intedge_id=int(_edges.size());intnode=add_edge_node(value);_edges.push_back(OriginalEdge{u,v,node,true});boolok1=link_nodes(vertex_node(u),node);boolok2=link_nodes(node,vertex_node(v));assert(ok1&&ok2);returnedge_id;}boolcut_edge(EdgeIdedge_id){check_edge(edge_id);OriginalEdge&edge=_edges[edge_id];if(!edge.alive)returnfalse;boolok1=cut_nodes(vertex_node(edge.u),edge.node);boolok2=cut_nodes(edge.node,vertex_node(edge.v));if(ok1&&ok2)edge.alive=false;returnok1&&ok2;}voidreroot(VertexIdvertex){evert_node(vertex_node(vertex));}boolconnected(VertexIdu,VertexIdv){check_vertex(u);check_vertex(v);returnconnected_nodes(vertex_node(u),vertex_node(v));}// Reroots the represented tree at vertex and returns its whole-tree cluster.Pathcomponent_prod(VertexIdvertex){intnode=vertex_node(vertex);evert_node(node);return_nodes[node].prod;}Pathquery_component(VertexIdvertex){returncomponent_prod(vertex);}};}// namespace ds}// namespace m1une#line 1 "math/modint.hpp"
#line 6 "math/modint.hpp"
#include<iostream>
#line 9 "math/modint.hpp"
namespacem1une{namespacemath{template<uint32_tModulus>structModInt{static_assert(0<Modulus,"Modulus must be positive");private:uint32_t_v;public:staticconstexpruint32_tmod(){returnModulus;}staticconstexprModIntraw(uint32_tv)noexcept{ModIntx;x._v=v;returnx;}constexprModInt()noexcept:_v(0){}template<classInteger,std::enable_if_t<std::is_integral_v<Integer>,int>=0>constexprModInt(Integerv)noexcept{ifconstexpr(std::is_signed_v<Integer>){int64_tx=static_cast<int64_t>(v)%static_cast<int64_t>(Modulus);if(x<0)x+=Modulus;_v=static_cast<uint32_t>(x);}else{_v=static_cast<uint32_t>(static_cast<uint64_t>(v)%Modulus);}}constexpruint32_tval()constnoexcept{return_v;}constexprModInt&operator++()noexcept{_v++;if(_v==Modulus)_v=0;return*this;}constexprModInt&operator--()noexcept{if(_v==0)_v=Modulus;_v--;return*this;}constexprModIntoperator++(int)noexcept{ModIntres=*this;++*this;returnres;}constexprModIntoperator--(int)noexcept{ModIntres=*this;--*this;returnres;}constexprModInt&operator+=(constModInt&rhs)noexcept{_v+=rhs._v;if(_v>=Modulus)_v-=Modulus;return*this;}constexprModInt&operator-=(constModInt&rhs)noexcept{_v-=rhs._v;if(_v>=Modulus)_v+=Modulus;return*this;}constexprModInt&operator*=(constModInt&rhs)noexcept{uint64_tz=_v;z*=rhs._v;_v=static_cast<uint32_t>(z%Modulus);return*this;}constexprModInt&operator/=(constModInt&rhs)noexcept{return*this*=rhs.inv();}constexprModIntoperator+(constModInt&rhs)constnoexcept{returnModInt(*this)+=rhs;}constexprModIntoperator-(constModInt&rhs)constnoexcept{returnModInt(*this)-=rhs;}constexprModIntoperator*(constModInt&rhs)constnoexcept{returnModInt(*this)*=rhs;}constexprModIntoperator/(constModInt&rhs)constnoexcept{returnModInt(*this)/=rhs;}constexprbooloperator==(constModInt&rhs)constnoexcept{return_v==rhs._v;}constexprbooloperator!=(constModInt&rhs)constnoexcept{return_v!=rhs._v;}constexprModIntpow(longlongn)constnoexcept{ModIntres=raw(1%Modulus);ModIntx=n<0?inv():*this;uint64_texponent=n<0?uint64_t(-(n+1))+1:uint64_t(n);while(exponent>0){if(exponent&1)res*=x;x*=x;exponent>>=1;}returnres;}constexprModIntinv()constnoexcept{int64_ta=_v,b=Modulus,u=1,v=0;while(b){int64_tt=a/b;a-=t*b;std::swap(a,b);u-=t*v;std::swap(u,v);}assert(a==1);u%=Modulus;if(u<0)u+=Modulus;returnraw(static_cast<uint32_t>(u));}friendstd::ostream&operator<<(std::ostream&os,constModInt&rhs){returnos<<rhs._v;}friendstd::istream&operator>>(std::istream&is,ModInt&rhs){longlongv;is>>v;rhs=ModInt(v);returnis;}};usingmodint998244353=ModInt<998244353>;usingmodint1000000007=ModInt<1000000007>;template<intId=0>structDynamicModInt{private:uint32_t_v;inlinestaticuint32_t_mod=1;public:staticuint32_tmod()noexcept{return_mod;}staticvoidset_mod(uint32_tmodulus)noexcept{assert(modulus>0);assert(modulus<=uint32_t(1)<<31);_mod=modulus;}staticDynamicModIntraw(uint32_tv)noexcept{assert(v<_mod);DynamicModIntx;x._v=v;returnx;}DynamicModInt()noexcept:_v(0){}template<classInteger,std::enable_if_t<std::is_integral_v<Integer>,int>=0>DynamicModInt(Integerv)noexcept{ifconstexpr(std::is_signed_v<Integer>){int64_tx=static_cast<int64_t>(v)%static_cast<int64_t>(_mod);if(x<0)x+=_mod;_v=static_cast<uint32_t>(x);}else{_v=static_cast<uint32_t>(static_cast<uint64_t>(v)%_mod);}}uint32_tval()constnoexcept{return_v;}DynamicModInt&operator++()noexcept{_v++;if(_v==_mod)_v=0;return*this;}DynamicModInt&operator--()noexcept{if(_v==0)_v=_mod;_v--;return*this;}DynamicModIntoperator++(int)noexcept{DynamicModIntresult=*this;++*this;returnresult;}DynamicModIntoperator--(int)noexcept{DynamicModIntresult=*this;--*this;returnresult;}DynamicModInt&operator+=(constDynamicModInt&rhs)noexcept{_v+=rhs._v;if(_v>=_mod)_v-=_mod;return*this;}DynamicModInt&operator-=(constDynamicModInt&rhs)noexcept{_v-=rhs._v;if(_v>=_mod)_v+=_mod;return*this;}DynamicModInt&operator*=(constDynamicModInt&rhs)noexcept{_v=static_cast<uint32_t>(uint64_t(_v)*rhs._v%_mod);return*this;}DynamicModInt&operator/=(constDynamicModInt&rhs)noexcept{return*this*=rhs.inv();}DynamicModIntoperator+(constDynamicModInt&rhs)constnoexcept{returnDynamicModInt(*this)+=rhs;}DynamicModIntoperator-(constDynamicModInt&rhs)constnoexcept{returnDynamicModInt(*this)-=rhs;}DynamicModIntoperator*(constDynamicModInt&rhs)constnoexcept{returnDynamicModInt(*this)*=rhs;}DynamicModIntoperator/(constDynamicModInt&rhs)constnoexcept{returnDynamicModInt(*this)/=rhs;}booloperator==(constDynamicModInt&rhs)constnoexcept{return_v==rhs._v;}booloperator!=(constDynamicModInt&rhs)constnoexcept{return_v!=rhs._v;}DynamicModIntpow(longlongexponent)constnoexcept{DynamicModIntresult=raw(1%_mod);DynamicModIntbase=exponent<0?inv():*this;uint64_tmagnitude=exponent<0?uint64_t(-(exponent+1))+1:uint64_t(exponent);while(magnitude>0){if(magnitude&1)result*=base;base*=base;magnitude>>=1;}returnresult;}DynamicModIntinv()constnoexcept{int64_ta=_v,b=_mod,u=1,v=0;while(b){int64_tquotient=a/b;a-=quotient*b;std::swap(a,b);u-=quotient*v;std::swap(u,v);}assert(a==1);u%=_mod;if(u<0)u+=_mod;returnraw(static_cast<uint32_t>(u));}friendstd::ostream&operator<<(std::ostream&os,constDynamicModInt&rhs){returnos<<rhs._v;}friendstd::istream&operator>>(std::istream&is,DynamicModInt&rhs){longlongvalue;is>>value;rhs=DynamicModInt(value);returnis;}};}// namespace math}// namespace m1une#line 8 "verify/ds/dynamic_tree/yosupo_point_set_tree_path_composite_sum.test.cpp"
usingmint=m1une::math::modint998244353;template<classT>structPointSetTreePathCompositeSum{structPoint{Ts;Tx;staticPointid(){returnPoint{T(0),T(0)};}Pointinv()const{returnPoint{T(0)-s,T(0)-x};}};structPath{Ta;Tb;Ts;Tx;};structVertexValue{Tx;};structEdgeValue{Ta;Tb;};staticPathmake_vertex_path(constPoint&d,constVertexValue&vertex){returnPath{T(1),T(0),d.s+vertex.x,d.x+T(1)};}staticPathmake_edge_path(constPoint&d,constEdgeValue&edge){returnPath{edge.a,edge.b,d.s*edge.a+d.x*edge.b,d.x};}staticPointto_point(constPath&path){returnPoint{path.s,path.x};}staticPointrake(constPoint&a,constPoint&b){returnPoint{a.s+b.s,a.x+b.x};}staticPathcompress(constPath&parent_side,constPath&child_side){returnPath{parent_side.a*child_side.a,parent_side.a*child_side.b+parent_side.b,parent_side.s+parent_side.a*child_side.s+parent_side.b*child_side.x,parent_side.x+child_side.x};}};intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingTreeDP=PointSetTreePathCompositeSum<mint>;usingVertexValue=typenameTreeDP::VertexValue;usingEdgeValue=typenameTreeDP::EdgeValue;usingLCT=m1une::ds::RakeCompressLinkCutTree<TreeDP>;intn,q;fast_input>>n>>q;LCTlct;std::vector<int>vertex_id(n);for(inti=0;i<n;i++){minta;fast_input>>a;vertex_id[i]=lct.add_vertex(VertexValue{a});}std::vector<int>edge_id(n-1);for(inti=0;i+1<n;i++){intu,v;mintb,c;fast_input>>u>>v>>b>>c;edge_id[i]=lct.add_edge(vertex_id[u],vertex_id[v],EdgeValue{b,c});}for(inti=0;i<q;i++){inttype;fast_input>>type;introot;if(type==0){intw;mintx;fast_input>>w>>x>>root;lct.set_vertex(vertex_id[w],VertexValue{x});}else{inte;minty,z;fast_input>>e>>y>>z>>root;lct.set_edge(edge_id[e],EdgeValue{y,z});}fast_output<<lct.component_prod(vertex_id[root]).s<<'\n';}}