D - Operation Log of the Rotating Stage Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

回転舞台の向きを表すために、円周を等しく M 個に分けた目盛りを使います。目盛りには、時計回りに 0,1,\ldots,M-1 の番号が付いています。舞台には矢印が一つあり、はじめは目盛り 0 を指しています。

高橋君は、舞台を回す操作を順に N 回行います。i 回目の操作では、次のどちらか一方を選びます。

  • 時計回りに A_i 目盛り分回す。
  • 反時計回りに B_i 目盛り分回す。

一周すると矢印は同じ目盛りへ戻ります。何周分回してもかまいません。また、0 目盛り分回す操作では向きは変わりません。

N 回の操作後に、矢印が目盛り X を指すような操作の選び方は何通りありますか。答えを 998244353 で割った余りを求めてください。

各回に時計回りと反時計回りのどちらを選んだかを記録し、一回でも選択が異なる記録は区別します。同じ目盛りへ着く二つの選択も区別します。

制約

  • 1\leq N,M\leq2000
  • 0\leq X<M
  • 0\leq A_i,B_i\leq10^9 (1\leq i\leq N)
  • 入力はすべて整数である。

入力

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

N M X
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目には、操作の回数 N、目盛りの個数 M、最後に指す目盛り X が与えられます。
  • 続く N 行のうち i 行目には、i 回目に選べる二つの回転量 A_i,B_i が与えられます。

出力

条件を満たす操作の選び方の個数を 998244353 で割った余りを出力してください。


入力例 1

2 5 0
1 1
1 1

出力例 1

2

入力例 2

1 6 2
1 3

出力例 2

0

入力例 3

3 4 1
1 1
2 2
0 4

出力例 3

4

入力例 4

5 7 3
1 0
2 0
3 0
4 0
5 0

出力例 4

5

入力例 5

1 1 0
1000000000 1000000000

出力例 5

2

Score : 366 pts

Problem Statement

To represent the orientation of a revolving stage, tick marks dividing the circumference into M equal parts are used. The tick marks are numbered 0,1,\ldots,M-1 in clockwise order. The stage has one arrow, which initially points to tick mark 0.

Takahashi will perform N operations of rotating the stage in sequence. In the i-th operation, he chooses one of the following:

  • Rotate the stage A_i tick marks clockwise.
  • Rotate the stage B_i tick marks counterclockwise.

After one full turn, the arrow returns to the same tick mark. The stage may make any number of full turns. Rotating by 0 tick marks does not change the orientation.

How many ways are there to choose the operations such that the arrow points to tick mark X after the N operations? Find the count modulo 998244353.

A record is kept of whether clockwise or counterclockwise was chosen for each operation, and two records are distinguished if they differ in at least one choice. Two choices are distinguished even if they result in the arrow pointing to the same tick mark.

Constraints

  • 1\leq N,M\leq2000
  • 0\leq X<M
  • 0\leq A_i,B_i\leq10^9 (1\leq i\leq N)
  • All input values are integers.

Input

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

N M X
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of operations N, the number of tick marks M, and the final tick mark X to point to.
  • The i-th of the following N lines contains the two rotation amounts A_i,B_i available for the i-th operation.

Output

Print the number of ways to choose the operations satisfying the condition, modulo 998244353.


Sample Input 1

2 5 0
1 1
1 1

Sample Output 1

2

Sample Input 2

1 6 2
1 3

Sample Output 2

0

Sample Input 3

3 4 1
1 1
2 2
0 4

Sample Output 3

4

Sample Input 4

5 7 3
1 0
2 0
3 0
4 0
5 0

Sample Output 4

5

Sample Input 5

1 1 0
1000000000 1000000000

Sample Output 5

2