A - Triangular Number

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

正整数 N が与えられます。
1 以上 N 以下の整数をすべて足し合わせた値 1+2+\cdots+N を出力してください。

制約

  • 1 \leq N \leq 100
  • N は整数

入力

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

N

出力

1 以上 N 以下の整数をすべて足し合わせた値を出力せよ。


入力例 1

5

出力例 1

15

1+2+3+4+5=15 のため、15 を出力します。


入力例 2

1

出力例 2

1

入力例 3

29

出力例 3

435

Score : 100 points

Problem Statement

You are given a positive integer N.
Output the sum of all integers from 1 to N, that is, 1+2+\cdots+N.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

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

N

Output

Output the sum of all integers from 1 to N.


Sample Input 1

5

Sample Output 1

15

Since 1+2+3+4+5=15, output 15.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

29

Sample Output 3

435
B - No-Divisible Range

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\leq l\leq r\leq N をみたす整数の組 (l,r) であって、 次の条件をみたすものの個数を求めてください。

l\leq i\leq r をみたす任意の整数 i について、A_iA_l+A_{l+1}+\cdots+A_r の約数でない

制約

  • 1 \leq N \leq 50
  • 1 \leq A_i \leq 1000
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
8 6 10 5 7

出力例 1

6

A=(8,6,10,5,7) です。

例えば、(l,r)=(1,2) は、A_l+A_{l+1}+\cdots+A_r=A_1+A_2=14 であり、A_1=8, A_2=6 はどちらも 14 の約数でないため、条件をみたします。
一方で、(l,r)=(1,3) は、A_l+A_{l+1}+\cdots+A_r=A_1+A_2+A_3=24 であり、A_1=824 の約数であるため、条件をみたしません。

条件をみたす組は (l,r)=(1,2), (1,4), (2,3), (2,4), (3,5), (4,5)6 つであるため、6 を出力します。


入力例 2

3
1 1 1

出力例 2

0

Score : 200 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\ldots,A_N) of length N.
Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N that satisfy the following condition:

For every integer i satisfying l\leq i\leq r, A_i is not a divisor of A_l+A_{l+1}+\cdots+A_r.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq A_i \leq 1000
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

5
8 6 10 5 7

Sample Output 1

6

We have A=(8,6,10,5,7).

For example, (l,r)=(1,2) satisfies the condition because A_l+A_{l+1}+\cdots+A_r=A_1+A_2=14, and neither A_1=8 nor A_2=6 is a divisor of 14.
On the other hand, (l,r)=(1,3) does not satisfy the condition because A_l+A_{l+1}+\cdots+A_r=A_1+A_2+A_3=24, and A_1=8 is a divisor of 24.

The pairs that satisfy the condition are (l,r)=(1,2), (1,4), (2,3), (2,4), (3,5), (4,5), which is six pairs, so output 6.


Sample Input 2

3
1 1 1

Sample Output 2

0
C - Domino

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

N 個のドミノが数直線上に一列に並んでいます。i 番目のドミノは座標 i の位置に立っており、高さは A_i です。

i 番目のドミノが右に倒れると、座標 i 以上 i+A_i 未満の範囲にあるドミノが全て右に倒れます。

1 番目のドミノを右に倒したとき、全部でいくつのドミノが倒れますか?

制約

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq N
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

1 番目のドミノを右に倒したときに倒れるドミノの個数を出力せよ。


入力例 1

4
3 1 4 1

出力例 1

4

1 番目のドミノが右に倒れると、2,3 番目のドミノも右に倒れます。3 番目のドミノが右に倒れると、4 番目のドミノも倒れます。


入力例 2

9
1 4 1 4 2 1 3 5 6

出力例 2

1

1 番目のドミノを右に倒しても、他のドミノが倒れることはありません。


入力例 3

10
5 4 3 2 1 1 2 3 4 5

出力例 3

5

Score : 300 points

Problem Statement

There are N dominoes standing in a row on a number line. The i-th domino is standing at coordinate i and has height A_i.

When the i-th domino falls to the right, all dominoes in the range between coordinates i and i+A_i-1, inclusive, fall to the right.

How many dominoes will fall in total when the first domino falls to the right?

Constraints

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq N
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output the number of dominoes that fall when the first domino falls to the right.


Sample Input 1

4
3 1 4 1

Sample Output 1

4

When the first domino falls to the right, the second and third dominoes also fall to the right. When the third domino falls to the right, the fourth domino also falls.


Sample Input 2

9
1 4 1 4 2 1 3 5 6

Sample Output 2

1

When the first domino falls to the right, no other dominoes will fall.


Sample Input 3

10
5 4 3 2 1 1 2 3 4 5

Sample Output 3

5
D - Reachability Query 2

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 425

問題文

N 頂点 M 辺の有向グラフが与えられます。
頂点には 1 から N の番号がついており、i 番目の辺は頂点 X_i から頂点 Y_i への有向辺です。
最初全ての頂点は白色です。

Q 個のクエリが与えられるので順に処理してください。クエリは以下の 2 種類のいずれかです。

  • 1 v:頂点 v を黒色にする
  • 2 v:頂点 v から辺を辿って黒色の頂点に到達可能かどうか判定する

制約

  • 1\leq N \leq 3\times 10^5
  • 0\leq M \leq 3\times 10^5
  • 1\leq Q \leq 3\times 10^5
  • 1\leq X_i,Y_i \leq N
  • 自己辺をもたない。すなわち X_i \neq Y_i
  • 多重辺をもたない。すなわち (X_i,Y_i) は相異なる
  • クエリにおいて 1 \leq v \leq N
  • 入力は全て整数

入力

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

N M
X_1 Y_1
\vdots
X_M Y_M
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

\mathrm{query}_ii 番目のクエリを表し、以下のいずれかの形式で与えられる。

1 v
2 v

出力

2 種類目のクエリの個数を q として q 行出力せよ。

i 行目には、i 番目の 2 種類目のクエリにおいて到達可能なら Yes、到達不可能なら No と出力せよ。


入力例 1

5 6
1 2
2 3
3 1
4 5
1 4
2 5
5
1 3
2 1
2 4
1 5
2 4

出力例 1

Yes
No
Yes
  • 最初、与えられたグラフは下図一番左の通りです。
  • 1 番目のクエリにより頂点 3 が黒色になり、下図中央のようになります。
  • 2 番目のクエリにおいて、頂点 1 から黒色の頂点 3 に到達可能です。
  • 3 番目のクエリにおいて、頂点 4 から黒色の頂点に到達することはできません。
  • 4 番目のクエリにより頂点 5 が黒色になり、下図右のようになります。
  • 5 番目のクエリにおいて、頂点 4 から黒色の頂点 5 に到達可能です。

図

Score : 425 points

Problem Statement

You are given a directed graph with N vertices and M edges.
The vertices are numbered from 1 to N, and the i-th edge is a directed edge from vertex X_i to vertex Y_i.
Initially, all vertices are white.

Process Q queries in order. Each query is of one of the following two types:

  • 1 v: Color vertex v black.
  • 2 v: Determine whether it is possible to reach a black vertex by following edges from vertex v.

Constraints

  • 1\leq N \leq 3\times 10^5
  • 0\leq M \leq 3\times 10^5
  • 1\leq Q \leq 3\times 10^5
  • 1\leq X_i,Y_i \leq N
  • There are no self-loops, that is, X_i \neq Y_i.
  • There are no multiple edges, that is, (X_i,Y_i) are distinct.
  • In queries, 1 \leq v \leq N.
  • All input values are integers.

Input

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

N M
X_1 Y_1
\vdots
X_M Y_M
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

\mathrm{query}_i represents the i-th query and is given in one of the following formats:

1 v
2 v

Output

Let q be the number of queries of the second type. Output q lines.

The i-th line should contain Yes if a black vertex is reachable in the i-th query of the second type, and No otherwise.


Sample Input 1

5 6
1 2
2 3
3 1
4 5
1 4
2 5
5
1 3
2 1
2 4
1 5
2 4

Sample Output 1

Yes
No
Yes
  • Initially, the given graph is as shown in the leftmost figure below.
  • By the first query, vertex 3 becomes black, as shown in the center figure.
  • In the second query, it is possible to reach black vertex 3 from vertex 1.
  • In the third query, it is not possible to reach a black vertex from vertex 4.
  • By the fourth query, vertex 5 becomes black, as shown in the rightmost figure.
  • In the fifth query, it is possible to reach black vertex 5 from vertex 4.

Figure

E - Cover query

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

N 個のマスが左右一列に並んでいます。
左から i (1\leq i\leq N) 番目のマスをマス i と呼びます。
最初、すべてのマスは白く塗られています。

Q 個のクエリが与えられるので、順に処理してください。
i (1\leq i\leq Q) 個目のクエリは次のとおりです。

2 つの整数 (L_i,R_i) (1\leq L_i\leq R_i\leq N) が与えられる。
マス L_i, L_i+1, \ldots, R_i をすべて黒く塗る。
このとき、元々白く塗られていたマスは新しく黒く塗られ、元々黒く塗られていたマスはそのままとなる。
その後、(N 個のマスのうち)白く 塗られているマスの個数を求める。

制約

  • 1\leq N\leq 10^9
  • 1\leq Q\leq 2\times 10^5
  • 1\leq L_i\leq R_i\leq N
  • 入力はすべて整数

入力

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

N Q
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

出力

Q 行出力せよ。
i 行目 (1\leq i\leq Q) には、i 個目のクエリに対する答えを出力せよ。


入力例 1

10 5
3 5
8 9
5 8
2 9
6 6

出力例 1

7
5
3
2
2

最初、10 個のマスが左右一列に並んでいます。

  • 1 個目のクエリではマス 3,4,5 を黒く塗ります。操作後、白く塗られているのはマス 1,2,6,7,8,9,107 マスです。
  • 2 個目のクエリではマス 8,9 を黒く塗ります。操作後、白く塗られているのはマス 1,2,6,7,105 マスです。
  • 3 個目のクエリではマス 5,6,7,8 を黒く塗ります。操作後、白く塗られているのはマス 1,2,103 マスです。
  • 4 個目のクエリではマス 2,3,\ldots,9 を黒く塗ります。操作後、白く塗られているのはマス 1,102 マスです。
  • 5 個目のクエリではマス 6 を黒く塗ります。操作後、白く塗られているのはマス 1,102 マスです。

よって、7,5,3,2,2 をこの順に各行に出力します。


入力例 2

1000000000 1
1 500000000

出力例 2

500000000

Score : 450 points

Problem Statement

There are N cells arranged in a row from left to right.
The i-th cell from the left (1\leq i\leq N) is called cell i.
Initially, all cells are painted white.

Process Q queries in order.
The i-th query (1\leq i\leq Q) is as follows:

Two integers (L_i,R_i) (1\leq L_i\leq R_i\leq N) are given.
Paint all cells L_i, L_i+1, \ldots, R_i black.
Here, cells that were painted white are now painted black, and cells that were painted black remain as is.
After that, find the number of cells (among the N cells) that are painted white.

Constraints

  • 1\leq N\leq 10^9
  • 1\leq Q\leq 2\times 10^5
  • 1\leq L_i\leq R_i\leq N
  • All input values are integers.

Input

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

N Q
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

Output

Output Q lines.
The i-th line (1\leq i\leq Q) should contain the answer to the i-th query.


Sample Input 1

10 5
3 5
8 9
5 8
2 9
6 6

Sample Output 1

7
5
3
2
2

Initially, 10 cells are arranged in a row from left to right.

  • The first query paints cells 3,4,5 black. After the operation, the cells painted white are cells 1,2,6,7,8,9,10, which is 7 cells.
  • The second query paints cells 8,9 black. After the operation, the cells painted white are 1,2,6,7,10, which is 5 cells.
  • The third query paints cells 5,6,7,8 black. After the operation, the cells painted white are 1,2,10, which is 3 cells.
  • The fourth query paints cells 2,3,\ldots,9 black. After the operation, the cells painted white are 1,10, which is 2 cells.
  • The fifth query paints cell 6 black. After the operation, the cells painted white are cells 1,10, which is 2 cells.

Thus, output 7,5,3,2,2 in this order on each line.


Sample Input 2

1000000000 1
1 500000000

Sample Output 2

500000000
F - Cat exercise

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 550

問題文

N 個のキャットタワーが左右一列に並んでおり、左から i 番目 (1\leq i\leq N) のタワーの高さは P_i です。
ここで、(P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の並び替えとなっています。
また、左から i 番目のタワーと j 番目のタワーの間の距離を \lvert i-j\rvert で定義します。

猫が一匹おり、最初、高さ N のキャットタワーの上にいます。
高橋君はタワーを一つ選んで撤去することを繰り返すことで、この猫を運動させたいです。

高橋君がタワーを撤去するとき、猫は以下のように移動します。

  • 撤去するタワーの上に猫がいない場合、猫は移動しません。
  • 撤去するタワーの上に猫がおり、かつそのタワーと左右に隣接するタワーのうち少なくとも一方が存在するとき、撤去されるタワーからスタートして隣接するタワーへの移動を繰り返して到達できるタワーのうち、撤去されるタワーを除いて最も高いタワーに猫が移動します。 このとき、移動前と移動後のタワーの間の距離だけ移動が発生します(移動前後および途中のタワーの高さは関係しません)。
  • 撤去するタワーの上に猫がおり、かつそのタワーと左右に隣接するタワーが存在しないとき、猫は高橋君の腕の中に飛び込み、運動はここで終了となります。このときには移動は発生しません。

ここで、撤去したタワーの間は詰めることはありません。 すなわち、最初の時点で左から i 番目のタワーと隣接しているタワーは(存在すれば)左から (i-1) 番目と (i+1) 番目のタワーのみであり、 その後 他のタワーの撤去によって新しく他のタワーと隣接するようになることはありません

運動が終了するまでの猫の移動距離の合計としてあり得る最大の値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • (P_1,P_2,\ldots, P_N)(1,2,\ldots,N) の並び替えである。
  • 入力はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

運動が終了するまでの猫の移動距離の合計としてあり得る最大の値を出力せよ。


入力例 1

5
5 3 4 1 2

出力例 1

5

最初、キャットタワーの高さは左から順に 5,3,4,1,2 です。
以下、左から i 番目 (1\leq i\leq 5) のキャットタワーをタワー i と呼びます。
最初、猫はタワー 1(高さ 5)にいます。

高橋君がタワー 1,2,3,5,4 の順に取り除くと猫は次のように移動します。

  • 高橋君がタワー 1 を取り除く。タワー 1 から隣接するタワーへの移動のみで移動できるタワーは(タワー 1 を除いて)タワー 2,3,4,5 である。猫はこのうち最も高いタワー 3(高さ 4 )に移動する。
  • 高橋君がタワー 2 を取り除く。猫は移動しない。
  • 高橋君がタワー 3 を取り除く。タワー 3 から隣接するタワーへの移動のみで移動できるタワーは(タワー 3 を除いて)タワー 4,5 である。猫はこのうち最も高いタワー 5(高さ 2 )に移動する。
  • 高橋君がタワー 5 を取り除く。タワー 5 から隣接するタワーへの移動のみで移動できるタワーは(タワー 5 を除いて)タワー 4 のみである。猫はタワー 4(高さ 1 )に移動する。
  • 高橋君がタワー 4 を取り除く。猫は高橋君の腕に飛び込み、運動は終了する。

このとき、猫の移動距離の合計は \lvert 1-3\rvert+\lvert 3-5\rvert+\lvert 5-4\rvert=5 となります。 移動距離の合計を 6 以上にする取り除き方は存在しないため、5 を出力します。


入力例 2

3
1 3 2

出力例 2

1

最初、キャットタワーの高さは左から順に 1,3,2 です。
以下、左から i 番目 (1\leq i\leq 3) のキャットタワーをタワー i と呼びます。
最初、猫はタワー 2(高さ 3)にいます。

高橋君がタワー 2,3 の順に取り除くと猫は次のように移動します。

  • 高橋君がタワー 2 を取り除く。タワー 2 から隣接するタワーへの移動のみで移動できるタワーは(タワー 2 を除いて)タワー 1,3 である。猫はこのうち最も高いタワー 3(高さ 2 )に移動する。
  • 高橋君がタワー 3 を取り除く。猫は高橋君の腕に飛び込み、運動は終了する。

このとき、猫の移動距離の合計は \lvert 2-3\rvert=1 となり、このときが最大です。
タワー 2 を取り除いた後も、タワー 1 とタワー 3 は隣接しているとみなされないことに注意してください。

Score : 550 points

Problem Statement

There are N cat towers arranged in a row from left to right, and the height of the i-th tower from the left (1\leq i\leq N) is P_i.
Here, (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
The distance between the i-th tower and the j-th tower from the left is defined as \lvert i-j\rvert.

There is one cat, initially on top of the cat tower of height N.
Takahashi wants to exercise this cat by repeatedly choosing and removing towers.

When Takahashi removes a tower, the cat moves as follows:

  • If the cat is not on top of the tower being removed, the cat does not move.
  • If the cat is on top of the tower being removed and at least one of the towers adjacent to the left or right of that tower exists, the cat moves to the tallest tower (excluding the tower being removed) among the towers that can be reached by repeatedly moving to adjacent towers starting from the tower being removed. At this time, the cat moves the distance between the tower before movement and the tower after movement (the heights of the towers before, after, and during movement do not matter).
  • If the cat is on top of the tower being removed and no towers adjacent to the left or right of that tower exist, the cat jumps into Takahashi's arms, and the exercise ends here. In this case, no movement occurs.

Here, the spaces between removed towers are not filled in. That is, the towers adjacent to the i-th tower from the left initially are only the (i-1)-th and (i+1)-th towers from the left (if they exist), and they will never become adjacent to other towers due to the removal of other towers afterward.

Find the maximum possible total distance the cat moves before the exercise ends.

Constraints

  • 1\leq N\leq 2\times 10^5
  • (P_1,P_2,\ldots, P_N) is a permutation of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Output the maximum possible total distance the cat moves before the exercise ends.


Sample Input 1

5
5 3 4 1 2

Sample Output 1

5

Initially, the heights of the cat towers are 5,3,4,1,2 from left to right.
Hereafter, the i-th cat tower from the left (1\leq i\leq 5) is called tower i.
Initially, the cat is on tower 1 (height 5).

If Takahashi removes towers in the order 1,2,3,5,4, the cat moves as follows:

  • Takahashi removes tower 1. The towers that can be reached by only moving to adjacent towers from tower 1 are towers 2,3,4,5 (excluding tower 1). The cat moves to tower 3 (height 4), which is the tallest among them.
  • Takahashi removes tower 2. The cat does not move.
  • Takahashi removes tower 3. The towers that can be reached by only moving to adjacent towers from tower 3 are towers 4,5 (excluding tower 3). The cat moves to tower 5 (height 2), which is the tallest among them.
  • Takahashi removes tower 5. The towers that can be reached by only moving to adjacent towers from tower 5 are only tower 4 (excluding tower 5). The cat moves to tower 4 (height 1).
  • Takahashi removes tower 4. The cat jumps into Takahashi's arms, and the exercise ends.

Here, the cat moves the total distance of \lvert 1-3\rvert+\lvert 3-5\rvert+\lvert 5-4\rvert=5. There is no way to remove towers that makes the total distance moved 6 or more, so output 5.


Sample Input 2

3
1 3 2

Sample Output 2

1

Initially, the heights of the cat towers are 1,3,2 from left to right.
Hereafter, the i-th cat tower from the left (1\leq i\leq 3) is called tower i.
Initially, the cat is on tower 2 (height 3).

If Takahashi removes towers in the order 2,3, the cat moves as follows:

  • Takahashi removes tower 2. The towers that can be reached by only moving to adjacent towers from tower 2 are towers 1,3 (excluding tower 2). The cat moves to tower 3 (height 2), which is the tallest among them.
  • Takahashi removes tower 3. The cat jumps into Takahashi's arms, and the exercise ends.

Here, the cat moves the total distance of \lvert 2-3\rvert=1, which is the maximum.
Note that after removing tower 2, tower 1 and tower 3 are not considered to be adjacent.

G - Domino Arrangement

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

1 から N の番号がついた N 個のマスがあります。最初、どのマスも色が塗られていません。

M 種類の色があり、i 番目の色ではマス L_i,L_i+1,\ldots,R_i のうち好きなマスを好きな個数塗ることができます。

以下の条件を満たす色の塗り方は何通りあるか、998244353 で割った余りを求めてください。

  • どのマス i についても、そのマスに色が塗られているならば、マス i-1,i+1 のうちちょうど一方がマス i と同じ色で塗られている。
    • ただし、マス 0,N+1 はどの色にも塗られていないとみなす。

制約

  • 1\leq N,M \leq 5\times 10^5
  • 1\leq L_i \leq R_i \leq N
  • 入力は全て整数

入力

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

N M
L_1 R_1
\vdots
L_M R_M

出力

答えを出力せよ。


入力例 1

5 2
1 3
1 5

出力例 1

11

1 番目の色ではマス 1,2,3 を、2 番目の色ではマス 1,2,3,4,5 を塗ることができます。

条件を満たす塗り方は以下の 11 通りです。

図


入力例 2

3 3
1 1
2 2
3 3

出力例 2

1

どのマスにも色を塗らない 1 通りが条件を満たします。


入力例 3

500000 10
1 499999
2 499998
3 499997
4 499996
5 499995
6 499994
7 499993
8 499992
9 499991
10 499990

出力例 3

775503999

998244353 で割った余りを求めてください。

Score : 600 points

Problem Statement

There are N cells numbered from 1 to N. Initially, no cell is painted with any color.

There are M types of colors, and with the i-th color, you can paint any number of cells you like among cells L_i,L_i+1,\ldots,R_i.

Find the number, modulo 998244353, of ways to paint the cells that satisfy the following condition:

  • For every cell i, if that cell is painted with a color, then exactly one of cells i-1 and i+1 is painted with the same color as cell i.
    • Here, cells 0 and N+1 are considered to be not painted with any color.

Constraints

  • 1\leq N,M \leq 5\times 10^5
  • 1\leq L_i \leq R_i \leq N
  • All input values are integers.

Input

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

N M
L_1 R_1
\vdots
L_M R_M

Output

Output the answer.


Sample Input 1

5 2
1 3
1 5

Sample Output 1

11

The first color can paint cells 1,2,3, and the second color can paint cells 1,2,3,4,5.

There are 11 ways to paint that satisfy the condition, as follows:

Figure


Sample Input 2

3 3
1 1
2 2
3 3

Sample Output 2

1

The one way of not painting any cell satisfies the condition.


Sample Input 3

500000 10
1 499999
2 499998
3 499997
4 499996
5 499995
6 499994
7 499993
8 499992
9 499991
10 499990

Sample Output 3

775503999

Find the count modulo 998244353.