A - 321-like Checker

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

配点 : 100

問題文

以下の条件を満たす正整数 x321-like Number と呼びます。

  • x の各桁を上から見ると狭義単調減少になっている。
  • すなわち、xd 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
    • ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )

なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。

例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。

N が入力として与えられるので、 N が 321-like Number なら Yes 、そうでないなら No と出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 99999

入力

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

N

出力

N が 321-like Number なら Yes 、そうでないなら No と出力せよ。


入力例 1

321

出力例 1

Yes

N=321 に対して、以下が成り立ちます。

  • 上から 1 桁目の 3 は上から 2 桁目の 2 より大きい。
  • 上から 2 桁目の 2 は上から 3 桁目の 1 より大きい。

以上より、 321 は 321-like Number です。


入力例 2

123

出力例 2

No

N=123 について、例えば以下が成り立ちます。

  • 上から 1 桁目の 1 は上から 2 桁目の 2 より大きくない。

このことから、 123 は 321-like Number ではありません。


入力例 3

1

出力例 3

Yes

入力例 4

86411

出力例 4

No

Score : 100 points

Problem Statement

A positive integer x is called a 321-like Number when it satisfies the following condition.

  • The digits of x are strictly decreasing from top to bottom.
  • In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
    • (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).

Note that all one-digit positive integers are 321-like Numbers.

For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.

You are given N as input. Print Yes if N is a 321-like Number, and No otherwise.

Constraints

  • All input values are integers.
  • 1 \le N \le 99999

Input

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

N

Output

Print Yes if N is a 321-like Number, and No otherwise.


Sample Input 1

321

Sample Output 1

Yes

For N=321, the following holds:

  • The first digit from the top, 3, is greater than the second digit from the top, 2.
  • The second digit from the top, 2, is greater than the third digit from the top, 1.

Thus, 321 is a 321-like Number.


Sample Input 2

123

Sample Output 2

No

For N=123, the following holds:

  • The first digit from the top, 1, is not greater than the second digit from the top, 2.

Thus, 123 is not a 321-like Number.


Sample Input 3

1

Sample Output 3

Yes

Sample Input 4

86411

Sample Output 4

No
B - Cutoff

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

配点 : 200

問題文

以下の手順で行われる試験があります。

  • 試験は 1 ラウンド目から N ラウンド目までの N ラウンドからなる。
  • 各ラウンドに対し、 0 以上 100 以下の整数でスコアが与えられる。
  • N ラウンドのスコアのうち、最高スコアと最低スコアを除いた N-2 ラウンドのスコアの合計が最終結果となる。
    • 厳密には、各ラウンドのスコアを昇順に並べた列を S=(S_1,S_2,\dots,S_N) としたとき、最終結果は S_2+S_3+\dots+S_{N-1} となる。

現在、試験のうち N-1 ラウンドが終了し、 i ラウンド目のスコアは A_i でした。
最終結果を X 以上とするために N ラウンド目に取るべきスコアの最小値を出力してください。
但し、 N ラウンド目にどのようなスコアを取っても最終結果が X 以上にならない場合、代わりに -1 と出力してください。
なお、 N ラウンド目に取りうるスコアは 0 以上 100 以下の整数であることに注意してください。

制約

  • 入力は全て整数
  • 3 \le N \le 100
  • 0 \le X \le 100 \times (N-2)
  • 0 \le A_i \le 100

入力

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

N X
A_1 A_2 \dots A_{N-1}

出力

答えを出力せよ。


入力例 1

5 180
40 60 80 50

出力例 1

70

4 ラウンド目までのスコアは 40,60,80,50 でした。
5 ラウンド目にスコア 70 を取ると、スコアを昇順に並べた列は S=(40,50,60,70,80) となり、最終結果は 50+60+70=180 となります。
なお、最終結果を 180 以上にするために取るべきスコアの最小値が 70 であることが示せます。


入力例 2

3 100
100 100

出力例 2

0

2 ラウンド目までのスコアは 100,100 でした。
3 ラウンド目にスコア 0 を取ると、スコアを昇順に並べた列は S=(0,100,100) となり、最終結果は 100 となります。
最大スコアである 100 が複数ありますが、そのうち 1 つしか除かれないことに注意してください。(最小スコアについても同様です)
なお、最終結果を 100 以上にするために取るべきスコアの最小値が 0 であることが示せます。


入力例 3

5 200
0 0 99 99

出力例 3

-1

4 ラウンド目までのスコアは 0,0,99,99 でした。
5 ラウンド目にどのようなスコアを取っても、最終結果を 200 以上にすることができないことが示せます。


入力例 4

10 480
59 98 88 54 70 24 8 94 46

出力例 4

45

Score : 200 points

Problem Statement

There is an exam structured as follows.

  • The exam consists of N rounds called round 1 to N.
  • In each round, you are given an integer score between 0 and 100, inclusive.
  • Your final grade is the sum of the N-2 of the scores earned in the rounds excluding the highest and lowest.
    • Formally, let S=(S_1,S_2,\dots,S_N) be the sequence of the scores earned in the rounds sorted in ascending order, then the final grade is S_2+S_3+\dots+S_{N-1}.

Now, N-1 rounds of the exam have ended, and your score in round i was A_i.
Print the minimum score you must earn in round N for a final grade of X or higher.
If your final grade will never be X or higher no matter what score you earn in round N, print -1 instead.
Note that your score in round N can only be an integer between 0 and 100.

Constraints

  • All input values are integers.
  • 3 \le N \le 100
  • 0 \le X \le 100 \times (N-2)
  • 0 \le A_i \le 100

Input

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

N X
A_1 A_2 \dots A_{N-1}

Output

Print the answer.


Sample Input 1

5 180
40 60 80 50

Sample Output 1

70

Your scores in the first four rounds were 40, 60, 80, and 50.
If you earn a score of 70 in round 5, the sequence of the scores sorted in ascending order will be S=(40,50,60,70,80), for a final grade of 50+60+70=180.
It can be shown that 70 is the minimum score you must earn for a final grade of 180 or higher.


Sample Input 2

3 100
100 100

Sample Output 2

0

Your scores in the first two rounds were 100 and 100.
If you earn a score of 0 in round 3, the sequence of the scores sorted in ascending order will be S=(0,100,100), for a final grade of 100.
Note that the highest score, 100, is earned multiple times, and only one of them is excluded. (The same goes for the lowest score.)
It can be shown that 0 is the minimum score you must earn for a final grade of 100 or higher.


Sample Input 3

5 200
0 0 99 99

Sample Output 3

-1

Your scores in the first four rounds were 0, 0, 99, and 99.
It can be shown that your final grade will never be 200 or higher no matter what score you earn in round 5.


Sample Input 4

10 480
59 98 88 54 70 24 8 94 46

Sample Output 4

45
C - 321-like Searcher

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

配点 : 300

問題文

以下の条件を満たす正整数 x321-like Number と呼びます。 この定義は A 問題と同様です。

  • x の各桁を上から見ると狭義単調減少になっている。
  • すなわち、xd 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
    • ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )

なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。

例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。

K 番目に小さい 321-like Number を求めてください。

制約

  • 入力は全て整数
  • 1 \le K
  • 321-like Number は K 個以上存在する

入力

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

K

出力

K 番目に小さい 321-like Number を整数として出力せよ。


入力例 1

15

出力例 1

32

321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。


入力例 2

321

出力例 2

9610

入力例 3

777

出力例 3

983210

Score : 300 points

Problem Statement

A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.

  • The digits of x are strictly decreasing from top to bottom.
  • In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
    • (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).

Note that all one-digit positive integers are 321-like Numbers.

For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.

Find the K-th smallest 321-like Number.

Constraints

  • All input values are integers.
  • 1 \le K
  • At least K 321-like Numbers exist.

Input

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

K

Output

Print the K-th smallest 321-like Number as an integer.


Sample Input 1

15

Sample Output 1

32

The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.


Sample Input 2

321

Sample Output 2

9610

Sample Input 3

777

Sample Output 3

983210
D - Set Menu

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

配点 : 400

問題文

AtCoder 食堂では N 種類の主菜と M 種類の副菜が提供されており、i 種類目の主菜の価格は A_ij 種類目の副菜の価格は B_j です。 AtCoder 食堂では新たに定食のメニューを設けることを検討しています。 定食は 1 種類の主菜と 1 種類の副菜から構成され、主菜と副菜の価格の和を s としたとき、定食の価格は \min(s,P) です。 ここで、P は入力で与えられる定数です。

定食を構成する主菜と副菜の選び方は NM 通りありますが、それらすべてに対する定食の価格の総和を求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • 入力は全て整数

入力

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

答えを整数として出力せよ。 なお、本問題の制約下では、答えは 64 bit 符号付き整数型の範囲に収まることが証明できる。


入力例 1

2 2 7
3 5
6 1

出力例 1

24
  • 1 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(3+6,7)=7 です。
  • 1 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(3+1,7)=4 です。
  • 2 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(5+6,7)=7 です。
  • 2 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(5+1,7)=6 です。

よって、答えは 7+4+7+6=24 です。


入力例 2

1 3 2
1
1 1 1

出力例 2

6

入力例 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

出力例 3

2115597124

Score : 400 points

Problem Statement

AtCoder cafeteria offers N main dishes and M side dishes. The price of the i-th main dish is A_i, and that of the j-th side dish is B_j. The cafeteria is considering introducing a new set meal menu. A set meal consists of one main dish and one side dish. Let s be the sum of the prices of the main dish and the side dish, then the price of the set meal is \min(s,P). Here, P is a constant given in the input.

There are NM ways to choose a main dish and a side dish for a set meal. Find the total price of all these set meals.

Constraints

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • All input values are integers.

Input

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print the answer as an integer. Under the constraints of this problem, it can be proved that the answer fits into a 64-bit signed integer.


Sample Input 1

2 2 7
3 5
6 1

Sample Output 1

24
  • If you choose the first main dish and the first side dish, the price of the set meal is \min(3+6,7)=7.
  • If you choose the first main dish and the second side dish, the price of the set meal is \min(3+1,7)=4.
  • If you choose the second main dish and the first side dish, the price of the set meal is \min(5+6,7)=7.
  • If you choose the second main dish and the second side dish, the price of the set meal is \min(5+1,7)=6.

Thus, the answer is 7+4+7+6=24.


Sample Input 2

1 3 2
1
1 1 1

Sample Output 2

6

Sample Input 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

Sample Output 3

2115597124
E - Complete Binary Tree

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

配点 : 450

問題文

1 から N までの番号が付けられた N 頂点からなる木があります。 各 i\ (2 \leq i \leq N) について、頂点 i と頂点 \lfloor \frac{i}{2} \rfloor を結ぶ辺が張られています。 逆に、これら以外の辺は存在しません。

この木において、頂点 X との距離が K である頂点の数を求めてください。 ただし、2 頂点 u,v の距離は、頂点 u,v を結ぶ単純パスに含まれる辺の個数として定義されます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。 ここで、\mathrm{test}_ii 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N X K 

出力

T 行出力せよ。

i\ (1 \leq i \leq T) 行目には、i 番目のテストケースに対する答えを整数として出力せよ。


入力例 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

出力例 1

1
3
4
2
0

N=10 のとき、木は以下の図のようになります。

このとき、

  • 頂点 2 との距離が 0 である頂点は 21 つです。
  • 頂点 2 との距離が 1 である頂点は 1,4,53 つです。
  • 頂点 2 との距離が 2 である頂点は 3,8,9,104 つです。
  • 頂点 2 との距離が 3 である頂点は 6,72 つです。
  • 頂点 2 との距離が 4 である頂点は存在しません。

入力例 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

出力例 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976

Score : 450 points

Problem Statement

There is a tree with N vertices numbered 1 to N. For each i\ (2 \leq i \leq N), there is an edge connecting vertex i and vertex \lfloor \frac{i}{2} \rfloor. There are no other edges.

In this tree, find the number of vertices whose distance from vertex X is K. Here, the distance between two vertices u and v is defined as the number of edges in the simple path connecting vertices u and v.

You have T test cases to solve.

Constraints

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i represents the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N X K 

Output

Print T lines.

The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case as an integer.


Sample Input 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

Sample Output 1

1
3
4
2
0

The tree for N=10 is shown in the following figure.

Here,

  • There is 1 vertex, 2, whose distance from vertex 2 is 0.
  • There are 3 vertices, 1,4,5, whose distance from vertex 2 is 1.
  • There are 4 vertices, 3,8,9,10, whose distance from vertex 2 is 2.
  • There are 2 vertices, 6,7, whose distance from vertex 2 is 3.
  • There are no vertices whose distance from vertex 2 is 4.

Sample Input 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

Sample Output 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976
F - #(subset sum = K) with Add and Erase

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

配点 : 525

問題文

箱を用意します。最初、箱は空です。
この箱に対して、以下の 2 種類の操作を合計 Q 個、入力で与えられた順に施します。

+ x

タイプ 1 : 箱の中に整数 x の書かれたボールを 1 つ追加する。

- x

タイプ 2 : 箱の中にある、整数 x の書かれたボールを 1 つ取り除く。
但し、取り除く前の時点で箱の中に整数 x が書かれたボールが含まれることが保証されます。


各操作後の箱に関して、以下の問題を解いてください。

箱からボールをいくつか取り出して、ボールに書かれた整数の総和を K とする方法の総数を 998244353 で割った余りを求めてください。
但し、箱の中に入っている全てのボールは区別可能です。

制約

  • 入力は全て整数
  • 1 \le Q \le 5000
  • 1 \le K \le 5000
  • タイプ 1 の操作に関して、 1 \le x \le 5000
  • 全ての操作は問題文中の条件を満たす。

入力

入力は以下の形式で標準入力から与えられる。
但し、 \rm{Query}_{i}i 個目の操作を表す。

Q K
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}

出力

全体で Q 行出力せよ。
そのうち i 行目には、 i 個目までの操作を終えた時点での答えを出力せよ。


入力例 1

15 10
+ 5
+ 2
+ 3
- 2
+ 5
+ 10
- 3
+ 1
+ 3
+ 3
- 5
+ 1
+ 7
+ 4
- 3

出力例 1

0
0
1
0
1
2
2
2
2
2
1
3
5
8
5

この入力には、操作が 15 個含まれます。

最後の操作の後、箱の中に入ったボールは (5,10,1,3,1,7,4) となります。
総和を 10 にする方法は以下の 5 通りです。

  • 5+1+3+1 ( 1,3,4,5 番目のボールを取り出す )
  • 5+1+4 ( 1,3,7 番目のボールを取り出す )
  • 5+1+4 ( 1,5,7 番目のボールを取り出す )
  • 10 ( 2 番目のボールを取り出す )
  • 3+7 ( 4,6 番目のボールを取り出す )

Score : 525 points

Problem Statement

We have a box, which is initially empty.
Let us perform a total of Q operations of the following two types in the order they are given in the input.

+ x

Type 1: Put into the box a ball with the integer x written on it.

- x

Type 2: Remove from the box a ball with the integer x written on it.
It is guaranteed that the box contains a ball with the integer x written on it before the operation.


For the box after each operation, solve the following problem.

Find the number, modulo 998244353, to pick up some number of balls from the box so that the integers written on them sum to K.
All balls in the box are distinguishable.

Constraints

  • All input values are integers.
  • 1 \le Q \le 5000
  • 1 \le K \le 5000
  • For each type-1 operation, 1 \le x \le 5000.
  • All the operations satisfy the condition in the problem statement.

Input

The input is given from Standard Input in the following format, where \mathrm{Query}_i represents the i-th operation:

Q K
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}

Output

Print Q lines.
The i-th line should contain the answer when the first i operations have been done.


Sample Input 1

15 10
+ 5
+ 2
+ 3
- 2
+ 5
+ 10
- 3
+ 1
+ 3
+ 3
- 5
+ 1
+ 7
+ 4
- 3

Sample Output 1

0
0
1
0
1
2
2
2
2
2
1
3
5
8
5

This input contains 15 operations.

After the last operation, the box contains the balls (5,10,1,3,1,7,4).
There are five ways to pick up balls for a sum of 10:

  • 5+1+3+1 (the 1-st, 3-rd, 4-th, 5-th balls)
  • 5+1+4 (the 1-st, 3-rd, 7-th balls)
  • 5+1+4 (the 1-st, 5-th, 7-th balls)
  • 10 (the 2-nd ball)
  • 3+7 (the 4-th, 6-th balls)
G - Electric Circuit

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

配点 : 600

問題文

1 から N までの番号が付けられた N 個の部品と M 本のケーブルを使って電気回路を作ろうとしています。 これらの部品には赤い端子と青い端子がそれぞれ合計で M 個ずつ存在し、i 個目の赤い端子は部品 R_i に、i 個目の青い端子は部品 B_i についています。 各ケーブルは赤い端子 1 つと青い端子 1 つを繋ぎます。 特に、同じ部品についた 2 つの端子を繋ぐことも許されます。 また、1 つの端子に対して 2 本以上のケーブルを繋げることはできません。 したがって、M 本のケーブルの繋ぎ方は全部で M! 通りあります(ケーブル同士は区別しないことに注意してください)。

部品を頂点、ケーブルを辺としたグラフとしてこの回路を見たときの連結成分数を s とします。 M 本のケーブルの繋ぎ方を M! 通りからランダムに選ぶときの s の期待値を \text{mod } 998244353 で求めてください。

期待値を \text{mod } 998244353 で求めるとは 求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。 この R を求めてください。

制約

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

入力

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

N M
R_1 R_2 \dots R_M
B_1 B_2 \dots B_M

出力

s の期待値を \text{mod } 998244353 で出力せよ。


入力例 1

3 2
1 2
3 2

出力例 1

499122178

i 個目の赤い端子と j 個目の青い端子をケーブルで繋ぐことを (i, j) と表記します。

  • (1,1), (2,2) の場合:\lbrace 1,3 \rbrace\lbrace 2 \rbrace という 2 つの連結成分ができるので、s=2 です。
  • (1,2), (2,1) の場合:全体が 1 つの連結成分になるので、s=1 です。

よって、s の期待値は \frac{3}{2} \equiv 499122178 \pmod {998244353} です。


入力例 2

17 5
1 1 1 1 1
1 1 1 1 1

出力例 2

17

どのように繋いでも s=N になります。


入力例 3

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

出力例 3

608849831

Score : 600 points

Problem Statement

You are creating an electrical circuit using N parts numbered 1 to N and M cables. These parts have a total of M red terminals and M blue terminals, with the i-th red terminal attached to part R_i and the i-th blue terminal attached to part B_i. Each cable connects one red terminal and one blue terminal. In particular, it is allowed to connect two terminals attached to the same part. You cannot connect two or more cables to a terminal. Therefore, there are M! ways in total to connect the M cables (the cables are not distinguished from each other).

Let s be the number of connected components when seeing this circuit as a graph with parts as vertices and cables as edges. Find the expected value, modulo 998244353, of s when randomly choosing the way to connect the M cables out of the M! ways.

What does it mean to find an expected value modulo 998244353? It can be proved that the sought expected value is always a rational number. Also, under the constraints of this problem, it can be proved that when this value is expressed as \frac{P}{Q} using two coprime integers P and Q, there is exactly one integer R that satisfies R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

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

Input

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

N M
R_1 R_2 \dots R_M
B_1 B_2 \dots B_M

Output

Print the expected value of s, modulo 998244353.


Sample Input 1

3 2
1 2
3 2

Sample Output 1

499122178

Let (i, j) denote the action of connecting the i-th red terminal and the j-th blue terminal with a cable.

  • For (1,1), (2,2): There will be two connected components, \lbrace 1,3 \rbrace and \lbrace 2 \rbrace, so s=2.
  • For (1,2), (2,1): The whole graph constitutes one connected component, so s=1.

Thus, the expected value of s is \frac{3}{2} \equiv 499122178 \pmod {998244353}.


Sample Input 2

17 5
1 1 1 1 1
1 1 1 1 1

Sample Output 2

17

s=N no matter how you connect the cables.


Sample Input 3

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

Sample Output 3

608849831