Problem:
Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 l r b c: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c.

1 l r: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353.

Constraints

1 \leq N, Q \leq 500000

0 \leq a_i, c < 998244353

1 \leq b < 998244353

0 \leq l < r \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 7
1 2 3 4 5
1 0 5
0 2 4 100 101
1 0 3
0 1 3 102 103
1 2 5
0 2 5 104 105
1 0 5

Sample Output 1

15
404
41511
4317767

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=998244353;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>

template< typename T, typename S >
struct LazySegmentTree{
    int n;
    vector<T> data;
    vector<S> lazy;
    T te;
    S se;

    inline void merge_functions(S& lazy,S& val){
        //lazy+=val;
        //lazy=val;
        ll a=lazy.first;ll b=lazy.second;
        ll c=val.first;ll d=val.second;
        lazy.first=a*c%MOD;
        lazy.second=(c*b%MOD+d%MOD)%MOD;
    }
    inline void operate(T& data,S& val,int len){
        //data+=val*len;
        //data=val*len;
        //data+=val;
        //data=val;
        ll a=val.first;ll b=val.second;
        data=(a*data%MOD+b*len%MOD)%MOD;
    }
    inline T merge_values(T& x,T& y){
        return (x+y)%MOD;
        //return min(x,y);
        //return max(x,y);
    }

    LazySegmentTree(){}
    LazySegmentTree(int sz,T te,S se):te(te),se(se){
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1,te);
        lazy.resize(2*n-1,se);
    }

    void build(const vector<T> &A){
        for(int k=0;k<int(A.size());k++) data[k+n-1]=A[k];
        for(int k=n-2;k>=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]);
    }

    void eval(int k,int l,int r){
        if(lazy[k]==se) return;
        operate(data[k],lazy[k],r-l);
        if(r-l>1){
            merge_functions(lazy[2*k+1],lazy[k]);
            merge_functions(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=se;
    }

    void update(int a,int b,S val,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            merge_functions(lazy[k],val);
            eval(k,l,r);
        }else{
            update(a,b,val,2*k+1,l,(l+r)/2);
            update(a,b,val,2*k+2,(l+r)/2,r);
            data[k]=merge_values(data[2*k+1],data[2*k+2]);
        }
    }

    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return te;
        if(a<=l&&r<=b) return data[k];

        T vl=query(a,b,2*k+1,l,(l+r)/2);
        T vr=query(a,b,2*k+2,(l+r)/2,r);
        return merge_values(vl,vr);
    }

    T get(int p){
        return query(p,p+1);
    }
};

/*
  LazySegmentTree<ll,ll> seg(N,te,se);
  vector<ll> init(N,0);
  seg.build(init);
*/

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,Q;
  cin>>N>>Q;
  vector<ll> A(N);
  rep(i,N) cin>>A[i];

  LazySegmentTree<ll,pair<ll,ll>> seg(N,0,mkp(1,0));
  seg.build(A);
  rep(q,Q){
      int T;cin>>T;
      if(T==0){
          int L,R;ll A,B;
          cin>>L>>R>>A>>B;
          seg.update(L,R,mkp(A,B));
      }else{
          int L,R;
          cin>>L>>R;
          ll ans=seg.query(L,R);
          cout<<ans<<"\n";
      }
  }

  return 0;
}