PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000250. Biến đổi affine và tính tổng đoạn (Range Affine Range Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Biến đổi affine và tính tổng đoạn (Range Affine Range Sum)

Range Affine Range Sum

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given the array a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}. Values are considered modulo 998244353998244353. Process qq operations in order:

  • 0 l r b c: for every i∈[l,r)i\in[l,r), replace aia_i with (bai+c) mod 998244353(b a_i+c)\bmod998244353.
  • 1 l r: print (∑i=lr−1ai) mod 998244353\bigl(\sum_{i=l}^{r-1}a_i\bigr)\bmod998244353.

Successive transformations must be applied in input order.

Input

  • The first line contains n,qn,q.
  • The second line contains nn values a0,…,an−1a_0,\ldots,a_{n-1}.
  • The next qq lines contain operations 0 l r b c or 1 l r.

Output

For each type 1 operation, print the corresponding sum modulo 998244353998244353 on a separate line.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤601\le q\le 60; 0≤ai,c<9982443530\le a_i,c<998244353; 1≤b<9982443531\le b<998244353; 0≤l<r≤n0\le l<r\le n.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 0≤ai,c<9982443530\le a_i,c<998244353; 1≤b<9982443531\le b<998244353; 0≤l<r≤n0\le l<r\le n.

  • Subtask 3 (50 points): 1≤n≤5000001\le n\le 500000; 1≤q≤5000001\le q\le 500000; 0≤ai,c<9982443530\le a_i,c<998244353; 1≤b<9982443531\le b<998244353; 0≤l<r≤n0\le l<r\le n.

Examples

Input

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

Output

15
404
41511
4317767

Explanation

The initial sum is 1+2+3+4+5=151+2+3+4+5=15. The first update changes a2=3a_2=3 to 401401 and a3=4a_3=4 to 501501, so the next sum is 1+2+401=4041+2+401=404. The second update changes a1a_1 from 22 to 307307 and a2a_2 from 401401 to 4100541005; therefore the sum over [2,5)[2,5) is 41005+501+5=4151141005+501+5=41511. The final update changes a2a_2 to 42646254264625, a3a_3 to 5220952209, and a4a_4 to 625625. The final full-array sum is 1+307+4264625+52209+625=43177671+307+4264625+52209+625=4317767.