E - Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 個の袋があります。
i には L_i 個のボールが入っていて、袋 ij(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。

それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?

ただし、書かれた数が同じであっても全てのボールは区別します。

制約

  • N \geq 2
  • L_i \geq 2
  • 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力に含まれる値は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

出力

答えを出力せよ。


入力例 1

2 40
3 1 8 4
2 10 5

出力例 1

2

13 番目のボールと袋 21 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
12 番目のボールと袋 22 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。


入力例 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

出力例 2

45

書かれた数が同じであっても全てのボールは区別することに注意してください。


入力例 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

出力例 3

0

総積が X になる取り出し方が 1 つも存在しないこともあります。

Score : 300 points

Problem Statement

We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.

We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?

Here, we distinguish all balls, even with the same numbers written on them.

Constraints

  • N \geq 2
  • L_i \geq 2
  • The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

Output

Print the answer.


Sample Input 1

2 40
3 1 8 4
2 10 5

Sample Output 1

2

When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.


Sample Input 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

Sample Output 2

45

Note that we distinguish all balls, even with the same numbers written on them.


Sample Input 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

Sample Output 3

0

There may be no way to make the product X.

F - Manga

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。

高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。

  • 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う

その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。

高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

6
1 2 4 6 7 271

出力例 1

4

『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。


入力例 2

10
1 1 1 1 1 1 1 1 1 1

出力例 2

5

高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。

  • 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う

入力例 3

1
5

出力例 3

0

高橋君は 1 巻を読めません。

Score : 300 points

Problem Statement

Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.

Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:

  • Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.

Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).

Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

6
1 2 4 6 7 271

Sample Output 1

4

Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.


Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

5

Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:

  • Sell two books of Volume 1 and buy a book of Volume 2 instead.
  • Sell two books of Volume 1 and buy a book of Volume 3 instead.
  • Sell two books of Volume 1 and buy a book of Volume 4 instead.
  • Sell two books of Volume 1 and buy a book of Volume 5 instead.

Sample Input 3

1
5

Sample Output 3

0

Takahashi cannot read Volume 1.

G - Swapping Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

HW 列の 2 つのグリッド A, B が与えられます。

1 \leq i \leq H1 \leq j \leq W を満たす各整数の組 (i, j) について、 i 行目 j 列目にあるマスをマス (i, j) と呼ぶとき、 グリッド A の マス (i, j) には整数 A_{i, j} が、 グリッド B の マス (i, j) には整数 B_{i, j} がそれぞれ書かれています。

あなたは「下記の 2 つのうちのどちらか 1 つを行う」という操作を好きな回数( 0 回でもよい)だけ繰り返します。

  • 1 \leq i \leq H-1 を満たす整数 i を選び、グリッド A の i 行目と (i+1) 行目を入れ替える。
  • 1 \leq i \leq W-1 を満たす整数 i を選び、グリッド A の i 列目と (i+1) 列目を入れ替える。

上記の操作の繰り返しによって、グリッド A をグリッド B に一致させることが可能かどうかを判定してください。 さらに、一致させることが可能な場合は、そのために行う操作回数の最小値を出力してください。

ここで、グリッド A とグリッド B が一致しているとは、 1 \leq i \leq H1 \leq j \leq W を満たす全ての整数の組 (i, j) について、 グリッド A の マス (i, j) とグリッド B の マス (i, j) に書かれた整数が等しいこととします。

制約

  • 入力される値は全て整数
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

入力

入力は以下の形式で標準入力から与えられる。

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

出力

グリッド A をグリッド B に一致させることが不可能な場合は -1 を、 可能な場合はグリッド A をグリッド B に一致させるために行う操作回数の最小値を出力せよ。


入力例 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

出力例 1

3

初期状態のグリッド A の 4 列目と 5 列目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

続けて、グリッド A の 2 行目と 3 行目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

最後に、グリッド A の 2 列目と 3 列目を入れ替えると、グリッド A は下記の通りになり、グリッド B に一致します。

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

上に述べた 3 回の操作でグリッド A をグリッド B に一致させることができ、 これより少ない回数の操作でグリッド A をグリッド B に一致させることはできないため、 3 を出力します。


入力例 2

2 2
1 1
1 1
1 1
1 1000000000

出力例 2

-1

問題文中の操作をどのように行ってもグリッド A をグリッド B に一致させることは不可能であるため -1 を出力します。


入力例 3

3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2

出力例 3

0

グリッド A ははじめからグリッド B に一致しています。


入力例 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

出力例 4

20

Score : 425 points

Problem Statement

You are given two grids, A and B, each with H rows and W columns.

For each pair of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, let (i, j) denote the cell in the i-th row and j-th column. In grid A, cell (i, j) contains the integer A_{i, j}. In grid B, cell (i, j) contains the integer B_{i, j}.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer i satisfying 1 \leq i \leq H-1 and swap the i-th and (i+1)-th rows in grid A.
  • Choose an integer i satisfying 1 \leq i \leq W-1 and swap the i-th and (i+1)-th columns in grid A.

Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.

Here, grid A is identical to grid B if and only if, for all pairs of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, the integer written in cell (i, j) of grid A is equal to the integer written in cell (i, j) of grid B.

Constraints

  • All input values are integers.
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

Input

The input is given from Standard Input in the following format:

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

Output

If it is impossible to make grid A identical to grid B, output -1. Otherwise, print the minimum number of operations required to make grid A identical to grid B.


Sample Input 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

Sample Output 1

3

Swapping the fourth and fifth columns of the initial grid A yields the following grid:

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

Then, swapping the second and third rows yields the following grid:

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

Finally, swapping the second and third columns yields the following grid, which is identical to grid B:

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3.


Sample Input 2

2 2
1 1
1 1
1 1
1 1000000000

Sample Output 2

-1

There is no way to perform the operation to make grid A match grid B, so print -1.


Sample Input 3

3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2

Sample Output 3

0

Grid A is already identical to grid B at the beginning.


Sample Input 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

Sample Output 4

20
H - Pac-Takahashi

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

HW 列のグリッドがあります。 上から i 行目、左から j 列目のマス目を (i,j) と表します。 グリッドの各マスはスタートマス、ゴールマス、空マス、壁マス、お菓子マスのいずれかです。 (i,j) が何のマスであるかは文字 A_{i,j} によって表され、A_{i,j}= S のときスタートマス、 A_{i,j}= G のときゴールマス、 A_{i,j}= . のとき空マス、 A_{i,j}= # のとき壁マス、 A_{i,j}= o のときお菓子マスです。 ここで、スタートマスとゴールマスはちょうど 1 つずつあり、お菓子マスは 18 個以下であることが保証されます。

高橋くんは現在スタートマスにいます。 高橋くんは、上下左右に隣接するマスであって壁マスでないマスに移動することを繰り返し行えます。 高橋くんは今から T 回以下の移動によってゴールマスに到達したいです。 そのようなことは可能かどうか判定してください。 可能な場合は、最終的にゴールマスにいるという条件のもとで、移動の途中に訪れるお菓子マスの数の最大値を求めてください。 ただし、1 つのお菓子マスに複数回訪れた場合でも、カウントするのは 1 回のみです。

制約

  • 1\leq H,W \leq 300
  • 1 \leq T \leq 2\times 10^6
  • H,W,T は整数
  • A_{i,j}S, G, ., #, o のいずれか
  • A_{i,j}= S を満たす (i,j) の組がちょうど 1 つ存在する
  • A_{i,j}= G を満たす (i,j) の組がちょうど 1 つ存在する
  • A_{i,j}= o を満たす (i,j) の組は 18 個以下

入力

入力は以下の形式で標準入力から与えられる。

H W T
A_{1,1}A_{1,2}\dots A_{1,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}

出力

T 回以下の移動によってゴールマスに到達することが不可能ならば -1 を出力せよ。 可能ならば、最終的にゴールマスにいるという条件のもとで、移動の途中に訪れるお菓子マスの数の最大値を出力せよ。


入力例 1

3 3 5
S.G
o#o
.#.

出力例 1

1

(1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (1,3)4 回移動すると、 1 個のお菓子マスを訪れた上で最終的にゴールマスにいることができます。 5 回以下の移動で 2 個のお菓子マスを訪れた上で最終的にゴールマスにいることはできないので、1 が答えです。

なお、(1,1) \rightarrow (2,1) \rightarrow (1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) と移動すると 5 回の移動で 2 個のお菓子マスを訪れることができますが、最終的にゴールマスにいないため無効であることに注意してください。


入力例 2

3 3 1
S.G
.#o
o#.

出力例 2

-1

1 回以下の移動でゴールマスに到達することはできません。


入力例 3

5 10 2000000
S.o..ooo..
..o..o.o..
..o..ooo..
..o..o.o..
..o..ooo.G

出力例 3

18

Score : 475 points

Problem Statement

We have a grid with H rows and W columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left. Each square in the grid is one of the following: the start square, the goal square, an empty square, a wall square, and a candy square. (i,j) is represented by a character A_{i,j}, and is the start square if A_{i,j}= S, the goal square if A_{i,j}= G, an empty square if A_{i,j}= ., a wall square if A_{i,j}= #, and a candy square if A_{i,j}= o. Here, it is guaranteed that there are exactly one start, exactly one goal, and at most 18 candy squares.

Takahashi is now at the start square. He can repeat moving to a vertically or horizontally adjacent non-wall square. He wants to reach the goal square in at most T moves. Determine whether it is possible. If it is possible, find the maximum number of candy squares he can visit on the way to the goal square, where he must finish. Each candy square counts only once, even if it is visited multiple times.

Constraints

  • 1\leq H,W \leq 300
  • 1 \leq T \leq 2\times 10^6
  • H, W, and T are integers.
  • A_{i,j} is one of S, G, ., #, and o.
  • Exactly one pair (i,j) satisfies A_{i,j}= S.
  • Exactly one pair (i,j) satisfies A_{i,j}= G.
  • At most 18 pairs (i,j) satisfy A_{i,j}= o.

Input

The input is given from Standard Input in the following format:

H W T
A_{1,1}A_{1,2}\dots A_{1,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}

Output

If it is impossible to reach the goal square in at most T moves, print -1. Otherwise, print the maximum number of candy squares that can be visited on the way to the goal square, where Takahashi must finish.


Sample Input 1

3 3 5
S.G
o#o
.#.

Sample Output 1

1

If he makes four moves as (1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (1,3), he can visit one candy square and finish at the goal square. He cannot make five or fewer moves to visit two candy squares and finish at the goal square, so the answer is 1.

Note that making five moves as (1,1) \rightarrow (2,1) \rightarrow (1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) to visit two candy squares is invalid since he would not finish at the goal square.


Sample Input 2

3 3 1
S.G
.#o
o#.

Sample Output 2

-1

He cannot reach the goal square in one or fewer moves.


Sample Input 3

5 10 2000000
S.o..ooo..
..o..o.o..
..o..ooo..
..o..o.o..
..o..ooo.G

Sample Output 3

18
I - Two Sequence Queries

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。
Q 個のクエリが与えられるので、順に処理してください。

クエリは次の 3 種類です。

  • 1 l r x : A_l, A_{l+1}, \ldots, A_rx を加える。
  • 2 l r x : B_l, B_{l+1}, \ldots, B_rx を加える。
  • 3 l r : \displaystyle\sum_{i=l}^r (A_i\times B_i)998244353 で割った余りを出力する。

制約

  • 1\leq N,Q\leq 2\times 10^5
  • 0\leq A_i,B_i\leq 10^9
  • 1\leq l\leq r\leq N
  • 1\leq x\leq 10^9
  • 入力はすべて整数
  • 3 種類目のクエリが 1 つ以上存在する。

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{query}_i (1\leq i\leq Q)i 番目に処理するクエリである。

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 l r x
2 l r x
3 l r

出力

3 種類目のクエリが K 個あるとき、K 行出力せよ。
i 行目 (1\leq i\leq K) には、i 個目の 3 種類目のクエリに対する出力を出力せよ。


入力例 1

5 6
1 3 5 6 8
3 1 2 1 2
3 1 3
1 2 5 3
3 1 3
1 1 3 1
2 5 5 2
3 1 5

出力例 1

16
25
84

最初、A=(1,3,5,6,8), B=(3,1,2,1,2) です。クエリは次の順で処理されます。

  • 1 個目のクエリでは (1\times 3)+(3\times 1)+(5\times 2)=16998244353 で割った余りである 16 を出力します。
  • 2 個目のクエリでは A_2,A_3,A_4,A_53 を加えます。A=(1,6,8,9,11) となります。
  • 3 個目のクエリでは (1\times 3)+(6\times 1)+(8\times 2)=25998244353 で割った余りである 25 を出力します。
  • 4 個目のクエリでは A_1,A_2,A_31 を加えます。A=(2,7,9,9,11) となります。
  • 5 個目のクエリでは B_52 を加えます。B=(3,1,2,1,4) となります。
  • 6 個目のクエリでは (2\times 3)+(7\times 1)+(9\times 2)+(9\times 1)+(11\times 4)=84998244353 で割った余りである 84 を出力します。

よって、1, 2, 3 行目にはそれぞれ 16, 25, 84 を出力します。


入力例 2

2 3
1000000000 1000000000
1000000000 1000000000
3 1 1
1 2 2 1000000000
3 1 2

出力例 2

716070898
151723988

3 種類目のクエリでは 998244353 で割った余りを出力することに注意してください。

Score : 550 points

Problem Statement

You are given sequences of length N, A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
You are also given Q queries to process in order.

There are three types of queries:

  • 1 l r x : Add x to each of A_l, A_{l+1}, \ldots, A_r.
  • 2 l r x : Add x to each of B_l, B_{l+1}, \ldots, B_r.
  • 3 l r : Print the remainder of \displaystyle\sum_{i=l}^r (A_i\times B_i) when divided by 998244353.

Constraints

  • 1\leq N,Q\leq 2\times 10^5
  • 0\leq A_i,B_i\leq 10^9
  • 1\leq l\leq r\leq N
  • 1\leq x\leq 10^9
  • All input values are integers.
  • There is at least one query of the third type.

Input

The input is given from Standard Input in the following format. Here, \mathrm{query}_i (1\leq i\leq Q) is the i-th query to be processed.

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 l r x
2 l r x
3 l r

Output

If there are K queries of the third type, print K lines.
The i-th line (1\leq i\leq K) should contain the output for the i-th query of the third type.


Sample Input 1

5 6
1 3 5 6 8
3 1 2 1 2
3 1 3
1 2 5 3
3 1 3
1 1 3 1
2 5 5 2
3 1 5

Sample Output 1

16
25
84

Initially, A=(1,3,5,6,8) and B=(3,1,2,1,2). The queries are processed in the following order:

  • For the first query, print (1\times 3)+(3\times 1)+(5\times 2)=16 modulo 998244353, which is 16.
  • For the second query, add 3 to A_2,A_3,A_4,A_5. Now A=(1,6,8,9,11).
  • For the third query, print (1\times 3)+(6\times 1)+(8\times 2)=25 modulo 998244353, which is 25.
  • For the fourth query, add 1 to A_1,A_2,A_3. Now A=(2,7,9,9,11).
  • For the fifth query, add 2 to B_5. Now B=(3,1,2,1,4).
  • For the sixth query, print (2\times 3)+(7\times 1)+(9\times 2)+(9\times 1)+(11\times 4)=84 modulo 998244353, which is 84.

Thus, the first, second, and third lines should contain 16, 25, and 84, respectively.


Sample Input 2

2 3
1000000000 1000000000
1000000000 1000000000
3 1 1
1 2 2 1000000000
3 1 2

Sample Output 2

716070898
151723988

Make sure to print the sum modulo 998244353 for the third type of query.