A - Distinct Strings

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字のみからなる長さ 3 の文字列 S が与えられます。

S の各文字を並び替えて得られる文字列は、何種類ありますか?

制約

  • S は英小文字のみからなる長さ 3 の文字列

入力

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

S

出力

S の各文字を並び替えて得られる文字列の種類数を出力せよ。


入力例 1

aba

出力例 1

3

S= aba の各文字を並び替えて得られる文字列は、aab, aba, baa3 通りです。


入力例 2

ccc

出力例 2

1

S= ccc の各文字を並び替えて得られる文字列は、ccc1 通りのみです。


入力例 3

xyz

出力例 3

6

S= xyz の各文字を並び替えて得られる文字列は、xyz, xzy, yxz, yzx, zxy, zyx6 通りです。

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of lowercase English letters.

How many different strings can be obtained by permuting the characters in S?

Constraints

  • S is a string S of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of different strings that can be obtained by permuting the characters in S.


Sample Input 1

aba

Sample Output 1

3

By permuting the characters in S= aba, three different strings can be obtained: aab, aba, baa.


Sample Input 2

ccc

Sample Output 2

1

By permuting the characters in S= ccc, just one string can be obtained: ccc.


Sample Input 3

xyz

Sample Output 3

6

By permuting the characters in S= xyz, six different strings can be obtained: xyz, xzy, yxz, yzx, zxy, zyx.

B - Star or Not

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 頂点 N-1 辺の木が与えられます。
頂点には 1,2,\ldots,N の番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

この木がスターであるか判定してください。

ただしスターとは、1 つの頂点から、他の全ての頂点に 1 本ずつ辺が出ている木のことです。

注記

「木」については、Wikipedia「木(数学)」 を参照してください。

制約

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 与えられるグラフは木である

入力

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

出力

与えられたグラフがスターであるなら Yes と、スターでないなら No と出力せよ。


入力例 1

5
1 4
2 4
3 4
4 5

出力例 1

Yes

与えられたグラフはスターです。


入力例 2

4
2 4
1 4
2 3

出力例 2

No

与えられたグラフはスターではありません。


入力例 3

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

出力例 3

Yes

Score : 200 points

Problem Statement

You are given a tree with N vertices and N-1 edges.
The vertices are numbered 1,2,\ldots,N. The i-th edge connects Vertex a_i and Vertex b_i.

Determine whether this tree is a star.

Here, a star is a tree where there is a vertex directly connected to all other vertices.

Notes

For the definition of a tree, see Tree (graph theory) - Wikipedia.

Constraints

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

Output

If the given graph is a star, print Yes; otherwise, print No.


Sample Input 1

5
1 4
2 4
3 4
4 5

Sample Output 1

Yes

The given graph is a star.


Sample Input 2

4
2 4
1 4
2 3

Sample Output 2

No

The given graph is not a star.


Sample Input 3

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

Sample Output 3

Yes
C - Calendar Validator

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

10^{100}7 列の行列 A があり、任意の整数対 (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7) についてその (i,j) 成分は (i-1) \times 7 + j です。

NM 列の行列 B が与えられるので、BA から一部の矩形領域を(向きを変えずに)切り出したものであるかを判定してください。

制約

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

出力

BA から一部の矩形領域を切り出したものであれば Yes と、そうでないなら No と出力せよ。


入力例 1

2 3
1 2 3
8 9 10

出力例 1

Yes

与えられる B は、A の左上 23 列を切り出したものとなっています。


入力例 2

2 1
1
2

出力例 2

No

与えられる B90 度回転させると A の左上 12 列と一致しますが、問題文中に「向きを変えずに」とある通り回転による一致は認められていないため、答えは No となります。


入力例 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

出力例 3

Yes

Score : 300 points

Problem Statement

There is a 10^{100} \times 7 matrix A, where the (i,j)-th entry is (i-1) \times 7 + j for every pair of integers (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7).

Given an N \times M matrix B, determine whether B is some (unrotated) rectangular part of A.

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

Output

If B is some rectangular part of A, print Yes; otherwise, print No.


Sample Input 1

2 3
1 2 3
8 9 10

Sample Output 1

Yes

The given matrix B is the top-left 2 \times 3 submatrix of A.


Sample Input 2

2 1
1
2

Sample Output 2

No

Although the given matrix B would match the top-left 1 \times 2 submatrix of A after rotating 90 degrees, the Problem Statement asks whether B is an unrotated part of A, so the answer is No.


Sample Input 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

Sample Output 3

Yes
D - Play Train

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は電車のおもちゃを連結させたり分離させたりして遊んでいます。
電車は N 個あり、電車 1 、電車 2\ldots 、電車 N と名前がついています。
はじめ電車どうしは連結しておらず全てバラバラです。

クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 x y :電車 x の後部と、電車 y の前部を連結させる。
    以下のことが保証されます。

    • x \neq y
    • クエリを処理する直前に、電車 x の後部と連結している電車は存在しない
    • クエリを処理する直前に、電車 y の前部と連結している電車は存在しない
    • クエリを処理する直前に、電車 x と電車 y は異なる連結成分に属する
  • 2 x y :電車 x の後部と、電車 y の前部を分離させる。
    以下のことが保証されます。

    • x \neq y
    • クエリを処理する直前に、電車 x の後部と電車 y の前部は直接連結している
  • 3 x :電車 x が含まれる連結成分に属する電車の番号を、先頭から順番に全て出力する。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq x \leq N
  • 1 \leq y \leq N
  • 入力は全て整数
  • クエリは全て問題文の条件を満たす
  • 3 x の形式のクエリで出力する電車の番号の個数の合計は 10^6 以下

入力

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

i 番目のクエリ \mathrm{query}_i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。
c_i = 1,2 の場合は x,y が追加で与えられ、c_i =3 の場合は x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x y
2 x y
3 x

出力

ある c_i = 3 のタイプのクエリにおいて、出力すべき値が j_1, j_2, \ldots , j_M であるとする。
このとき以下の形式で 1 行に出力せよ。

M j_1 j_2 \ldots j_M

c_i = 3 のタイプのクエリの数を q として、q 行出力せよ。
k (1 \leq k \leq q) 行目では k 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6

出力例 1

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

\mathrm{query}_5 まで処理した時、電車は以下のようになっています。
この時、たとえば電車 2 は、電車 3,5,6,7 と同じ連結成分に属していますが、電車 1,4 とは同じ連結成分に属していません。

\mathrm{query}_{11} まで処理した時、電車は以下のようになっています。

Score : 400 points

Problem Statement

Takahashi is playing with toy trains, connecting and disconnecting them.
There are N toy train cars, with car numbers: Car 1, Car 2, \ldots, Car N.
Initially, all cars are separated.

You will be given Q queries. Process them in the order they are given. There are three kinds of queries, as follows.

  • 1 x y: Connect the front of Car y to the rear of Car x.
    It is guaranteed that:

    • x \neq y
    • just before this query, no train is connected to the rear of Car x;
    • just before this query, no train is connected to the front of Car y;
    • just before this query, Car x and Car y belong to different connected components.
  • 2 x y: Disconnect the front of Car y from the rear of Car x.
    It is guaranteed that:

    • x \neq y;
    • just before this query, the front of Car y is directly connected to the rear of Car x.
  • 3 x: Print the car numbers of the cars belonging to the connected component containing Car x, from front to back.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq x \leq N
  • 1 \leq y \leq N
  • All values in input are integers.
  • All queries satisfy the conditions in the Problem Statement.
  • The queries of the format 3 x ask to print at most 10^6 car numbers in total.

Input

Input is given from Standard Input in the following format:

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

The i-th query \mathrm{query}_i begins with an integer c_i (1, 2, or 3) representing the kind of the query, followed by x and y if c_i = 1 or 2, and followed by x if c_i = 3.

In short, each query is in one of the following three formats:

1 x y
2 x y
3 x

Output

If a query with c_i = 3 asks to print the values j_1, j_2, \ldots, j_M, output the following line:

M j_1 j_2 \ldots j_M

Your output should consist of q lines, where q is the number of queries with c_i = 3.
The k-th line (1 \leq k \leq q) should contain the response to the k-th such query.


Sample Input 1

7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6

Sample Output 1

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

The figure below shows the cars when the first 5 queries are processed.
For example, Car 2 belongs to the same connected component as Cars 3, 5, 6, 7, which is different from the connected component containing Cars 1, 4.

The figure below shows the cars when the first 11 queries are processed.

E - 7

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

二次元平面上の第一象限上に N 個のフの字があります。

i\ (1 \leq i \leq N) 個目のフの字は、(x_i-1,y_i)(x_i,y_i) を結ぶ線分と (x_i,y_i-1)(x_i,y_i) を結ぶ線分の 2 つを組み合わせた図形です。

あなたは、N 個のフの字から 0 個以上を選び、削除することができます。

適切に削除するフの字を選んだとき、原点から全体が見えるフの字の個数は最大でいくつになりますか?

ここで、原点からあるフの字(便宜上 i 個目のフの字とする)の全体が見える必要十分条件は、以下の通りです。

  • 原点、(x_i-1,y_i)(x_i,y_i)(x_i,y_i-1)4 点を頂点とする四角形の内部(境界を除く)と他のフの字が共通部分を持たない。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq x_i,y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
  • 入力はすべて整数

入力

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

N
x_1 y_1
x_2 y_2
\hspace{0.45cm}\vdots
x_N y_N

出力

原点から全体が見えるフの字の個数の最大値を出力せよ。


入力例 1

3
1 1
2 1
1 2

出力例 1

2

1 個目のフの字を削除したとき原点からは 2 個目のフの字と 3 個目のフの字の 2 つが見えるようになり、これが最大です。

1 つのフの字も削除しない場合、原点からは 1 個目のフの字のみしか見えません。


入力例 2

10
414598724 87552841
252911401 309688555
623249116 421714323
605059493 227199170
410455266 373748111
861647548 916369023
527772558 682124751
356101507 249887028
292258775 110762985
850583108 796044319

出力例 2

10

すべてのフの字を削除せずに残すのが最善です。

Score : 500 points

Problem Statement

There are N 7's drawn in the first quadrant of a plane.

The i-th 7 is a figure composed of a segment connecting (x_i-1,y_i) and (x_i,y_i), and a segment connecting (x_i,y_i-1) and (x_i,y_i).

You can choose zero or more from the N 7's and delete them.

What is the maximum possible number of 7's that are wholly visible from the origin after the optimal deletion?

Here, the i-th 7 is wholly visible from the origin if and only if:

  • the interior (excluding borders) of the quadrilateral whose vertices are the origin, (x_i-1,y_i), (x_i,y_i), (x_i,y_i-1) does not intersect with the other 7's.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq x_i,y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
\hspace{0.45cm}\vdots
x_N y_N

Output

Print the maximum possible number of 7's that are wholly visible from the origin.


Sample Input 1

3
1 1
2 1
1 2

Sample Output 1

2

If the first 7 is deleted, the other two 7's ― the second and third ones ― will be wholly visible from the origin, which is optimal.

If no 7's are deleted, only the first 7 is wholly visible from the origin.


Sample Input 2

10
414598724 87552841
252911401 309688555
623249116 421714323
605059493 227199170
410455266 373748111
861647548 916369023
527772558 682124751
356101507 249887028
292258775 110762985
850583108 796044319

Sample Output 2

10

It is best to keep all 7's.

F - String Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

カードが N 枚あり、i 番目のカードには文字列 S_i が書かれています。

この中からちょうど K 枚選び、好きな順序で繋げてできる文字列のうち辞書順最小のものを求めてください。

制約

  • 1 \leq K \leq N \leq 50
  • 1 \leq |S_i| \leq 50
  • S_i は英小文字からなる

入力

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

N K
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

4 3
ode
zaaa
r
atc

出力例 1

atcoder

カードの中に書かれている文字を、反転させたり並び替えたりすることはできません。
たとえば 1 枚目のカードに書かれている ode を、edodeo のように使うことはできません。


入力例 2

5 2
z
z
zzz
z
zzzzzz

出力例 2

zz

S_i = S_j を満たす i,j(i\neq j) の組が存在することもあります。

Score : 500 points

Problem Statement

We have N cards. The i-th card has a string S_i written on it.

Find the lexicographically smallest string that can be obtained by choosing K of these cards and concatenating them in any order.

Constraints

  • 1 \leq K \leq N \leq 50
  • 1 \leq |S_i| \leq 50
  • S_i consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N K
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

4 3
ode
zaaa
r
atc

Sample Output 1

atcoder

Note that it is not possible to reverse or permute the string written on a card.
For example, ode written on the first card cannot be used as edo or deo.


Sample Input 2

5 2
z
z
zzz
z
zzzzzz

Sample Output 2

zz

There may be a pair i, j (i\neq j) such that S_i = S_j.

G - X

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

H 行、横 W 列のグリッドがあります。各マスには整数が書かれており、上から i 行目、左から j 列目のマス (i,j) には A_{i,j} が書かれています。

これから高橋くんが、H \times W 個あるマスから 0 個以上を選び、バツ印を付けます。1 つのバツ印は、書かれるマスの左上の角と右下の角を結ぶ線分、および右上の角と左下の角を結ぶ線分の 2 本からなります。

高橋くんのスコアを、(バツ印を付けられたマスに書かれた整数の総和)- C \times (バツ印を書くために必要な線分の本数の最小値) と定義しましょう。

ここで、高橋くんは斜めに隣接するマスのバツ印を続けて書くことができます。

例えば、マス (1,1) とマス (2,2) にバツ印を付けるとき、高橋くんは

  • マス (1,1) の左上の角とマス (2,2) の右下の角を結ぶ 1 本の線分
  • マス (1,1) の右上の角とマス (1,1) の左下の角を結ぶ 1 本の線分
  • マス (2,2) の右上の角とマス (2,2) の左下の角を結ぶ 1 本の線分

の計 3 本によってバツ印を書くことができます。

高橋くんのスコアの最大値を求めてください。なお、バツ印を付けないマスには何も書いてはいけないことに注意してください。

制約

  • 1 \leq H,W \leq 100
  • 1 \leq C \leq 10^9
  • 1 \leq A_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

H W C
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\hspace{1.5cm}\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

高橋くんのスコアの最大値を出力せよ。


入力例 1

2 2 2
2 10
8 3

出力例 1

12

マス (1,2) とマス (2,1) にバツ印を付ける場合、高橋くんは

  • マス (1,2) の左上の角とマス (1,2) の右下の角を結ぶ 1 本の線分
  • マス (2,1) の左上の角とマス (2,1) の右下の角を結ぶ 1 本の線分
  • マス (1,2) の右上の角とマス (2,1) の左下の角を結ぶ 1 本の線分

の計 3 本によってバツ印を書くことができます。故にこの場合の高橋くんのスコアは 10+8-2 \times 3=12 です。

これよりも真にスコアが大きくなるバツ印の付け方は存在しないため、答えは 12 となります。


入力例 2

3 3 100
1 1 1
1 1 1
1 1 1

出力例 2

0

どのマスにもバツ印を付けないのが最善です。


入力例 3

8 9 970861213
1313462 943495812 203775264 839015475 115668311 14701110 819458175 827176922 236492592
843915104 786367010 344840288 618248834 824858165 549189141 120648070 805825275 933750119
709330492 38579914 890555497 75314343 238373458 854061807 637519536 53226153 627677130
671706386 380984116 221773266 787763728 639374738 298691145 359138139 183373508 524415106
716502263 150803008 390520954 913021901 553285119 876389099 952721235 46809105 635239775
355621458 511843148 117663063 37274476 891025941 832254337 346436418 783134705 488516288
383723241 322408013 948364423 409068145 120813872 697127655 968230339 988041557 222591780
712959990 233114128 210373172 798667159 568746366 579461421 923556823 777007925 422249456

出力例 3

9785518299

Score : 600 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Each square contains an integer. The square (i, j) at the i-th row from the top and j-th column from the left contains the integer A_{i,j}.

Takahashi will choose zero or more from the H \times W squares and draw an X on each of them. An X is composed of a segment connecting the top-left and bottom-right corners, and a segment connecting the top-right and bottom-left corners.

Let us define Takahashi's score as (the sum of integers contained in the squares on which X is drawn) - C \times (the minimum number of segments needed to draw the X's).

Here, Takahashi can draw X's on diagonally adjacent squares at once.

For example, he can draw X's on the squares (1, 1) and (2, 2) with three segments:

  • a segment connecting the top-left corner of (1, 1) and the bottom-right corner of (2, 2),
  • a segment connecting the top-right corner of (1, 1) and the bottom-left corner of (1, 1),
  • a segment connecting the top-right corner of (2, 2) and the bottom-left corner of (2, 2).

Find Takahashi's maximum possible score. Note that nothing should be drawn on unchosen squares.

Constraints

  • 1 \leq H,W \leq 100
  • 1 \leq C \leq 10^9
  • 1 \leq A_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W C
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\hspace{1.5cm}\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

Output

Print Takahashi's maximum possible score.


Sample Input 1

2 2 2
2 10
8 3

Sample Output 1

12

If he chooses the squares (1,2) and (2,1), he can draw X's on them with three segments:

  • a segment connecting the top-left corner of (1, 2) and the bottom-right corner of (1, 2),
  • a segment connecting the top-left corner of (2, 1) and the bottom-right corner of (2, 1),
  • a segment connecting the top-right corner of (1, 2) and the bottom-left corner of (2, 1).

Thus, Takahashi's score here will be 10+8-2 \times 3=12.

There is no way to mark squares that achieves a strictly higher score, so the answer is 12.


Sample Input 2

3 3 100
1 1 1
1 1 1
1 1 1

Sample Output 2

0

It is best to mark no squares.


Sample Input 3

8 9 970861213
1313462 943495812 203775264 839015475 115668311 14701110 819458175 827176922 236492592
843915104 786367010 344840288 618248834 824858165 549189141 120648070 805825275 933750119
709330492 38579914 890555497 75314343 238373458 854061807 637519536 53226153 627677130
671706386 380984116 221773266 787763728 639374738 298691145 359138139 183373508 524415106
716502263 150803008 390520954 913021901 553285119 876389099 952721235 46809105 635239775
355621458 511843148 117663063 37274476 891025941 832254337 346436418 783134705 488516288
383723241 322408013 948364423 409068145 120813872 697127655 968230339 988041557 222591780
712959990 233114128 210373172 798667159 568746366 579461421 923556823 777007925 422249456

Sample Output 3

9785518299
H - Social Distance 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 列に椅子が N 個並んでおり、椅子 1 、椅子 2\ldots 、椅子 N と名前がついています。
1 つの椅子に 2 人以上が座ることはできません。

M 人が椅子に座りますが、座り方によって以下の式で与えられるスコアが定められます。

人が座っている椅子の番号を昇順にソートした数列を B=(B_1,B_2,\ldots,B_M) として、
\displaystyle \prod_{i=1}^{M-1} (B_{i+1} - B_i)

i (1 \leq i \leq K) は既に椅子 A_i に座っています。
残りの M-K 人の座り方は {} _ {N-K} \mathrm{P} _ {M-K} 通りありますが、座り方全てについてスコアの和を取るといくつになりますか?

答えは非常に大きくなる可能性があるので、998244353 で割った余りを求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq N
  • 0 \leq K \leq M
  • 1 \leq A_1 \lt A_2 \lt \ldots \lt A_K \leq N
  • 入力は全て整数

入力

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

N M K
A_1 A_2 \ldots A_K

出力

答えを出力せよ。


入力例 1

5 3 2
1 3

出力例 1

7

3 が椅子 2 に座った時のスコアは、(2-1) \times (3-2)=1 \times 1 = 1 です。
3 が椅子 4 に座った時のスコアは、(3-1) \times (4-3)=2 \times 1 = 2 です。
3 が椅子 5 に座った時のスコアは、(3-1) \times (5-3)=2 \times 2 = 4 です。
答えは 1+2+4=7 です。


入力例 2

6 6 1
4

出力例 2

120

全ての座り方でスコアは 1 です。
座り方は {} _ {5} \mathrm{P} _ {5} = 120 通りあるので、答えは 120 です。


入力例 3

99 10 3
10 50 90

出力例 3

761621047

Score : 600 points

Problem Statement

There are N chairs arranged in a row, called Chair 1, Chair 2, \ldots, Chair N.
A chair seats only one person.

M people will sit on M of these chairs. Here, let us define the score as follows:

\displaystyle \prod_{i=1}^{M-1} (B_{i+1} - B_i), where B=(B_1,B_2,\ldots,B_M) is the sorted list of the indices of the chairs the people sit on.

Person i (1 \leq i \leq K) is already sitting on Chair A_i.
There are {} _ {N-K} \mathrm{P} _ {M-K} ways for the other M-K people to take seats. Find the sum of the scores for all of these ways.

Since this sum may be enormous, compute it modulo 998244353.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq N
  • 0 \leq K \leq M
  • 1 \leq A_1 \lt A_2 \lt \ldots \lt A_K \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_K

Output

Print the answer.


Sample Input 1

5 3 2
1 3

Sample Output 1

7

If Person 3 sits on Chair 2, the score will be (2-1) \times (3-2)=1 \times 1 = 1.
If Person 3 sits on Chair 4, the score will be (3-1) \times (4-3)=2 \times 1 = 2.
If Person 3 sits on Chair 5, the score will be (3-1) \times (5-3)=2 \times 2 = 4.
The answer is 1+2+4=7.


Sample Input 2

6 6 1
4

Sample Output 2

120

The score for every way of sitting will be 1.
There are {} _ {5} \mathrm{P} _ {5} = 120 ways of sitting, so the answer is 120.


Sample Input 3

99 10 3
10 50 90

Sample Output 3

761621047