A - Spoiler

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

配点 : 150 点

問題文

英小文字と | のみからなる文字列 S が与えられます。S は | をちょうど 2 個含むことが保証されます。

2 つの | の間にある文字および | を S から削除した文字列を出力してください。

制約

  • S は英小文字および | のみからなる長さ 2 以上 100 以下の文字列
  • S は | をちょうど 2 個含む

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder|beginner|contest

出力例 1

atcodercontest

2 つの | に挟まれた文字を全て削除して出力してください。


入力例 2

|spoiler|

出力例 2



全ての文字が削除されることもあります。


入力例 3

||xyz

出力例 3

xyz

Score: 150 points

Problem Statement

You are given a string S consisting of lowercase English letters and |. S is guaranteed to contain exactly two |s.

Remove the characters between the two |s, including the |s themselves, and print the resulting string.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of lowercase English letters and |.
  • S contains exactly two |s.

Input

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

S

Output

Print the answer.


Sample Input 1

atcoder|beginner|contest

Sample Output 1

atcodercontest

Remove all the characters between the two |s and print the result.


Sample Input 2

|spoiler|

Sample Output 2



It is possible that all characters are removed.


Sample Input 3

||xyz

Sample Output 3

xyz
B - Compromise

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

配点 : 100 点

問題文

N 個の選択肢があります。

あなたは、これらの中から 1 つの選択肢を選びます。あなたが i 番目の選択肢を選んだときの嬉しさは X_i です。

どの選択肢を選んでも嬉しさが負になる場合は Yes を、そうでない場合は No を出力してください。

制約

  • 2 \leq N \leq 10
  • -100 \leq X_i \leq 100
  • 入力される値はすべて整数

入力

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

N
X_1 X_2 \ldots X_N

出力

問題文中の指示に従って Yes、No のいずれかを出力せよ。


入力例 1

4
2 0 -1 2

出力例 1

No

選択肢 1, 2, 4 のいずれかを選んだとき、嬉しさは負になりません。


入力例 2

3
-5 -2 -1

出力例 2

Yes

入力例 3

4
0 -2 0 -1

出力例 3

No

Score : 100 points

Problem Statement

There are N choices.

You will choose one of these choices. If you choose the i-th choice, your happiness will be X_i.

If your happiness will be negative no matter which choice you choose, output Yes; otherwise, output No.

Constraints

  • 2 \leq N \leq 10
  • -100 \leq X_i \leq 100
  • All input values are integers.

Input

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

N
X_1 X_2 \ldots X_N

Output

Output Yes or No according to the instructions in the problem statement.


Sample Input 1

4
2 0 -1 2

Sample Output 1

No

If you choose choice 1, 2, or 4, the happiness will not be negative.


Sample Input 2

3
-5 -2 -1

Sample Output 2

Yes

Sample Input 3

4
0 -2 0 -1

Sample Output 3

No
C - 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_i は A_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=8 が 24 の約数であるため、条件をみたしません。

条件をみたす組は (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
D - Measure

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

配点 : 200 点

問題文

正整数 N が与えられるので、下記で定まる長さ (N+1) の文字列 s_0s_1\ldots s_N を出力してください。

各 i = 0, 1, 2, \ldots, N について、

  • 1 以上 9 以下の N の約数 j であって、i が N/j の倍数であるものが存在するとき、そのような j のうち最小のものに対応する数字を s_i とする。(よって、この場合 s_i は 1 、2 、\ldots 、9 のいずれかである。)
  • そのような j が存在しないとき、s_i は - とする。

制約

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

入力

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

N

出力

答えを出力せよ。


入力例 1

12

出力例 1

1-643-2-346-1

以下で、いくつかの i について s_i の決め方を説明します。

  • i = 0 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは、j = 1, 2, 3, 4, 6 の 5 個です。そのうち最小のものは 1 であるので、s_0 = 1 です。

  • i = 4 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは、j = 3, 6 の 2 個です。そのうち最小のものは 3 であるので、s_4 = 3 です。

  • i = 11 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは存在しないので、s_{11} = - です。


入力例 2

7

出力例 2

17777771

入力例 3

1

出力例 3

11

Score : 200 points

Problem Statement

You are given a positive integer N. Print a string of length (N+1), s_0s_1\ldots s_N, defined as follows.

For each i = 0, 1, 2, \ldots, N,

  • if there is a divisor j of N that is between 1 and 9, inclusive, and i is a multiple of N/j, then s_i is the digit corresponding to the smallest such j (s_i will thus be one of 1, 2, ..., 9);
  • if no such j exists, then s_i is -.

Constraints

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

Input

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

N

Output

Print the answer.


Sample Input 1

12

Sample Output 1

1-643-2-346-1

We will explain how to determine s_i for some i.

  • For i = 0, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 1, 2, 3, 4, 6. The smallest of these is 1, so s_0 = 1.

  • For i = 4, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 3, 6. The smallest of these is 3, so s_4 = 3.

  • For i = 11, there are no divisors j of N between 1 and 9 such that i is a multiple of N/j, so s_{11} = -.


Sample Input 2

7

Sample Output 2

17777771

Sample Input 3

1

Sample Output 3

11
E - Bingo 2

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

配点 : 300 点

問題文

縦 N 行、横 N 列のマス目があり、上から i 行目、左から j 列目のマスには整数 N\times (i-1)+j が書かれています。

今から T ターンにわたって相異なる整数が宣言されます。i ターン目には A_i が宣言され、A_i が書かれたマスに印をつけます。初めてビンゴを達成するのは何ターン目か求めてください。ただし、T ターンの中でビンゴを達成しない場合は -1 を出力してください。

ここで、ビンゴを達成するとは以下のいずれかのうち少なくとも一つ満たされることを言います。

  • マス目の横の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
  • マス目の縦の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
  • マス目の対角線の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する

制約

  • 2\leq N\leq 2\times 10^3
  • 1\leq T\leq \min(N^2,2\times 10^5)
  • 1\leq A_i\leq N^2
  • i\neq j ならば A_i\neq A_j
  • 入力は全て整数

入力

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

N T
A_1 A_2 \ldots A_T

出力

T ターンの中でビンゴを達成するならば初めてビンゴを達成するターンを、そうでないならば -1 を出力せよ。


入力例 1

3 5
5 1 8 9 7

出力例 1

4

マス目の状態は以下のように変化します。初めてビンゴを達成するのは 4 ターン目です。


入力例 2

3 5
4 2 9 7 5

出力例 2

-1

5 ターンの中でビンゴを達成できないので -1 を出力してください。


入力例 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

出力例 3

9

Score : 300 points

Problem Statement

There is an N \times N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N \times (i-1) + j.

Over T turns, integers will be announced. On Turn i, the integer A_i is announced, and the cell containing A_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

  • There exists a row in which all N cells are marked.
  • There exists a column in which all N cells are marked.
  • There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.

Constraints

  • 2 \leq N \leq 2 \times 10^3
  • 1 \leq T \leq \min(N^2, 2 \times 10^5)
  • 1 \leq A_i \leq N^2
  • A_i \neq A_j if i \neq j.
  • All input values are integers.

Input

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

N T
A_1 A_2 \ldots A_T

Output

If Bingo is achieved within T turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1.


Sample Input 1

3 5
5 1 8 9 7

Sample Output 1

4

The state of the grid changes as follows. Bingo is achieved for the first time on Turn 4.


Sample Input 2

3 5
4 2 9 7 5

Sample Output 2

-1

Bingo is not achieved within five turns, so print -1.


Sample Input 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

Sample Output 3

9
F - Previous Permutation

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

配点 : 300 点

問題文

(1, \dots, N) の順列 P = (P_1, \dots, P_N) が与えられます。ただし、(P_1, \dots, P_N) \neq (1, \dots, N) です。

(1 \dots, N) の順列を全て辞書順で小さい順に並べたとき、P が K 番目であるとします。辞書順で小さい方から K-1 番目の順列を求めてください。

順列とは?

(1, \dots, N) の順列とは、(1, \dots, N) を並べ替えて得られる数列のことをいいます。

辞書順とは?

長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) に対し、A が B より辞書順で真に小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことをいいます。

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
  • A_i < B_i

制約

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • 入力される値は全て整数

入力

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

N
P_1 \ldots P_N

出力

求める順列を Q = (Q_1, \dots, Q_N) として、Q_1, \dots, Q_N をこの順に空白区切りで一行に出力せよ。


入力例 1

3
3 1 2

出力例 1

2 3 1

(1, 2, 3) の順列を辞書順で小さい順に並べると次のようになります。

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

よって P = (3, 1, 2) は小さい方から 5 番目であり、求める順列、すなわち小さい方から 5 - 1 = 4 番目の順列は (2, 3, 1) です。


入力例 2

10
9 8 6 5 10 3 1 2 4 7

出力例 2

9 8 6 5 10 2 7 4 3 1

Score : 300 points

Problem Statement

You are given a permutation P = (P_1, \dots, P_N) of (1, \dots, N), where (P_1, \dots, P_N) \neq (1, \dots, N).

Assume that P is the K-th lexicographically smallest among all permutations of (1 \dots, N). Find the (K-1)-th lexicographically smallest permutation.

What are permutations?

A permutation of (1, \dots, N) is an arrangement of (1, \dots, N) into a sequence.

What is lexicographical order?

For sequences of length N, A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1 \leq i \leq N that satisfies both of the following.

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
  • A_i < B_i.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • All values in the input are integers.

Input

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

N
P_1 \ldots P_N

Output

Let Q = (Q_1, \dots, Q_N) be the sought permutation. Print Q_1, \dots, Q_N in a single line in this order, separated by spaces.


Sample Input 1

3
3 1 2

Sample Output 1

2 3 1

Here are the permutations of (1, 2, 3) in ascending lexicographical order.

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

Therefore, P = (3, 1, 2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5 - 1 = 4), is (2, 3, 1).


Sample Input 2

10
9 8 6 5 10 3 1 2 4 7

Sample Output 2

9 8 6 5 10 2 7 4 3 1
G - The Big Two

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

配点 : 400 点

問題文

あるゲームには N 人のプレイヤー 1,2,\dots,N がいます。
このゲームは 2 人のプレイヤーが 1 対 1 で戦う形式です。
N 人のプレイヤーによるトーナメント戦が M 回行われ、m 回目のトーナメント戦の決勝にはプレイヤー A_m,B_m の 2 人が勝ち上がりました。
以下の条件を満たすような 2 整数 x,y の組がいくつあるかを求めてください。

  • 1 \leq x \lt y \leq N
  • どのトーナメント戦においても、プレイヤー x とプレイヤー y の少なくとも一方が決勝に勝ち上がっている

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq N
  • 入力される値はすべて整数

入力

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

N M  
A_1 B_1  
A_2 B_2  
\vdots  
A_M B_M  

出力

答えを 1 行で出力せよ。


入力例 1

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

出力例 1

1

(x,y)=(2,3) のとき条件は満たされ、それ以外のとき条件は満たされません。
たとえば 4 回目のトーナメントではプレイヤー 1,4 はいずれも決勝に勝ち上がっていないので、(x,y)=(1,4) は条件を満たしません。


入力例 2

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

出力例 2

2

条件を満たす (x,y) の組は、(1,2),(1,4) の 2 つです。


入力例 3

5 8
1 2
2 4
1 3
1 3
1 2
1 2
1 5
1 2

出力例 3

2

Score : 400 points

Problem Statement

There is a game with N players 1,2,\dots,N.
This game is played in a format where two players face each other one-on-one.
A tournament among the N players was held M times, and the two players A_m and B_m advanced to the final of the m-th tournament.
Find the number of pairs of integers x and y satisfying the following conditions.

  • 1 \leq x \lt y \leq N
  • In every tournament, at least one of players x and y advanced to the final.

Constraints

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

Input

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

N M  
A_1 B_1  
A_2 B_2  
\vdots  
A_M B_M  

Output

Output the answer in one line.


Sample Input 1

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

Sample Output 1

1

The conditions are satisfied when (x,y)=(2,3), and not satisfied otherwise.
For example, in the fourth tournament, neither player 1 nor player 4 advanced to the final, so (x,y)=(1,4) does not satisfy the conditions.


Sample Input 2

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

Sample Output 2

2

There are two pairs (x,y) satisfying the conditions: (1,2) and (1,4).


Sample Input 3

5 8
1 2
2 4
1 3
1 3
1 2
1 2
1 5
1 2

Sample Output 3

2
H - E [max]

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

配点 : 450 点

問題文

N 個の六面サイコロがあります。 サイコロには 1 から N までの番号が付けられており、サイコロ i の各面に書かれた数はそれぞれ A_{i,1},A_{i,2},\dots,A_{i,6} です。

今からこれら N 個のサイコロを全て同時に振ります。 各サイコロの上を向いた面に書かれた数の最大値の期待値を \bmod\ 998244353 で求めてください。

なお、いずれのサイコロについても、そのサイコロを振った際に上を向く面は独立かつ一様ランダムに選ばれるものとします。

期待値を \bmod\ 998244353 で求めるとは

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

制約

  • 1\leq N \leq 10^5
  • 1\leq A_{i,j} \leq 10^9
  • 入力は全て整数

入力

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

N
A_{1,1} A_{1,2} \dots A_{1,6}
A_{2,1} A_{2,2} \dots A_{2,6}
\vdots
A_{N,1} A_{N,2} \dots A_{N,6}

出力

答えを出力せよ。


入力例 1

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

出力例 1

332748121

サイコロ i の上を向いた面に書かれた数を x_i とします。

  • x_1=1,x_2=1 のケース:\frac{2}{6}\times\frac{3}{6}=\frac{1}{6} の確率で発生し、上を向いた面に書かれた数の最大値は 1 です。
  • x_1=1,x_2=3 のケース:\frac{2}{6}\times\frac{3}{6}=\frac{1}{6} の確率で発生し、上を向いた面に書かれた数の最大値は 3 です。
  • x_1=4,x_2=1 のケース:\frac{4}{6}\times\frac{3}{6}=\frac{1}{3} の確率で発生し、上を向いた面に書かれた数の最大値は 4 です。
  • x_1=4,x_2=3 のケース:\frac{4}{6}\times\frac{3}{6}=\frac{1}{3} の確率で発生し、上を向いた面に書かれた数の最大値は 4 です。

よって、求める期待値は \frac{1}{6}\times 1 + \frac{1}{6}\times 3 + \frac{1}{3}\times 4 + \frac{1}{3}\times 4 = \frac{10}{3} \equiv 332748121 \pmod{998244353} です。


入力例 2

2
1 1 1 1 1 1
2 2 2 2 2 2

出力例 2

2

サイコロ 1,2 の上を向いた面に書かれた数はそれぞれ常に 1,2 であり、その最大値は常に 2 です。


入力例 3

8
55 76 80 21 34 28
82 84 2 32 56 17
11 57 37 28 39 18
47 2 97 25 75 29
72 45 22 75 26 81
6 79 16 68 68 40
31 80 68 57 18 55
49 10 63 91 93 40

出力例 3

213725517

Score : 450 points

Problem Statement

There are N six-sided dice. The dice are numbered from 1 to N, and the numbers written on the faces of die i are A_{i,1},A_{i,2},\dots,A_{i,6}.

Now, all N dice will be rolled simultaneously. Find the expected value, modulo 998244353, of the maximum value among the numbers written on the face that comes up on each die.

For any die, the face that comes up when the die is rolled is chosen independently and uniformly at random.

Finding the expected value modulo 998244353

It can be proved that the expected value to be found is always a rational number. Also, under the constraints of this problem, it can be proved that when the value is expressed as an irreducible fraction \frac{P}{Q}, we have Q \not\equiv 0 \pmod{998244353}. Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Find this R.

Constraints

  • 1\leq N \leq 10^5
  • 1\leq A_{i,j} \leq 10^9
  • All input values are integers.

Input

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

N
A_{1,1} A_{1,2} \dots A_{1,6}
A_{2,1} A_{2,2} \dots A_{2,6}
\vdots
A_{N,1} A_{N,2} \dots A_{N,6}

Output

Output the answer.


Sample Input 1

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

Sample Output 1

332748121

Let x_i be the number written on the face that comes up on die i.

  • Case x_1=1,x_2=1: Occurs with probability \frac{2}{6}\times\frac{3}{6}=\frac{1}{6}, and the maximum value among the numbers written on the faces that come up is 1.
  • Case x_1=1,x_2=3: Occurs with probability \frac{2}{6}\times\frac{3}{6}=\frac{1}{6}, and the maximum value among the numbers written on the faces that come up is 3.
  • Case x_1=4,x_2=1: Occurs with probability \frac{4}{6}\times\frac{3}{6}=\frac{1}{3}, and the maximum value among the numbers written on the faces that come up is 4.
  • Case x_1=4,x_2=3: Occurs with probability \frac{4}{6}\times\frac{3}{6}=\frac{1}{3}, and the maximum value among the numbers written on the faces that come up is 4.

Thus, the expected value to be found is \frac{1}{6}\times 1 + \frac{1}{6}\times 3 + \frac{1}{3}\times 4 + \frac{1}{3}\times 4 = \frac{10}{3} \equiv 332748121 \pmod{998244353}.


Sample Input 2

2
1 1 1 1 1 1
2 2 2 2 2 2

Sample Output 2

2

The numbers written on the faces that come up on dice 1,2 are always 1,2, respectively, and their maximum value is always 2.


Sample Input 3

8
55 76 80 21 34 28
82 84 2 32 56 17
11 57 37 28 39 18
47 2 97 25 75 29
72 45 22 75 26 81
6 79 16 68 68 40
31 80 68 57 18 55
49 10 63 91 93 40

Sample Output 3

213725517
I - Cards

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

配点 : 500 点

問題文

1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。

N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。

条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
  • 入力に含まれる値は全て整数である

入力

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

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

出力

答えを出力せよ。


入力例 1

3
1 2 3
2 1 3

出力例 1

3

例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。

条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\} の 3 通りです。


入力例 2

5
2 3 5 4 1
4 2 1 3 5

出力例 2

12

入力例 3

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

出力例 3

1

Score : 500 points

Problem Statement

There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).

How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.

Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq P_i,Q_i \leq N
  • P and Q are permutations of (1, 2, \dots, N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N

Output

Print the answer.


Sample Input 1

3
1 2 3
2 1 3

Sample Output 1

3

For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.

There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.


Sample Input 2

5
2 3 5 4 1
4 2 1 3 5

Sample Output 2

12

Sample Input 3

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

Sample Output 3

1