A - Secret Numbers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字と数字のみからなる文字列 S が与えられます。

S から数字である文字だけを取り出し、元の順序のまま並べた文字列を求めてください。

制約

  • S は英小文字と数字のみからなる長さ 1 以上 50 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abc462

出力例 1

462

abc462 に含まれる数字は 4, 6, 2 です。これらを元の順序のまま連結すると 462 となります。したがって、462 を出力してください。


入力例 2

codequeen

出力例 2


codequeen に数字は含まれないので空文字列を出力してください。


入力例 3

31415

出力例 3

31415

入力例 4

10plus2is12

出力例 4

10212

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and digits.

Extract only the digit characters from S and output the string formed by arranging them in their original order.

Constraints

  • S is a string of length between 1 and 50, inclusive, consisting of lowercase English letters and digits.

Input

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

S

Output

Output the answer.


Sample Input 1

abc462

Sample Output 1

462

The digits contained in abc462 are 4, 6, 2. Concatenating them in their original order gives 462. Thus, output 462.


Sample Input 2

codequeen

Sample Output 2


codequeen contains no digits, so output the empty string.


Sample Input 3

31415

Sample Output 3

31415

Sample Input 4

10plus2is12

Sample Output 4

10212
B - Gift

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1 から人 NN 人がギフトを送り合いました。

i は人 A_{i,1},A_{i,2},\ldots,A_{i,K_i}K_i 人にギフトを送りました。

i=1,2,\ldots,N に対し、人 i にギフトを送った人を全て求めてください。

制約

  • 2\le N\le 100
  • 1\le K_i\le N-1
  • 1\le A_{i,1} < A_{i,2} < \cdots < A_{i,K_i}\le N
  • A_{i,j} \neq i
  • 入力される値は全て整数

入力

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

N
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

出力

N 行出力せよ。

i 行目には、人 i にギフトを送った人の番号を昇順に B_1,B_2,\ldots,B_{X} (ただし X は人 i にギフトを送った人数)としたとき、以下の形式で出力せよ。

X B_1 B_2 \ldots B_X

入力例 1

4
1 2
1 3
1 2
3 1 2 3

出力例 1

1 4
3 1 3 4
2 2 4
0

1 は人 2 に、人 2 は人 3 に、人 3 は人 2 に、人 4 は人 1,2,3 にギフトを送りました。

1 は人 4 からギフトを送られました。したがって、1 行目には 1 4 を出力してください。

2 は人 1,3,4 からギフトを送られました。したがって、2 行目には 3 1 3 4 を出力してください。

3 は人 2,4 からギフトを送られました。したがって、3 行目には 2 2 4 を出力してください。

4 は誰からもギフトを送られませんでした。したがって、4 行目には 0 を出力してください。


入力例 2

4
3 2 3 4
2 1 4
2 1 2
2 2 3

出力例 2

2 2 3
3 1 3 4
2 1 4
2 1 2

入力例 3

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

出力例 3

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

Score : 200 points

Problem Statement

N people, numbered 1 through N, exchanged gifts with each other.

Person i sent gifts to K_i people: persons A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.

For each i = 1, 2, \ldots, N, find all people who sent a gift to person i.

Constraints

  • 2\le N\le 100
  • 1\le K_i\le N-1
  • 1\le A_{i,1} < A_{i,2} < \cdots < A_{i,K_i}\le N
  • A_{i,j} \neq i
  • All input values are integers.

Input

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

N
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

Output

Output N lines.

For the i-th line, let B_1, B_2, \ldots, B_X be the numbers of the people who sent a gift to person i, listed in ascending order (where X is the count of people who sent a gift to person i), and output in the following format:

X B_1 B_2 \ldots B_X

Sample Input 1

4
1 2
1 3
1 2
3 1 2 3

Sample Output 1

1 4
3 1 3 4
2 2 4
0

Person 1 sent a gift to person 2, person 2 sent a gift to person 3, person 3 sent a gift to person 2, and person 4 sent gifts to persons 1, 2, 3.

Person 1 received a gift from person 4. Thus, output 1 4 on the first line.

Person 2 received gifts from persons 1, 3, 4. Thus, output 3 1 3 4 on the second line.

Person 3 received gifts from persons 2, 4. Thus, output 2 2 4 on the third line.

Person 4 did not receive any gifts. Thus, output 0 on the fourth line.


Sample Input 2

4
3 2 3 4
2 1 4
2 1 2
2 2 3

Sample Output 2

2 2 3
3 1 3 4
2 1 4
2 1 2

Sample Input 3

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

Sample Output 3

0
1 4
3 1 2 5
2 2 6
1 7
2 2 4
4 2 3 4 5
C - Not Covered Points

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

2 次元平面上に点 1 から点 N までの N 個の点があります。点 i (1\le i\le N) の座標は (X_i,Y_i) です。ここで、X,Y はそれぞれ (1,2,\ldots,N) の順列であることが保証されます。

左下の頂点を (0,0) 、右上の頂点を (X_i,Y_i) とする x 軸に平行な辺と y 軸に平行な辺のみからなる長方形の内部(辺上を含まない)に点 1 から点 N までの N 個の点をどれも含まないような i の個数を求めてください。

制約

  • 1\le N\le 3\times 10^5
  • 1\le X_i,Y_i\le N
  • X,Y はそれぞれ (1,2,\ldots,N) の順列
  • 入力される値は全て整数

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

答えを出力せよ。


入力例 1

3
2 1
1 3
3 2

出力例 1

2

i=1,22 つが条件を満たします。

i=3 の場合、左下を (0,0) 、右上を (3,2) とする x 軸に平行な辺を持つ長方形の内部(辺上を含まない)に点 1 を含みます。


入力例 2

5
1 1
4 2
2 3
5 5
3 4

出力例 2

1

入力例 3

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

出力例 3

2

Score : 300 points

Problem Statement

There are N points numbered 1 through N on a two-dimensional plane. Point i (1\le i\le N) has coordinates (X_i,Y_i). Here, it is guaranteed that X and Y are each permutations of (1,2,\ldots,N).

Find the number of values of i such that the interior (not including the boundary) of the rectangle with sides parallel to the x-axis and y-axis, with lower-left vertex (0,0) and upper-right vertex (X_i,Y_i), contains none of the N points numbered 1 through N.

Constraints

  • 1\le N\le 3\times 10^5
  • 1\le X_i,Y_i\le N
  • X and Y are each permutations of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Output the answer.


Sample Input 1

3
2 1
1 3
3 2

Sample Output 1

2

The two values i=1,2 satisfy the condition.

For i=3, the interior (not including the boundary) of the rectangle with sides parallel to the x-axis, with lower-left vertex (0,0) and upper-right vertex (3,2), contains point 1.


Sample Input 2

5
1 1
4 2
2 3
5 5
3 4

Sample Output 2

1

Sample Input 3

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

Sample Output 3

2
D - Accomplice

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

とある館で殺人事件が起きました。犯人の候補は N 人おり、彼らを人 1、人 2\dots、人 N と呼びます。

i は時刻 S_i に館に入り、時刻 T_i に館を出て、ほかの時刻には出入りしませんでした。

犯行について以下のことが分かっています。

  • 犯人はちょうど 2 人いる。
  • 犯行はある整数時刻 x に開始し、D 単位時間かけて行われ、時刻 x + D に完了した。
  • 犯人は 2 人とも犯行開始から犯行完了まで常に館にいた。(犯行開始と同時に館に入ったり、犯行完了と同時に館を出たりしたかもしれない。)

N 人の犯人候補の中に犯人が 2 人ともいると仮定したとき、2 人の犯人と犯行開始時刻の組としてありうるものは何通りあるでしょうか。ここで、2 人の犯人には順番を付けません。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq T_i \leq 10^6
  • 1 \leq D \leq 10^6
  • 入力される値は全て整数

入力

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

N D
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

2 人の犯人と犯行開始時刻の組としてありうるものの個数を出力せよ。


入力例 1

3 2
9 17
10 12
13 20

出力例 1

4

この例では犯人の候補が 3 人おり、犯行は 2 単位時間かけて行われました。

  • 1, 2 が犯人であるとすると、彼らがともに館にいたのは時刻 10 から時刻 12 までです。犯行開始時刻が時刻 10 であるとすると、2 単位時間かけて犯行が可能です。
  • 1, 3 が犯人であるとすると、彼らがともに館にいたのは時刻 13 から時刻 17 までです。犯行開始時刻が時刻 13, 14, 15 のいずれかであるとすると、2 単位時間かけて犯行が可能です。
  • 2, 3 が犯人であるとすると、彼らがともに館にいた時間はなく、このペアによる犯行は不可能です。

以上より、2 人の犯人と犯行開始時刻の組としてありうるものは(人 1, 2, 時刻 10)、(人 1, 3, 時刻 13)、(人 1, 3, 時刻 14)、(人 1, 3, 時刻 15)の 4 通りです。


入力例 2

3 5
9 17
10 12
13 20

出力例 2

0

犯人の候補は入力例 1 と同じですが、今回の犯行は 5 単位時間かけて行われました。

どの 2 人が犯人であるとしても、2 人がともに館にいた時間が犯行の所要時間より短く、犯行は不可能です。

従って、2 人の犯人と犯行開始時刻の組としてありうるものは 0 通りです。(すなわち、仮定は誤りであり、我々は真犯人を逃しています。)


入力例 3

4 1
1 1000000
1 1000000
1 1000000
1 1000000

出力例 3

5999994

より大きなケースでのオーバーフローに注意してください。

Score : 400 points

Problem Statement

A murder occurred at a certain mansion. There are N suspects, called person 1, person 2, \dots, person N.

Person i entered the mansion at time S_i, left the mansion at time T_i, and did not enter or leave at any other times.

The following facts are known about the crime:

  • There are exactly two culprits.
  • The crime started at some integer time x, took D units of time, and was completed at time x + D.
  • Both culprits were in the mansion at all times from the start to the completion of the crime. (They may have entered the mansion exactly when the crime started, or left exactly when it was completed.)

Assuming that both culprits are among the N suspects, how many combinations of the two culprits and the crime start time are possible? Here, the order of the two culprits does not matter.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq T_i \leq 10^6
  • 1 \leq D \leq 10^6
  • All input values are integers.

Input

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

N D
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Output the number of possible combinations of the two culprits and the crime start time.


Sample Input 1

3 2
9 17
10 12
13 20

Sample Output 1

4

In this sample, there are three suspects, and the crime took two units of time.

  • If persons 1 and 2 are the culprits, they were both in the mansion from time 10 to time 12. If the crime start time is time 10, the crime can be committed in two units of time.
  • If persons 1 and 3 are the culprits, they were both in the mansion from time 13 to time 17. If the crime start time is 13, 14, or 15, the crime can be committed in two units of time.
  • If persons 2 and 3 are the culprits, they were never in the mansion at the same time, so this pair cannot have committed the crime.

Thus, the possible combinations of the two culprits and the crime start time are (persons 1, 2, time 10), (persons 1, 3, time 13), (persons 1, 3, time 14), (persons 1, 3, time 15), giving four combinations.


Sample Input 2

3 5
9 17
10 12
13 20

Sample Output 2

0

The suspects are the same as in Sample Input 1, but this time the crime took five units of time.

No matter which two persons are the culprits, the time they were both in the mansion is shorter than the time required for the crime, so the crime is impossible.

Thus, the number of possible combinations of the two culprits and the crime start time is zero. (In other words, our assumption is wrong, and the real culprit has escaped us.)


Sample Input 3

4 1
1 1000000
1 1000000
1 1000000
1 1000000

Sample Output 3

5999994

Beware of overflow for larger cases.

E - Alternating Costs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

整数 A,B,X,Y が与えられます。

二次元平面上にコマが置かれています。はじめコマは座標 (0,0) にあります。

あなたは以下の操作を 0 回以上何回でも行うことができます:

  • 今コマがある座標を (x,y) として、座標 (x-1,y),(x+1,y),(x,y-1),(x,y+1) のいずれかにコマを移動させる。

k 回目 (k \geq 1) に行う操作にかかるコストは k の偶奇によって異なり、それぞれ以下の通りです:

  • k が奇数のとき:今コマがある座標を (x,y) として、座標 (x-1,y),(x+1,y) に移動させる場合のコストは A、座標 (x,y-1),(x,y+1) に移動させる場合のコストは B である。
  • k が偶数のとき:今コマがある座標を (x,y) として、座標 (x-1,y),(x+1,y) に移動させる場合のコストは B、座標 (x,y-1),(x,y+1) に移動させる場合のコストは A である。

コマを座標 (X,Y) に移動させるために必要なコストの総和の最小値を求めてください。

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

制約

  • 1\le T\le 2\times 10^5
  • 1\le A,B\le 10^9
  • -10^9\le X,Y\le 10^9
  • 入力される値は全て整数

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。

A B X Y

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

5
1 2 -1 2
8 5 0 0
7 13 9 4
1 1 0 100
31 9 -74 -60

出力例 1

4
0
103
100
1332

1 番目のテストケースについて考えます。

以下のように移動することでコストの総和が 4 になります:

  • コマを座標 (0,0) から座標 (-1,0) に移動させる。コストが 1 かかる。
  • コマを座標 (-1,0) から座標 (-1,1) に移動させる。コストが 1 かかる。
  • コマを座標 (-1,1) から座標 (-1,2) に移動させる。コストが 2 かかる。

コストの総和が 4 未満でコマを座標 (-1,2) に移動させることはできないので、1 行目には 4 を出力してください。

Score : 450 points

Problem Statement

You are given integers A,B,X,Y.

A piece is placed on a two-dimensional plane. Initially, the piece is at coordinate (0,0).

You can perform the following operation zero or more times:

  • Let the current coordinate of the piece be (x,y). Move the piece to one of the coordinates (x-1,y),(x+1,y),(x,y-1),(x,y+1).

The cost of the k-th operation (k \geq 1) depends on the parity of k, as follows:

  • If k is odd: Let the current coordinate of the piece be (x,y). The cost of moving to (x-1,y) or (x+1,y) is A, and the cost of moving to (x,y-1) or (x,y+1) is B.
  • If k is even: Let the current coordinate of the piece be (x,y). The cost of moving to (x-1,y) or (x+1,y) is B, and the cost of moving to (x,y-1) or (x,y+1) is A.

Find the minimum total cost required to move the piece to coordinate (X,Y).

T test cases are given; solve each one.

Constraints

  • 1\le T\le 2\times 10^5
  • 1\le A,B\le 10^9
  • -10^9\le X,Y\le 10^9
  • All input values are integers.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

The i-th (1\le i\le T) test case \text{case}_i is given in the following format:

A B X Y

Output

Output the answers for the test cases in order, each on a separate line.


Sample Input 1

5
1 2 -1 2
8 5 0 0
7 13 9 4
1 1 0 100
31 9 -74 -60

Sample Output 1

4
0
103
100
1332

Consider the first test case.

The following moves cost 4 in total:

  • Move the piece from (0,0) to (-1,0). This costs 1.
  • Move the piece from (-1,0) to (-1,1). This costs 1.
  • Move the piece from (-1,1) to (-1,2). This costs 2.

It is impossible to move the piece to (-1,2) with a total cost less than 4, so output 4 on the first line.

F - More ABC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

英大文字のみからなる文字列 S が与えられます。
高橋君の目標は、S の文字のうちいくつかを置換することによって、SABC を部分文字列として含む個数をちょうど K増やす ことです。

このようなことが可能か判定し、可能な場合は目標を達成するために高橋君が最低何文字置換する必要があるか求めてください。

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

置換とは

S の文字のうちの 1 つを置換するとは、1\leq i\leq \lvert S\rvert をみたす整数 i1 つ選び、Si 文字目を任意の英大文字 1 で置き換えることを指します。
ただし、\lvert S\rvertS の長さを表すものとします。

部分文字列とは

S の部分文字列とは、S の先頭および末尾からそれぞれ 0 文字以上削除して得られる文字列のことを指します。
2 つの部分文字列は、S から取り出した場所が異なれば、文字列として等しくとも区別して数えられます。

制約

  • 1\leq T\leq 10^5
  • S は英大文字のみからなる長さ 3 以上 3\times 10^5 以下の文字列
  • 1\leq K \leq 10
  • それぞれの入力において、各テストケースの S の長さの総和は 3\times 10^5 以下である。
  • T,K は整数

入力

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

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

\mathrm{case}_ii 番目のテストケースを表す。
各テストケースは以下の形式で与えられる。

S
K

出力

T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースに対する答えを出力せよ。
ここで、各テストケースに対する答えとして、高橋君が目標を達成することが不可能な場合は -1 を、そうでない場合は目標を達成するために置換する必要のある文字の個数の最小値を出力せよ。


入力例 1

5
ATCABC
1
ABABC
1
XABCYZ
1
ZZZZZ
1
ABCBABCAEFCABAABC
2

出力例 1

1
-1
6
3
3

1 個目のテストケースにおいて、与えられる文字列 S=ATCABCABC を部分文字列として 1 つ含んでいます。
S2 文字目を B に置換すると、S=ABCABC となって、ABC を部分文字列として 2 つ含むようになります。
これにより、1 字置換することで、 ABC を部分文字列として含む個数が 1 つ増え、目標は達成されました。よって、1 行目には 1 を出力します。

2 個目のテストケースにおいて、与えられる文字列 S=ABABCABC を部分文字列として 1 つ含んでいますが、 S の文字をどのように置換しても、SABC を部分文字列として 2 つ含むようにすることはできません。
よって、2 行目には -1 を出力します。

3 個目のテストケースにおいて、与えられる文字列 S=XABCYZABC を部分文字列として 1 つ含んでいます。
SABC を部分文字列として 2 つ含むようにするためには、S の全ての文字を置換し、S=ABCABC とする必要があります。
よって、3 行目には 6 を出力します。

Score : 500 points

Problem Statement

You are given a string S consisting of uppercase English letters.
Takahashi's goal is to increase the number of occurrences of ABC as a substring of S by exactly K, by replacing some characters of S.

Determine whether this is possible, and if so, find the minimum number of characters Takahashi needs to replace to achieve his goal.

T test cases are given; solve each one.

What does "replacing" mean?

Replacing one character of S means choosing an integer i satisfying 1\leq i\leq \lvert S\rvert and replacing the i-th character of S with any one uppercase English letter.
Here, \lvert S\rvert denotes the length of S.

What is a substring?

A substring of S is a string obtained by removing zero or more characters from the beginning and end of S.
Two substrings are counted as distinct if they are taken from different positions in S, even if they are equal as strings.

Constraints

  • 1\leq T\leq 10^5
  • S is a string of length between 3 and 3\times 10^5, inclusive, consisting of uppercase English letters.
  • 1\leq K \leq 10
  • In each input, the total length of S over all test cases is at most 3\times 10^5.
  • T and K are integers.

Input

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

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

\mathrm{case}_i represents the i-th test case.
Each test case is given in the following format:

S
K

Output

Output T lines.
The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
For each test case, if it is impossible for Takahashi to achieve his goal, output -1; otherwise, output the minimum number of characters that need to be replaced to achieve his goal.


Sample Input 1

5
ATCABC
1
ABABC
1
XABCYZ
1
ZZZZZ
1
ABCBABCAEFCABAABC
2

Sample Output 1

1
-1
6
3
3

In the first test case, the given string S=ATCABC contains ABC as a substring once.
Replacing the second character of S with B gives S=ABCABC, which contains ABC as a substring twice.
Thus, replacing one character increases the number of occurrences of ABC as a substring by 1, achieving the goal. Therefore, output 1 on the first line.

In the second test case, the given string S=ABABC contains ABC as a substring once; no matter how the characters of S are replaced, it is impossible to make S contain ABC as a substring twice.
Therefore, output -1 on the second line.

In the third test case, the given string S=XABCYZ contains ABC as a substring once.
To make S contain ABC as a substring twice, all characters of S must be replaced, giving S=ABCABC.
Therefore, output 6 on the third line.

G - Completely Wrong

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

1 から N までの番号がついた N 個のボールが入った箱があります。各ボールには整数で表される色がついており、ボール i は色 C_i で塗られています。

高橋君は操作を N 回行いました。k 回目 (1\le k\le N) の操作は以下の通りです:

  • 箱の中からボールを一様ランダムに一つ取り出して捨てる。取り出したボールが色 G_k であれば、1 点を得る。

N 回の操作で得た合計得点が 0 点となる確率を \text{mod}\ 998244353 で求めてください。

確率 \text{mod}\ 998244353 の定義

求める確率は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、求める有理数を既約分数 \frac{P}{Q} で表した時、Q {{}\not\equiv{}} 0 \pmod{998244353} となることが証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R \lt 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1\le N\le 2\times 10^5
  • 1\le C_i,G_k\le N
  • 入力される値は全て整数

入力

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

N
C_1 C_2 \ldots C_N
G_1 G_2 \ldots G_N

出力

求めた確率を \text{mod}\ 998244353 で出力せよ。


入力例 1

3
1 2 3
1 1 2

出力例 1

332748118

例えば操作は以下のように進行します:

  • 1 回目:箱の中から色 2 のボールが取り出される。
  • 2 回目:箱の中から色 1 のボールが取り出される。1 点を獲得する。
  • 3 回目:箱の中から色 3 のボールが取り出される。

この場合の合計得点は 1 点です。

高橋君の合計得点が 0 点となる確率は \displaystyle \frac13 です。


入力例 2

3
1 1 3
3 3 3

出力例 2

0

高橋君の合計得点は必ず 1 点となります。


入力例 3

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

出力例 3

316110712

Score : 575 points

Problem Statement

There is a box containing N balls numbered 1 through N. Each ball is painted with a color represented as an integer, and ball i is painted with color C_i.

Takahashi performed N operations. The k-th operation (1\le k\le N) is as follows:

  • Draw one ball uniformly at random from the box and discard it. If the drawn ball has color G_k, gain 1 point.

Find the probability, modulo 998244353, that the total score over N operations is 0 points.

Definition of probability modulo 998244353

It can be proved that the sought probability is always a rational number. Furthermore, under the constraints of this problem, when the rational number is expressed as an irreducible fraction \frac{P}{Q}, it can be proved that Q {{}\not\equiv{}} 0 \pmod{998244353}. Thus, there exists a unique integer R satisfying R \times Q \equiv P \pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 1\le N\le 2\times 10^5
  • 1\le C_i,G_k\le N
  • All input values are integers.

Input

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

N
C_1 C_2 \ldots C_N
G_1 G_2 \ldots G_N

Output

Output the sought probability modulo 998244353.


Sample Input 1

3
1 2 3
1 1 2

Sample Output 1

332748118

For example, the operations may proceed as follows:

  • First operation: A ball with color 2 is drawn from the box.
  • Second operation: A ball with color 1 is drawn from the box. Gain 1 point.
  • Third operation: A ball with color 3 is drawn from the box.

In this case, the total score is 1 point.

The probability that Takahashi's total score is 0 is \displaystyle \frac13.


Sample Input 2

3
1 1 3
3 3 3

Sample Output 2

0

Takahashi's total score is always 1 point.


Sample Input 3

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

Sample Output 3

316110712