A - i i's

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

配点 : 400

問題文

正整数 N が与えられるので、 長さ L \coloneqq N(N+1)/2 の整数列 A = (A_1, A_2, \ldots, A_L) であって、下記の条件をすべて満たすものを 1 つ出力してください。

  • すべての i = 1, 2, \ldots, N について、Ai をちょうど i 個含む。
  • すべての i = 1, 2, \ldots, L について、1 \leq |A_i - A_{i+1}| \leq 2 。ただし、A_{L+1}A_1 を表す。

なお、本問題の制約下において、上記の条件をすべて満たす長さ L の整数列 A が必ず存在することが証明できます。

制約

  • 3 \leq N \leq 1000
  • N は整数

入力

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

N

出力

A の各要素を下記の形式にしたがって空白区切りで出力せよ。

A_1 A_2 \ldots A_L

なお、問題文中の条件を満たす長さ L の整数列 A が複数存在する場合は、その中のどれを出力しても正解となる。


入力例 1

4

出力例 1

1 3 4 2 4 3 4 2 4 3

整数列 A = (1, 3, 4, 2, 4, 3, 4, 2, 4, 3) は、ちょうど 1 個の 1 、ちょうど 2 個の 2 、ちょうど 3 個の 3 、ちょうど 4 個の 4 を含むため、1 つ目の条件を満たします。 また、下記の通り 2 つ目の条件も満たします。

  • |A_1 - A_2| = |1 - 3| = 2
  • |A_2 - A_3| = |3 - 4| = 1
  • |A_3 - A_4| = |4 - 2| = 2
  • |A_4 - A_5| = |2 - 4| = 2
  • |A_5 - A_6| = |4 - 3| = 1
  • |A_6 - A_7| = |3 - 4| = 1
  • |A_7 - A_8| = |4 - 2| = 2
  • |A_8 - A_9| = |2 - 4| = 2
  • |A_9 - A_{10}| = |4 - 3| = 1
  • |A_{10} - A_1| = |3 - 1| = 2

Score : 400 points

Problem Statement

You are given a positive integer N. Print an integer sequence of length L \coloneqq N(N+1)/2, A = (A_1, A_2, \ldots, A_L), that satisfies all of the following conditions.

  • A contains exactly i occurrences of i for every i = 1, 2, \ldots, N.
  • 1 \leq |A_i - A_{i+1}| \leq 2 for every i = 1, 2, \ldots, L. Here, A_{L+1} means A_1.

It can be proved that, under the Constraints of this problem, there is always an integer sequence A of length L that satisfies all of the above conditions.

Constraints

  • 3 \leq N \leq 1000
  • N is an integer.

Input

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

N

Output

Print the elements of A, separated by spaces, in the following format:

A_1 A_2 \ldots A_L

If multiple integer sequences A of length L satisfy the conditions in the problem statement, any one of them is accepted.


Sample Input 1

4

Sample Output 1

1 3 4 2 4 3 4 2 4 3

The integer sequence A = (1, 3, 4, 2, 4, 3, 4, 2, 4, 3) contains exactly one 1, two 2s, three 3s, and four 4s, satisfying the first condition. The second condition is also satisfied, as follows:

  • |A_1 - A_2| = |1 - 3| = 2
  • |A_2 - A_3| = |3 - 4| = 1
  • |A_3 - A_4| = |4 - 2| = 2
  • |A_4 - A_5| = |2 - 4| = 2
  • |A_5 - A_6| = |4 - 3| = 1
  • |A_6 - A_7| = |3 - 4| = 1
  • |A_7 - A_8| = |4 - 2| = 2
  • |A_8 - A_9| = |2 - 4| = 2
  • |A_9 - A_{10}| = |4 - 3| = 1
  • |A_{10} - A_1| = |3 - 1| = 2
B - Red and Blue Spanning Tree

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

配点 : 600

問題文

N 頂点 M 辺の連結無向グラフ G があります。i=1,2,\ldots,M に対し i 番目の辺は頂点 a_i, b_i を結んでいて、c_i= R ならば赤で、c_i= B ならば青で塗られています。

次の条件を満たす G の全域木が存在するかどうかを判定し、存在する場合は 1 つ示してください。

  • i=1,2,\ldots,N すべてに対し、
    • s_i = R ならば、頂点 i を端点とする赤の辺が 1 本以上存在する
    • s_i = B ならば、頂点 i を端点とする青の辺が 1 本以上存在する

制約

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • c_iR または B
  • i \neq j ならば (a_i,b_i,c_i) \neq (a_j,b_j,c_j)
  • 与えられるグラフは連結
  • s_iR または B
  • N,M,a_i,b_i は整数

入力

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

N M
a_1 b_1 c_1
\vdots
a_M b_M c_M
s_1 s_2 \ldots s_N

出力

条件を満たす G の全域木が存在しない場合、No と出力せよ。

No

存在する場合、以下の形式で出力せよ。

Yes
t_1 t_2 \ldots t_{N-1}

ここで、t_i は全域木の i 番目の辺が G における何番目の辺かを表す。
なお、問題文中の条件を満たす G の全域木が複数存在する場合、その中のどれを出力しても正解となる。


入力例 1

3 3
1 2 R
1 3 B
2 3 B
RRB

出力例 1

Yes
2 1

G における 1,2 番目の辺からなる全域木が条件を満たすことを以下に示します。

  • s_1 = R なので、i=1 に対する条件は頂点 1 を端点とする赤の辺が 1 本以上存在することです。これは G における 1 番目の辺が該当します。
  • s_2 = R なので、i=2 に対する条件は頂点 2 を端点とする赤の辺が 1 本以上存在することです。これは G における 1 番目の辺が該当します。
  • s_3 = B なので、i=3 に対する条件は頂点 3 を端点とする青の辺が 1 本以上存在することです。これは G における 2 番目の辺が該当します。

入力例 2

3 4
1 2 R
1 2 B
1 3 B
2 3 B
RRR

出力例 2

No

入力例 3

8 16
5 7 B
2 7 R
1 6 R
1 4 R
6 7 R
4 6 B
4 8 R
2 3 R
3 5 R
6 7 B
2 6 B
5 6 R
1 3 B
4 5 B
2 7 B
1 8 B
BRBRRBRB

出力例 3

Yes
1 2 4 9 11 13 16

入力例 4

8 10
1 7 R
1 3 B
2 5 B
2 8 R
1 5 R
3 6 R
2 6 B
3 4 B
2 8 B
4 6 B
RRRBBBRB

出力例 4

No

Score : 600 points

Problem Statement

You are given a connected undirected graph G with N vertices and M edges. For each i=1,2,\ldots,M, the i-th edge connects vertices a_i and b_i, and is colored red if c_i= R and blue if c_i= B.

Determine if there is a spanning tree of G that satisfies the following condition, and if so, display such a tree.

  • For every i=1,2,\ldots,N,
    • if s_i = R, at least one red edge has vertex i as its endpoint;
    • if s_i = B, at least one blue edge has vertex i as its endpoint.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • c_i is R or B.
  • (a_i,b_i,c_i) \neq (a_j,b_j,c_j) if i \neq j.
  • The given graph is connected.
  • s_i is R or B.
  • N,M,a_i,b_i are integers.

Input

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

N M
a_1 b_1 c_1
\vdots
a_M b_M c_M
s_1 s_2 \ldots s_N

Output

If no spanning tree of G satisfies the condition, print No.

No

Otherwise, print such a tree in the following format:

Yes
t_1 t_2 \ldots t_{N-1}

This means that the i-th edge of the spanning tree is the t_i-th edge of G.
If multiple spanning trees of G satisfy the condition, any one of them is accepted.


Sample Input 1

3 3
1 2 R
1 3 B
2 3 B
RRB

Sample Output 1

Yes
2 1

Here we show that the spanning tree formed by the first and second edges of G satisfies the condition.

  • s_1 = R, so for i=1, at least one red edge must have vertex 1 as its endpoint. The first edge of G is such an edge.
  • s_2 = R, so for i=2, at least one red edge must have vertex 2 as its endpoint. The first edge of G is such an edge.
  • s_3 = B, so for i=3, at least one blue edge must have vertex 3 as its endpoint. The second edge of G is such an edge.

Sample Input 2

3 4
1 2 R
1 2 B
1 3 B
2 3 B
RRR

Sample Output 2

No

Sample Input 3

8 16
5 7 B
2 7 R
1 6 R
1 4 R
6 7 R
4 6 B
4 8 R
2 3 R
3 5 R
6 7 B
2 6 B
5 6 R
1 3 B
4 5 B
2 7 B
1 8 B
BRBRRBRB

Sample Output 3

Yes
1 2 4 9 11 13 16

Sample Input 4

8 10
1 7 R
1 3 B
2 5 B
2 8 R
1 5 R
3 6 R
2 6 B
3 4 B
2 8 B
4 6 B
RRRBBBRB

Sample Output 4

No
C - Erase and Divide Game

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

配点 : 900

問題文

高橋君と青木君が以下のようなゲームをします。

  1. i=1,2,\ldots,N の順に次の操作をする。
    • l_i 以上 r_i 以下の整数を 1 個ずつ黒板に書く。(ここで、l_i,r_i は入力より与えられる非負整数である)
  2. 黒板に整数が 1 個以上書かれている間、高橋君を先手として交互に次の操作をする。
    • 以下の 2 種類の操作のうちちょうど一方を選び、実行する。
      • 黒板に書かれている偶数をすべて削除し、残った整数をそれぞれ 2 で割って小数点以下を切り捨てた値に書き換える。
      • 黒板に書かれている奇数をすべて削除し、残った整数をそれぞれ 2 で割った値に書き換える。
  3. 黒板に整数が 1 個も書かれていない状態になった場合、最後に操作をした人を勝者としてゲームを終了する。

高橋君と青木君が最適な行動を取った場合、ゲームは有限回の操作で終了することが示せます。この時の勝者を求めてください。

T ケースについて上記問題を解いてください。

制約

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^4
  • 0 \leq l_i \leq r_i \leq 10^{18}
  • r_i \lt l_{i+1}
  • 1 つの入力に含まれるテストケースについて、N の総和は 10^4 以下
  • 入力はすべて整数

入力

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

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

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

N
l_1 r_1
\vdots
l_N r_N

出力

T 行出力せよ。i 行目には、i 番目のテストケースにおいて勝者が高橋君ならば Takahashi と、青木君ならば Aoki と出力せよ。


入力例 1

3
2
1 2
5 7
1
0 100
10
1312150450968413 28316250877914571
74859962623690078 84324828731963974
148049062628894320 252509054433933439
269587449430302150 335408917861648766
349993004923078531 354979173822804781
522842184971407769 578223540024979436
585335723211047194 615812229161735895
645762258982631926 760713016476190622
779547116602436424 819875141880895723
822981260158260519 919845426262703496

出力例 1

Aoki
Aoki
Takahashi

1 番目のテストケースに対するゲームの流れの例を以下に示します。

  • 黒板に 1,2,5,6,71 個ずつ書かれる。
  • 高橋君が奇数を削除する方の操作をする。黒板から 1,5,7 が削除され、残った整数 2,6 がそれぞれ 2 で割った値の 1,3 に書き換えられる。
  • 青木君が奇数を削除する方の操作をする。黒板から 1,3 が削除され、黒板に整数が 1 個も書かれていない状態になったため最後に操作をした青木君を勝者としてゲームが終了する。

Score : 900 points

Problem Statement

Takahashi and Aoki will play the following game.

  1. For each i=1,2,\ldots,N in this order, do the following.
    • Write each integer from l_i through r_i once on the blackboard. (Here, l_i and r_i are non-negative integers from the input.)
  2. While one or more integers are written on the blackboard, the players take turns doing the following, with Takahashi going first.
    • Choose and perform exactly one of the following two operations.
      • Delete all even numbers written on the blackboard, and replace each remaining integer with half its value rounded down to an integer.
      • Delete all odd numbers written on the blackboard, and replace each remaining integer with half its value.
  3. When no integer is written on the blackboard, the game ends and the last player to perform an operation wins.

It can be proved that the game ends in a finite number of operations if Takahashi and Aoki act optimally. Find the winner in this case.

Solve the above problem for T cases.

Constraints

  • 1 \leq T \leq 10^4
  • 1 \leq N \leq 10^4
  • 0 \leq l_i \leq r_i \leq 10^{18}
  • r_i \lt l_{i+1}
  • The sum of N over the test cases in a single input is at most 10^4.
  • 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
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N
l_1 r_1
\vdots
l_N r_N

Output

Print T lines. The i-th line should contain Takahashi if Takahashi wins in the i-th test case, and Aoki if Aoki wins.


Sample Input 1

3
2
1 2
5 7
1
0 100
10
1312150450968413 28316250877914571
74859962623690078 84324828731963974
148049062628894320 252509054433933439
269587449430302150 335408917861648766
349993004923078531 354979173822804781
522842184971407769 578223540024979436
585335723211047194 615812229161735895
645762258982631926 760713016476190622
779547116602436424 819875141880895723
822981260158260519 919845426262703496

Sample Output 1

Aoki
Aoki
Takahashi

Here is an example of how the game goes for the first test case.

  • Each of 1,2,5,6,7 is written once on the blackboard.
  • Takahashi chooses to delete the odd numbers. 1,5,7 are deleted from the blackboard, and the remaining integers 2,6 are replaced by half their values, that is, 1,3, respectively.
  • Aoki chooses to delete the odd numbers. 1,3 are deleted from the blackboard, and no more integer is written on the blackboard, so the game ends and Aoki, who performed the last operation, wins.
D - Red and Blue Chips

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

配点 : 900

問題文

マス 1 、マス 2\ldots 、マス NN 個のマスがあり、各マスは赤と青のどちらかの色で塗られています。 各マスの色は、RB のみからなる長さ N の文字列 S によって表され、具体的には i = 1, 2, \ldots, N についてマス i は、Si 文字目が R のとき赤で、B のとき青で塗られています。 また、マス N は必ず青で塗られています。

はじめ、各マスに 1 枚ずつ、マスの色と同じ色のチップが置かれています。

i = 1, 2, \ldots, N-1 についてこの順番で下記の操作を行います。

  • i \lt j \leq N かつ マス j青で塗られているような整数 j を選び、マス i にあるチップの山を、チップの上下の位置関係を維持したまま、マス j にあるチップの山の上に重ねる。

上記の手順を行った後にマス N にできる N 枚のチップからなる山の、各チップの色を山の上のものから順に並べた列としてあり得るものの個数を 998244353 で割った余りを出力してください。

制約

  • 2 \leq N \leq 300
  • N は整数
  • SRB のみからなる長さ N の文字列
  • SN 文字目は B

入力

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

N
S

出力

答えを出力せよ。


入力例 1

4
RBRB

出力例 1

2

山の各チップの色を山の上のものから順に並べた列を、赤と青に対応する文字 RB からなる文字列で表すことにします。 特に、0 枚のチップからなる山の各チップの色を並べた列は、空文字列 \varepsilon です。

また、マス 1, 2, 3, 4 の山の各チップの色を山の上のものから順に並べた列がそれぞれ S_1, S_2, S_3, S_4 である状態を、状態 (S_1, S_2, S_3, S_4) と呼ぶことにします。

下記の手順を行うと、その後にマス 4 にできる山の各チップの色を山の上のものから順に並べた列として、RRBB が得られます。

  • まず、各マスに 1 枚ずつ、マスの色と同じ色のチップを置く。その結果、状態 ( R , B , R , B ) になる。
  • マス 1 にある山を、マス 2 にある山の上に重ねる。その結果、状態 (\varepsilon, RB , R , B ) になる。
  • マス 2 にある山を、マス 4 にある山の上に重ねる。その結果、状態 (\varepsilon, \varepsilon, R , RBB ) になる。
  • マス 3 にある山を、マス 4 にある山の上に重ねる。その結果、状態 (\varepsilon, \varepsilon, \varepsilon, RRBB ) になる。

また、下記の手順を行うと、その後にマス 4 にできる山の各チップの色を山の上のものから順に並べた列として、RBRB が得られます。

  • まず、各マスに 1 枚ずつ、マスの色と同じ色のチップを置く。その結果、状態 ( R , B , R , B ) になる。
  • マス 1 にある山を、マス 4 にある山の上に重ねる。その結果、状態 (\varepsilon, B , R , RB ) になる。
  • マス 2 にある山を、マス 4 にある山の上に重ねる。その結果、状態 (\varepsilon, \varepsilon, R , BRB ) になる。
  • マス 3 にある山を、マス 4 にある山の上に重ねる。その結果、状態 (\varepsilon, \varepsilon, \varepsilon, RBRB ) になる。

問題中の手順を行った後にマス 4 にできる山の、各チップの色を山の上のものから順に並べた列としてあり得るものは、上記の RRBBRBRB2 個のみです。


入力例 2

20
RRBRRRBBRBBBBRBRBRBB

出力例 2

92378

Score : 900 points

Problem Statement

There are N squares: square 1, square 2, \ldots, square N. Each square is colored red or blue. The colors of the squares are represented by a string S of length N. Specifically, for each i = 1, 2, \ldots, N, square i is colored red if the i-th character of S is R, and blue if it is B. Square N is always colored blue.

Initially, each square has a chip of the same color as the square.

For each i = 1, 2, \ldots, N-1 in this order, let us perform the following operation.

  • Choose an integer j such that i \lt j \leq N and square j is colored blue, and place the pile of chips on square i on top of the pile of chips on square j, keeping the order of the chips.

Print the number of possible sequences of colors of chips of the N-chip pile on square N from top to bottom after the above process, modulo 998244353.

Constraints

  • 2 \leq N \leq 300
  • N is an integer.
  • S is a string of length N consisting of R and B.
  • The N-th character of S is B.

Input

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

N
S

Output

Print the answer.


Sample Input 1

4
RBRB

Sample Output 1

2

We use a string consisting of R and B, corresponding to red and blue, to represent the sequence of colors of the chips of a pile from top to bottom. Particularly, the sequence of colors of the chips of a 0-chip pile is an empty string \varepsilon.

Also, the state where the sequences of colors of chips of the piles on square 1, 2, 3, 4 are S_1, S_2, S_3, S_4, respectively, is called as state (S_1, S_2, S_3, S_4).

The following process yields RRBB as the sequence of colors of chips of the pile on square 4.

  • First, on each square, place a chip of the same color as the square, resulting in state ( R , B , R , B ).
  • Place the pile of chips on square 1 on top of the pile of chips on square 2, resulting in state (\varepsilon, RB , R , B ).
  • Place the pile of chips on square 2 on top of the pile of chips on square 4, resulting in state (\varepsilon, \varepsilon, R , RBB ).
  • Place the pile of chips on square 3 on top of the pile of chips on square 4, resulting in state (\varepsilon, \varepsilon, \varepsilon, RRBB ).

The following process yields RBRB as the sequence of colors of chips of the pile on square 4.

  • First, on each square, place a chip of the same color as the square, resulting in state ( R , B , R , B ).
  • Place the pile of chips on square 1 on top of the pile of chips on square 4, resulting in state (\varepsilon, B , R , RB ).
  • Place the pile of chips on square 2 on top of the pile of chips on square 4, resulting in state (\varepsilon, \varepsilon, R , BRB ).
  • Place the pile of chips on square 3 on top of the pile of chips on square 4, resulting in state (\varepsilon, \varepsilon, \varepsilon, RBRB ).

The above two, RRBB and RBRB, are the only possible sequences of colors of chips of the pile on square 4 from top to bottom after the process in the problem.


Sample Input 2

20
RRBRRRBBRBBBBRBRBRBB

Sample Output 2

92378
E - Cross Sum Construction

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

配点 : 1300

問題文

正整数 N と長さ N の整数列 A=(a_1,\ldots,a_N), B=(b_1,\ldots,b_N) が与えられます。
また、XN^2 個の値 (a_i+b_j)(1 \leq i,j \leq N) からなる多重集合とします。

各要素が -10^{18} 以上 10^{18} 以下の整数である N \times N の行列 M に対し、スコアを以下のように定めます。

  • SN^2 個の値「Mi 行目または j 列目に属する 2N-1 要素の総和」 (1 \leq i,j \leq N)からなる多重集合とする。この時、スコアはすべての整数 z に対する \min( X に含まれる z の個数, S に含まれる z の個数) の総和。

スコアが最大となる行列 M1 つ求めてください。

T ケースについて上記問題を解いてください。

制約

  • 1 \leq T \leq 2.5 \times 10^5
  • 1 \leq N \leq 500
  • -10^9 \leq a_i,b_i \leq 10^9
  • 1 つの入力に含まれるテストケースについて、N^2 の総和は 2.5 \times 10^5 以下
  • 入力はすべて整数

入力

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

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

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

N
a_1 \ldots a_N
b_1 \ldots b_N

出力

各テストケースについて、以下の形式で出力せよ。

m_{1,1} \ldots m_{1,N}
\vdots
m_{N,1} \ldots m_{N,N}

ここで、m_{i,j} はスコアが最大となる行列 Mij 列目の要素であり、-10^{18} \leq m_{i,j} \leq 10^{18} を満たす整数である必要がある。
なお、スコアが最大となる行列 M が複数存在する場合は、その中のどれを出力しても正解となる。


入力例 1

3
1
5
-10
2
0 -1
8 -11
3
20 23 26
1 2 3

出力例 1

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

1 番目のケースの入出力では X=\{-5\}, S=\{-5\} であり、スコアは 1 です。
2 番目のケースの入出力では X=\{8,-11,7,-12\}, S=\{7,8,-11,-10\} であり、スコアは 3 です。
3 番目のケースの入出力では X=\{21,22,23,24,25,26,27,28,29\}, S=\{28,21,26,23,25,27,24,29,22\} であり、スコアは 9 です。

Score : 1300 points

Problem Statement

You are given a positive integer N and integer sequences of length N: A=(a_1,\ldots,a_N) and B=(b_1,\ldots,b_N).
Let X be the multiset of N^2 instances (a_i+b_j)(1 \leq i,j \leq N).

For an N \times N matrix M whose elements are integers between -10^{18} and 10^{18}, inclusive, we define the score as follows.

  • Let S be the multiset of N^2 instances, each of which is the sum of the 2N-1 elements in the i-th row or j-th column of M (1 \leq i,j \leq N). Then, the score is the sum of \min(the multiplicity of z in X, the multiplicity of z in S) over all integers z.

Find a matrix M with the maximum score.

Solve the above problem for T cases.

Constraints

  • 1 \leq T \leq 2.5 \times 10^5
  • 1 \leq N \leq 500
  • -10^9 \leq a_i,b_i \leq 10^9
  • The sum of N^2 over the test cases in a single input is at most 2.5 \times 10^5.
  • 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
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N
a_1 \ldots a_N
b_1 \ldots b_N

Output

For each test case, print a solution in the following format:

m_{1,1} \ldots m_{1,N}
\vdots
m_{N,1} \ldots m_{N,N}

Here, m_{i,j} is the element at the i-th row and j-th column of a matrix M with the maximum score, and must be an integer such that -10^{18} \leq m_{i,j} \leq 10^{18}.
If multiple matrices M have the maximum score, any one of them is accepted.


Sample Input 1

3
1
5
-10
2
0 -1
8 -11
3
20 23 26
1 2 3

Sample Output 1

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

For the input and output in the first case, X=\{-5\}, S=\{-5\}, for a score of 1.
For the input and output in the second case, X=\{8,-11,7,-12\}, S=\{7,8,-11,-10\}, for a score of 3.
For the input and output in the third case, X=\{21,22,23,24,25,26,27,28,29\}, S=\{28,21,26,23,25,27,24,29,22\}, for a score of 9.

F - No Permutations

実行時間制限: 8 sec / メモリ制限: 1024 MB

配点 : 1700

問題文

正整数 N が与えられます。 1 以上 N 以下の整数をそれぞれ 3 個ずつ含む長さ 3N の数列 A であって、 下記の条件を満たすものの個数を 998244353 で割った余りを出力してください。

  • A の長さ N のどの連続部分列も、数列 (1, 2, \ldots, N) の順列ではない。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 入力はすべて整数

入力

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

N

出力

答えを出力せよ。


入力例 1

3

出力例 1

132

例えば、A = (1, 3, 3, 2, 2, 2, 1, 1, 3) は問題文中の条件を満たします。 一方、A = (1, 3, 3, 2, 2, 3, 1, 1, 2) は問題文中の条件を満たしません。 なぜなら、A5, 6, 7 番目の要素からなる連続部分列が数列 (1, 2, 3) の順列であるからです。


入力例 2

123456

出力例 2

31984851

Score : 1700 points

Problem Statement

You are given a positive integer N. Print the number, modulo 998244353, of sequences A of length 3N that contain each integer from 1 through N three times and satisfy the following condition.

  • A has no contiguous subsequence of length N that is a permutation of the sequence (1, 2, \ldots, N).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 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

3

Sample Output 1

132

For instance, A = (1, 3, 3, 2, 2, 2, 1, 1, 3) satisfies the condition in the problem statement. On the other hand, A = (1, 3, 3, 2, 2, 3, 1, 1, 2) does not, because the 5-th, 6-th, 7-th elements of A form a contiguous subsequence that is a permutation of the sequence (1, 2, 3).


Sample Input 2

123456

Sample Output 2

31984851