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 with AC Library v1.1) (judge verdict: Accepted)

#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

using mint=modint998244353;

#define REP(i,m,n) for(int i=(m);i<(n);++i)
#define rep(i,n) REP(i,0,n)

struct S{
  mint a;
  int size;
};

S op(S l, S r){
  return S{l.a+r.a,l.size+r.size};
}

S e(){
  return S{0,0};
}

struct F{
  mint a,b;
};

S mapping(F l, S r){
  return S{r.a*l.a+r.size*l.b,r.size};
}

F composition(F l, F r){
  return F{l.a*r.a,r.b*l.a+l.b};
}

F id(){
  return F{1,0};
}

int main(){
  int N,Q;
  scanf("%d%d",&N,&Q);
  
  vector<S> a(N);
  rep(i,N){
    int x;
    scanf("%d",&x);
    a[i]={x,1};
  }
  
  lazy_segtree<S, op, e, F, mapping, composition, id> st(a);
  
  rep(i,Q){
    int t;
    scanf("%d",&t);
    if(t==0){
      int l,r,b,c;
      scanf("%d%d%d%d",&l,&r,&b,&c);
      st.apply(l,r,F{b,c});
    }
    else if(t==1){
      int l,r;
      scanf("%d%d",&l,&r);
      printf("%d\n",st.prod(l,r).a.val());
    }
  }
  
  return 0;
}