A - Edge Checker 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

下の画像で示す図において、a 番の点と b 番の点が線で直接結ばれているかを答えてください。

制約

  • 1 \leq a \lt b \leq 15
  • a,b は整数

入力

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

a b

出力

a 番の点と b 番の点が線で直接結ばれているなら Yes、結ばれていないなら No を出力せよ。


入力例 1

1 2

出力例 1

Yes

問題文で示した図において、1 番の点と 2 番の点は線で直接結ばれています。 よって、Yes を出力します。


入力例 2

2 8

出力例 2

No

問題文で示した図において、2 番の点と 8 番の点は線で直接結ばれていません。 よって、No を出力します。


入力例 3

14 15

出力例 3

No

Score : 100 points

Problem Statement

Determine if there is a segment that directly connects the points numbered a and b in the figure below.

Constraints

  • 1 \leq a \lt b \leq 15
  • a and b are integers.

Input

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

a b

Output

Print Yes if there is a segment that directly connects the points numbered a and b; print No otherwise.


Sample Input 1

1 2

Sample Output 1

Yes

In the figure in the Problem Statement, there is a segment that directly connects the points numbered 1 and 2, so Yes should be printed.


Sample Input 2

2 8

Sample Output 2

No

In the figure in the Problem Statement, there is no segment that directly connects the points numbered 2 and 8, so No should be printed.


Sample Input 3

14 15

Sample Output 3

No
B - A Unique Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

長さ 3 の文字列 S が与えられます。
S に 1 度だけ含まれる文字を 1 つ出力してください。
但し、そのような文字が存在しない場合は代わりに -1 と出力してください。

制約

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

入力

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

S

出力

答えを出力せよ。正解が複数ある場合、どれを出力してもよい。


入力例 1

pop

出力例 1

o

pop に o は 1 度だけ含まれます。


入力例 2

abc

出力例 2

a

abc に a, b, c はどれも 1 度だけ含まれるので、どれを出力しても構いません。


入力例 3

xxx

出力例 3

-1

xxx に 1 度だけ含まれる文字はありません。

Score : 100 points

Problem Statement

You are given a string S of length 3.
Print a character that occurs only once in S.
If there is no such character, print -1 instead.

Constraints

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer. If multiple solutions exist, you may print any of them.


Sample Input 1

pop

Sample Output 1

o

o occurs only once in pop.


Sample Input 2

abc

Sample Output 2

a

a, b, and c occur once each in abc, so you may print any of them.


Sample Input 3

xxx

Sample Output 3

-1

No character occurs only once in xxx.

C - Distance Table

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N 個の駅、駅 1, 駅 2, \ldots, 駅 N が直線上にこの順に並んでいます。
ここで、1\leq i\leq N-1 について、駅 i と駅 (i+1) の間の距離は D_i です。

1\leq i<j\leq N をみたす整数の組 (i,j) それぞれについて、駅 i と駅 j の間の距離を求めてください。
なお、出力形式については出力の欄を参照してください。

制約

  • 2 \leq N \leq 50
  • 1 \leq D_i \leq 100
  • 入力はすべて整数

入力

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

N
D_1 D_2 \ldots D_{N-1}

出力

N-1 行出力せよ。
i 行目 (1\leq i\leq N-1) には、(N-i) 個の整数を空白区切りで出力せよ。
i 行目の j 番目 (1\leq j\leq N-i) には、駅 i と駅 (i+j) の間の距離を出力せよ。


入力例 1

5
5 10 2 3

出力例 1

5 15 17 20
10 12 15
2 5
3

各駅の間の距離は次のようになります。

  • 駅 1 と駅 2 の間の距離は 5 です。
  • 駅 1 と駅 3 の間の距離は 5+10=15 です。
  • 駅 1 と駅 4 の間の距離は 5+10+2=17 です。
  • 駅 1 と駅 5 の間の距離は 5+10+2+3=20 です。
  • 駅 2 と駅 3 の間の距離は 10 です。
  • 駅 2 と駅 4 の間の距離は 10+2=12 です。
  • 駅 2 と駅 5 の間の距離は 10+2+3=15 です。
  • 駅 3 と駅 4 の間の距離は 2 です。
  • 駅 3 と駅 5 の間の距離は 2+3=5 です。
  • 駅 4 と駅 5 の間の距離は 3 です。

よって、上記のように出力します。


入力例 2

2
100

出力例 2

100

Score : 200 points

Problem Statement

There are N stations, station 1, station 2, \ldots, station N, arranged in this order on a straight line.
Here, for 1\leq i\leq N-1, the distance between stations i and (i+1) is D_i.

For each pair of integers (i,j) satisfying 1\leq i<j\leq N, find the distance between stations i and j.
For the output format, refer to the Output section.

Constraints

  • 2 \leq N \leq 50
  • 1 \leq D_i \leq 100
  • All input values are integers.

Input

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

N
D_1 D_2 \ldots D_{N-1}

Output

Output N-1 lines.
On the i-th line (1\leq i\leq N-1), output (N-i) integers, separated by spaces.
The j-th integer on the i-th line (1\leq j\leq N-i) should be the distance between stations i and (i+j).


Sample Input 1

5
5 10 2 3

Sample Output 1

5 15 17 20
10 12 15
2 5
3

The distances between stations are as follows:

  • The distance between stations 1 and 2 is 5.
  • The distance between stations 1 and 3 is 5+10=15.
  • The distance between stations 1 and 4 is 5+10+2=17.
  • The distance between stations 1 and 5 is 5+10+2+3=20.
  • The distance between stations 2 and 3 is 10.
  • The distance between stations 2 and 4 is 10+2=12.
  • The distance between stations 2 and 5 is 10+2+3=15.
  • The distance between stations 3 and 4 is 2.
  • The distance between stations 3 and 5 is 2+3=5.
  • The distance between stations 4 and 5 is 3.

Thus, output as shown above.


Sample Input 2

2
100

Sample Output 2

100
D - Product Calculator

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は電卓を持っています。電卓には最初 1 が表示されています。
高橋君は電卓に対して N 回操作を行います。
i 回目 (1\leq i\leq N) の操作では、その時点で画面に表示されている数に正の整数 A_i をかけます。
しかし、電卓には K 桁までしか表示できないため、計算結果が (K+1) 桁以上になる場合、代わりに 1 が画面に表示されます。 そうでない場合は正しく計算結果が表示されます。

N 回の操作の後に電卓に表示されている数を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq 18
  • 1 \leq A_i < 10^K
  • 入力はすべて整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

N 回の操作の後に電卓に表示されている数を出力せよ。


入力例 1

5 2
7 13 3 2 5

出力例 1

10

今回電卓は 2 桁まで表示することができ、最初 1 が表示されています。これに対して、次のように高橋君は操作を行います。

  • 1 回目の操作で、7 をかけます。1\times 7=7 であり、電卓には 7 が表示されます。
  • 2 回目の操作で、13 をかけます。7\times 13=91 であり、電卓には 91 が表示されます。
  • 3 回目の操作で、3 をかけます。91\times 3=273 であり、3 桁になってしまうため、電卓には 1 が表示されます。
  • 4 回目の操作で、2 をかけます。1\times 2=2 であり、電卓には 2 が表示されます。
  • 5 回目の操作で、5 をかけます。2\times 5=10 であり、電卓には 10 が表示されます。

入力例 2

2 1
2 5

出力例 2

1

Score : 200 points

Problem Statement

Takahashi has a calculator that initially displays 1.
He will perform N operations on the calculator.
In the i-th operation (1 \leq i \leq N), he multiplies the currently displayed number by a positive integer A_i.
However, the calculator can display at most K digits. If the result of the multiplication has (K+1) or more digits, the display shows 1 instead; otherwise, the result is shown correctly.

Find the number showing on the calculator after the N operations.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq K \leq 18
  • 1 \leq A_i < 10^K
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Output the number shown on the calculator after the N operations.


Sample Input 1

5 2
7 13 3 2 5

Sample Output 1

10

This calculator can display at most two digits and initially shows 1. Takahashi operates as follows:

  • The 1st operation multiplies by 7. Since 1 \times 7 = 7, the calculator shows 7.
  • The 2nd operation multiplies by 13. Since 7 \times 13 = 91, the calculator shows 91.
  • The 3rd operation multiplies by 3. Since 91\times 3=273, which has three digits, the calculator shows 1.
  • The 4th operation multiplies by 2. Since 1\times 2=2, the calculator shows 2.
  • The 5th operation multiplies by 5. Since 2\times 5=10, the calculator shows 10.

Sample Input 2

2 1
2 5

Sample Output 2

1
E - Keys

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

あなたは N 本の鍵 1,2,\dots,N を持っています。
このうち何本かの鍵は正しい鍵で、それ以外はダミーの鍵です。

また、鍵を何本でも挿し込める ドアX があり、この ドアX は正しい鍵を K 本以上挿し込んだ時、またその時に限って開きます。

あなたはこれらの鍵に対して M 回のテストを行いました。このうち i 回目のテストの内容は次の通りです。

  • C_i 本の鍵 A_{i,1},A_{i,2},\dots,A_{i,C_i} を ドアX に挿し込む。
  • テスト結果はひとつの英文字 R_i で表現される。
    • R_i = o のとき i 回目のテストでドアが開いたことを表す。
    • R_i = x のとき i 回目のテストでドアが開かなかったことを表す。

各鍵が正しいかダミーかの組み合わせは 2^N 通り考えられますが、このうちどのテスト結果にも矛盾しない組み合わせの個数を求めてください。
ただし、与えられるテスト結果が誤っており上記の条件を満たす組み合わせが存在しない場合もあります。その場合は 0 通りと解答してください。

制約

  • N,M,K,C_i,A_{i,j} は整数
  • 1 \le K \le N \le 15
  • 1 \le M \le 100
  • 1 \le C_i \le N
  • 1 \le A_{i,j} \le N
  • j \neq k ならば A_{i,j} \neq A_{i,k}
  • R_i は o または x

入力

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

N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M

出力

答えを整数として出力せよ。


入力例 1

3 2 2
3 1 2 3 o
2 2 3 x

出力例 1

2

この入力では鍵が 3 本あり、テストは 2 回行われました。
また、 ドアX を開くのに必要な正しい鍵の本数は 2 本です。

  • 1 回目のテストでは鍵 1,2,3 を使い、その結果 ドアX は開きました。
  • 2 回目のテストでは鍵 2,3 を使い、その結果 ドアX は開きませんした。

各鍵が正しいかダミーかの組み合わせであって、どのテスト結果にも矛盾しないものは以下の 2 通りです。

  • 鍵 1 は本物、鍵 2 はダミー、鍵 3 は本物である。
  • 鍵 1 は本物、鍵 2 は本物、鍵 3 はダミーである。

入力例 2

4 5 3
3 1 2 3 o
3 2 3 4 o
3 3 4 1 o
3 4 1 2 o
4 1 2 3 4 x

出力例 2

0

問題文中でも述べた通り、答えが 0 通りである場合もあります。


入力例 3

11 4 9
10 1 2 3 4 5 6 7 8 9 10 o
11 1 2 3 4 5 6 7 8 9 10 11 o
10 11 10 9 8 7 6 5 4 3 2 x
10 11 9 1 4 3 7 5 6 2 10 x

出力例 3

8

Score : 300 points

Problem Statement

You have N keys numbered 1, 2, \dots, N.
Some of these are real keys, while the others are dummies.

There is a door, Door X, into which you can insert any number of keys. Door X will open if and only if at least K real keys are inserted.

You have conducted M tests on these keys. The i-th test went as follows:

  • You inserted C_i keys A_{i,1}, A_{i,2}, \dots, A_{i,C_i} into Door X.
  • The test result is represented by a single English letter R_i.
    • R_i = o means that Door X opened in the i-th test.
    • R_i = x means that Door X did not open in the i-th test.

There are 2^N possible combinations of which keys are real and which are dummies. Among these, find the number of combinations that do not contradict any of the test results.
It is possible that the given test results are incorrect and no combination satisfies the conditions. In such a case, report 0.

Constraints

  • N, M, K, C_i, and A_{i,j} are integers.
  • 1 \le K \le N \le 15
  • 1 \le M \le 100
  • 1 \le C_i \le N
  • 1 \le A_{i,j} \le N
  • A_{i,j} \neq A_{i,k} if j \neq k.
  • R_i is o or x.

Input

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

N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M

Output

Print the answer as an integer.


Sample Input 1

3 2 2
3 1 2 3 o
2 2 3 x

Sample Output 1

2

In this input, there are three keys and two tests were conducted.
Two correct keys are required to open Door X.

  • In the first test, keys 1, 2, 3 were used, and Door X opened.
  • In the second test, keys 2, 3 were used, and Door X did not open.

There are two combinations of which keys are real and which are dummies that do not contradict any of the test results:

  • Key 1 is real, key 2 is a dummy, and key 3 is real.
  • Key 1 is real, key 2 is real, and key 3 is a dummy.

Sample Input 2

4 5 3
3 1 2 3 o
3 2 3 4 o
3 3 4 1 o
3 4 1 2 o
4 1 2 3 4 x

Sample Output 2

0

As mentioned in the problem statement, the answer may be 0.


Sample Input 3

11 4 9
10 1 2 3 4 5 6 7 8 9 10 o
11 1 2 3 4 5 6 7 8 9 10 11 o
10 11 10 9 8 7 6 5 4 3 2 x
10 11 9 1 4 3 7 5 6 2 10 x

Sample Output 3

8
F - Adjacent Swaps

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。

高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。

  • 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。

操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。

制約

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

入力

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

N Q
x_1
\vdots
x_Q

出力

a_1,\ldots,a_N を空白区切りで出力せよ。


入力例 1

5 5
1
2
3
4
5

出力例 1

1 2 3 5 4

操作は以下のように行われます。

  • 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
  • 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
  • 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
  • 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。

入力例 2

7 7
7
7
7
7
7
7
7

出力例 2

1 2 3 4 5 7 6

入力例 3

10 6
1
5
2
9
6
6

出力例 3

1 2 3 4 5 7 6 8 10 9

Score : 300 points

Problem Statement

N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.

Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.

  • Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.

Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
x_1
\vdots
x_Q

Output

Print a_1,\ldots,a_N, with spaces in between.


Sample Input 1

5 5
1
2
3
4
5

Sample Output 1

1 2 3 5 4

The operations are performed as follows.

  • Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
  • Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
  • Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
  • Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.

Sample Input 2

7 7
7
7
7
7
7
7
7

Sample Output 2

1 2 3 4 5 7 6

Sample Input 3

10 6
1
5
2
9
6
6

Sample Output 3

1 2 3 4 5 7 6 8 10 9
G - Coprime 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられるので、以下の条件を満たす 1 以上 M 以下の整数 k を全て求めてください。

  • 全ての 1 \le i \le N を満たす整数 i について、 \gcd(A_i,k)=1 である。

制約

  • 入力は全て整数
  • 1 \le N,M \le 10^5
  • 1 \le A_i \le 10^5

入力

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

N M
A_1 A_2 \dots A_N

出力

1 行目に、出力する整数の数 x を出力せよ。
続く x 行に、答えとなる整数を小さい方から順に 1 行に 1 つずつ出力せよ。


入力例 1

3 12
6 1 5

出力例 1

3
1
7
11

例えば、 7 は \gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1 を満たすので答えとなる整数の集合に含まれます。
一方、 9 は \gcd(6,9)=3 となるため、答えとなる整数の集合に含まれません。
条件を満たす 1 以上 12 以下の整数は 1,7,11 の 3 つです。これらを小さい方から出力することに注意してください。

Score : 400 points

Problem Statement

Given a sequence of N positive integers A=(A_1,A_2,\dots,A_N), find every integer k between 1 and M (inclusive) that satisfies the following condition:

  • \gcd(A_i,k)=1 for every integer i such that 1 \le i \le N.

Constraints

  • All values in input are integers.
  • 1 \le N,M \le 10^5
  • 1 \le A_i \le 10^5

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

In the first line, print x: the number of integers satisfying the requirement.
In the following x lines, print the integers satisfying the requirement, in ascending order, each in its own line.


Sample Input 1

3 12
6 1 5

Sample Output 1

3
1
7
11

For example, 7 has the properties \gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1, so it is included in the set of integers satisfying the requirement.
On the other hand, 9 has the property \gcd(6,9)=3, so it is not included in that set.
We have three integers between 1 and 12 that satisfy the condition: 1, 7, and 11. Be sure to print them in ascending order.

H - Subarray Sum Divisibility

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

長さ N の整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。

あなたの目的は、以下の操作を繰り返し行うことにより、A のすべての長さ L の連続部分列についてその総和が M の倍数であるようにすることです。

  • 1 \leq i \leq N なる整数 i を選び、A_i の値を 1 増やす。

目的を達成するまでの操作回数として考えられる最小値を求めてください。

制約

  • 1 \leq N, M \leq 500
  • 1 \leq L \leq N
  • 0 \leq A_i < M
  • 入力される値はすべて整数

入力

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

N M L
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 5 3
4 2 1 3

出力例 1

4

i = 2 を選ぶ操作を 1 回、i = 3 を選ぶ操作を 2 回、i = 4 を選ぶ操作を 1 回行うことで合計 4 回の操作で A = (4, 3, 3, 4) となり、すべての長さ 3 の連続部分列の総和が 5 の倍数となります。


入力例 2

7 10 4
7 0 9 1 6 4 2

出力例 2

10

Score : 475 points

Problem Statement

You are given a length-N integer sequence A = (A_1, A_2, \ldots, A_N).

Your goal is to perform the following operation repeatedly so that for every length-L contiguous subarray of A, the sum is a multiple of M.

  • Choose an integer i such that 1 \leq i \leq N, and increase the value of A_i by 1.

Find the minimum possible number of operations before achieving the goal.

Constraints

  • 1 \leq N, M \leq 500
  • 1 \leq L \leq N
  • 0 \leq A_i < M
  • All input values are integers.

Input

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

N M L
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

4 5 3
4 2 1 3

Sample Output 1

4

By performing the operation once choosing i = 2, twice choosing i = 3, and once choosing i = 4, you get A = (4, 3, 3, 4) with a total of four operations, where every length-3 contiguous subarray sums to a multiple of 5.


Sample Input 2

7 10 4
7 0 9 1 6 4 2

Sample Output 2

10
I - Maximum Diameter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

長さ N の正整数列 X=(X_1,X_2\ldots,X_N) に対して、f(X) を以下のように定めます。

  • N 頂点の木であって、i\ (1 \leq i \leq N) 番目の頂点の次数が X_i であるようなものを良い木と呼ぶ。 良い木が存在するならば、f(X) は良い木の直径の最大値。良い木が存在しないならば、f(X)=0。

ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。

長さ N の正整数列 X としてあり得るもの全てに対する f(X) の総和を 998244353 で割った余りを求めてください。 なお、f(X) の総和は有限値になることが証明できます。

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

制約

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

入力

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

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

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

N

出力

T 行出力せよ。

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


入力例 1

10
2
3
5
8
13
21
34
55
89
144

出力例 1

1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

N=3 の場合について、

例えば、

  • X=(1,1,1) のとき、次数が 1,1,1 となる 3 頂点の木は存在しないため、f(X)=0 です。
  • X=(2,1,1) のとき、良い木は以下の図のものに限られます。この木の直径は 2 であるため、f(X)=2 です。


3 頂点の木

X=(2,1,1),(1,2,1),(1,1,2) のとき f(X)=2 であり、それ以外の X のとき f(X)=0 であるため、答えは 6 です。

Score : 500 points

Problem Statement

For a sequence X=(X_1,X_2\ldots,X_N) of length N consisting of positive integers, we define f(X) as follows:

  • A tree with N vertices is said to be good if and only if the degree of the i-th (1 \leq i \leq N) vertex is X_i. If a good tree exists, f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of a tree is the maximum distance between two vertices.

Find the sum, modulo 998244353, of f(X) over all possible sequences X of length N consisting of positive integers. We can prove that the sum of f(X) is a finite value.

Given T test cases, find the answer for each of them.

Constraints

  • 1\leq T \leq 2\times 10^5
  • 2 \leq N \leq 10^6
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i denotes 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

Output

Print T lines.

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


Sample Input 1

10
2
3
5
8
13
21
34
55
89
144

Sample Output 1

1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

If N=3,

for example,

  • when X=(1,1,1), there is no tree with three vertices whose degrees are 1,1, and 1, so f(X)=0.
  • When X=(2,1,1), the only possible tree is illustrated below. The diameter of this tree is 2, so f(X)=2.


3-vertex tree

For X=(2,1,1),(1,2,1),(1,1,2), we have f(X)=2; for other X, we have f(X)=0. Thus, the answer is 6.