A - Neq Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数 X が以下の条件を満たすとき、X“Neq Number” であるといいます。

  • X を十進法表記した際、どの隣接する 2 文字も相異なる

例えば 1,173,9090 は “Neq Number” です。一方、 22,6335 は “Neq Number” ではありません。

正整数 K が与えられます。小さいほうから K 番目の “Neq Number” を求めてください。

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

制約

  • 1 \leq T \leq 100
  • 1 \leq K \leq 10^{12}
  • 入力される値はすべて整数

入力

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

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

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

K

出力

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


入力例 1

3
25
148
998244353

出力例 1

27
173
2506230721

1 つめのテストケースについて、 “Neq Number” を小さいものから 25 個あげていくと

  • 1 から 9 までの整数の 9
  • 10 から 19 までの整数のうち、 11 を除いた 9
  • 20 から 27 までの整数のうち、 22 を除いた 7

となります。よって、小さいほうから 25 番目の “Neq Number” は 27 となります。

Score: 400 points

Problem Statement

A positive integer X is called a "Neq Number" if it satisfies the following condition:

  • When X is written in decimal notation, no two adjacent characters are the same.

For example, 1, 173, and 9090 are Neq Numbers, while 22 and 6335 are not.

You are given a positive integer K. Find the K-th smallest Neq Number.

You have T test cases to solve.

Constraints

  • 1 \leq T \leq 100
  • 1 \leq K \leq 10^{12}
  • All input values are integers.

Input

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

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

Each case is given in the following format:

K

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
25
148
998244353

Sample Output 1

27
173
2506230721

For the first test case, here are the smallest 25 Neq Numbers in ascending order:

  • The nine integers from 1 to 9
  • The nine integers from 10 to 19, excluding 11
  • The seven integers from 20 to 27, excluding 22

Thus, the 25-th smallest Neq Number is 27.

B - Make Many Triangles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

二次元平面上に相異なる N 個の点があります。 i 番目の点の座標は (x_i,y_i) です。

これらの点のいずれかを頂点とする(非退化な)三角形をたくさん作りたいです。ただし、同じ点を複数の三角形の頂点として用いることはできません。

最大で何個の三角形が作れるか求めてください。

非退化な三角形とは 非退化な三角形とは、 3 つの頂点が同一直線上に並ばない三角形のことを指します。

制約

  • 3 \leq N \leq 300
  • -10^9 \leq x_i,y_i \leq 10^9
  • i \neq j ならば (x_i,y_i) \neq (x_j,y_j)
  • 入力される値はすべて整数

入力

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

N
x_1 y_1
x_2 y_2
\vdots
x_{N} y_{N}

出力

答えを出力せよ。


入力例 1

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

出力例 1

2

例えば 1,3,6 番目の点からなる三角形と 2,4,5 番目の点からなる三角形を考えると、三角形を 2 つ作ることができます。

同じ点を複数の三角形の頂点として用いることはできませんが、三角形が共通部分を持っても構いません。


入力例 2

3
0 0
0 1000000000
0 -1000000000

出力例 2

0

Score: 500 points

Problem Statement

There are N distinct points on a two-dimensional plane. The coordinates of the i-th point are (x_i,y_i).

We want to create as many (non-degenerate) triangles as possible using these points as the vertices. Here, the same point cannot be used as a vertex of multiple triangles.

Find the maximum number of triangles that can be created.

What is a non-degenerate triangle? A non-degenerate triangle is a triangle whose three vertices are not collinear.

Constraints

  • 3 \leq N \leq 300
  • -10^9 \leq x_i,y_i \leq 10^9
  • If i \neq j, then (x_i,y_i) \neq (x_j,y_j)
  • 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

Print the answer.


Sample Input 1

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

Sample Output 1

2

For example, if we create a triangle from the first, third, and sixth points and another from the second, fourth, and fifth points, we can create two triangles.

The same point cannot be used as a vertex for multiple triangles, but the triangles may have overlapping areas.


Sample Input 2

3
0 0
0 1000000000
0 -1000000000

Sample Output 2

0
C - Not Median

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの整数からなる長さ N の順列 P=(P_1,P_2,\dots,P_N) が与えられます。

i=1,2,\dots,N に対し、以下の条件をすべて満たす 2 つの整数の組 (l,r) に対する r-l+1 の最小値を出力してください。ただし、そのような (l,r) が存在しない場合は -1 を出力してください。

  • 1 \leq l \leq i \leq r \leq N
  • r-l+1 は奇数
  • P の連続部分列 (P_l,P_{l+1},\dots,P_r) の中央値は P_i ではない

ここで、長さが L (奇数)の整数列 A に対して A の中央値とは、 A を昇順にソートして得られる数列を A' として A'\frac{L+1}{2} 番目の値のことを指します。

制約

  • 3 \leq N \leq 3 \times 10^5
  • (P_1,P_2,\dots,P_N)1 から N までの整数からなる順列
  • 入力される値はすべて整数

入力

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

N
P_1 P_2 \dots P_N

出力

i=1,2,\dots,N に対する答えをこの順に空白区切りで出力せよ。


入力例 1

5
1 3 5 4 2

出力例 1

3 3 3 5 3

例えば i=2 のとき、 (l,r)=(2,4) とすると r-l+1=3 は奇数であり、 (P_2,P_3,P_4)=(3,5,4) の中央値は 4 となり、 P_2 ではないため条件を満たします。よって答えは 3 です。

一方、i=4 のとき、 (l,r)=(4,4),(2,4),(3,5) に対して、 (P_l,\dots,P_r) の中央値は常に P_4=4 です。(l,r)=(1,5) とすると (P_1,P_2,P_3,P_4,P_5)=(1,3,5,4,2) の中央値は 3 となり、 P_4 ではないため条件を満たします。よって答えは 5 です。


入力例 2

3
2 1 3

出力例 2

-1 3 3

i=1 のとき、条件を満たす整数の組 (l,r) は存在しません。


入力例 3

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

出力例 3

5 3 3 7 3 3 3 5 3 3 5 3 3 3

Score: 600 points

Problem Statement

You are given a permutation P=(P_1,P_2,\dots,P_N) of integers from 1 to N.

For each i=1,2,\dots,N, print the minimum value of r-l+1 for a pair of integers (l,r) that satisfies all of the following conditions. If no such (l,r) exists, print -1.

  • 1 \leq l \leq i \leq r \leq N
  • r-l+1 is odd.
  • The median of the contiguous subsequence (P_l,P_{l+1},\dots,P_r) of P is not P_i.

Here, the median of A for an integer sequence A of length L (odd) is defined as the \frac{L+1}{2}-th value of the sequence A' obtained by sorting A in ascending order.

Constraints

  • 3 \leq N \leq 3 \times 10^5
  • (P_1,P_2,\dots,P_N) is a permutation of integers from 1 to N.
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N

Output

Print the answers for i=1,2,\dots,N in this order, separated by spaces.


Sample Input 1

5
1 3 5 4 2

Sample Output 1

3 3 3 5 3

For example, when i=2, if we set (l,r)=(2,4), then r-l+1=3 is odd, and the median of (P_2,P_3,P_4)=(3,5,4) is 4, which is not P_2, so the conditions are satisfied. Thus, the answer is 3.

On the other hand, when i=4, the median of (P_l,\dots,P_r) for any of (l,r)=(4,4),(2,4),(3,5) is P_4=4. If we set (l,r)=(1,5), the median of (P_1,P_2,P_3,P_4,P_5)=(1,3,5,4,2) is 3, which is not P_4, so the conditions are satisfied. Thus, the answer is 5.


Sample Input 2

3
2 1 3

Sample Output 2

-1 3 3

When i=1, no pair of integers (l,r) satisfies the conditions.


Sample Input 3

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

Sample Output 3

5 3 3 7 3 3 3 5 3 3 5 3 3 3
D - Bracket Walk

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 700

問題文

N 頂点 M 辺の有向グラフ G があります。頂点には 1 から N の番号が付いており、各辺には ( , ) のいずれかのラベルが付与されています。i 番目の辺は頂点 u_i から頂点 v_i に向かう辺であり、ラベル c_i が付与されています。このグラフは多重辺、自己ループを持ちません。

このグラフにおいては任意の 2 頂点 s,t に対して、 s から t に向かうパスが存在します。

グラフ G 上のウォークであって、以下の条件をすべて満たすものが存在するか判定してください。

  • ウォークの始点と終点は同じ頂点である
  • i=1,2,\dots,M に対して、 i 番目の辺はウォークに 1 回以上用いられる
  • ウォークに用いた辺のラベルを、辺の使用順に並べて得られる文字列は正しい括弧列である
ウォークとは グラフ G 上のウォークとは、 k 個( k は正整数)の頂点と k-1 個の辺を交互に並べた列 (v_1,e_1,v_2,\dots,v_{k-1},e_{k-1},v_k) であって、辺 e_i が頂点 v_i から頂点 v_{i+1} へ向かう辺であるようなものを指し、頂点 v_1,v_k をそれぞれウォークの始点、終点とよぶ。
正しい括弧列とは

正しい括弧列とは、以下のいずれかの条件を満たす文字列です。

  • 空文字列
  • ある正しい括弧列 A が存在して (, A, ) をこの順に連結した文字列
  • ある空でない正しい括弧列 A,B が存在して、 A,B をこの順に連結した文字列

制約

  • 2 \leq N \leq 4000
  • N \leq M \leq 8000
  • 1 \leq u_i,v_i \leq N
  • c_i( , ) のいずれか
  • u_i \neq v_i
  • i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
  • 入力される数値はすべて整数
  • 入力で与えられるグラフにおいて、任意の 2 頂点 s,t に対して、 s から t に向かうパスが存在する。

入力

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

N M
u_1 v_1 c_1
u_2 v_2 c_2
\vdots
u_M v_M c_M

出力

条件を満たすウォークが存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

5 7
1 2 (
2 3 )
3 4 (
4 1 )
2 4 )
4 5 (
5 1 )

出力例 1

Yes

1,2,3,4,1,5,6,7 をこの順で用いるウォークは、すべての辺を一度以上用いており、辺のラベルを使用順に並べて得られる文字列 ()()()() は正しい括弧列であるため、条件を満たします。

ウォークは同じ辺を 2 回以上使用したり、同じ頂点を 2 回以上訪れるものであっても構いません。


入力例 2

2 2
1 2 )
2 1 )

出力例 2

No

入力例 3

10 20
4 5 (
5 6 (
6 7 )
2 5 )
5 8 (
6 3 )
8 5 )
1 2 (
9 10 (
4 7 (
3 4 )
8 9 (
2 1 )
1 4 )
2 3 )
3 2 (
7 8 (
7 4 )
10 9 )
9 8 )

出力例 3

Yes

Score: 700 points

Problem Statement

You are given a directed graph G with N vertices and M edges. The vertices are numbered from 1 to N, and each edge is labeled with ( or ). The i-th edge is directed from vertex u_i to vertex v_i with a label c_i. The graph does not contain multi-edges or self-loops.

In this graph, for any two vertices s and t, there is a path from s to t.

Determine if there is a walk on the graph G that satisfies all of the following conditions:

  • The start and end vertices of the walk are the same.
  • For i=1,2,\dots,M, the i-th edge is used at least once in the walk.
  • The string obtained by arranging the labels of the edges used in the walk in the order of their usage is a regular bracket sequence.
What is a walk? A walk on a graph G is a sequence (v_1,e_1,v_2,\dots,v_{k-1},e_{k-1},v_k) of k vertices (k is a positive integer) and k-1 edges, where the edge e_i is directed from vertex v_i to vertex v_{i+1}. The vertices v_1 and v_k are called the start and end vertices of the walk, respectively.
What is a regular bracket sequence?

A regular bracket sequence is a string that satisfies one of the following conditions:

  • It is an empty string.
  • It is a string obtained by concatenating (, a regular bracket sequence A, and ) in this order.
  • It is a string obtained by concatenating two non-empty regular bracket sequences A and B in this order.

Constraints

  • 2 \leq N \leq 4000
  • N \leq M \leq 8000
  • 1 \leq u_i,v_i \leq N
  • c_i is ( or ).
  • u_i \neq v_i
  • If i \neq j, then (u_i,v_i) \neq (u_j,v_j).
  • All input values are integers.
  • In the input graph, for any two vertices s and t, there is a path from s to t.

Input

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

N M
u_1 v_1 c_1
u_2 v_2 c_2
\vdots
u_M v_M c_M

Output

If there is a walk satisfying the conditions, print Yes; otherwise, print No.


Sample Input 1

5 7
1 2 (
2 3 )
3 4 (
4 1 )
2 4 )
4 5 (
5 1 )

Sample Output 1

Yes

The walk that uses edges 1,2,3,4,1,5,6,7 in this order uses all the edges at least once, and the string ()()()() obtained by arranging the labels of the edges in the order of their usage is a regular bracket sequence, so all conditions are satisfied.

The walk may use the same edge multiple times or visit the same vertex multiple times.


Sample Input 2

2 2
1 2 )
2 1 )

Sample Output 2

No

Sample Input 3

10 20
4 5 (
5 6 (
6 7 )
2 5 )
5 8 (
6 3 )
8 5 )
1 2 (
9 10 (
4 7 (
3 4 )
8 9 (
2 1 )
1 4 )
2 3 )
3 2 (
7 8 (
7 4 )
10 9 )
9 8 )

Sample Output 3

Yes
E - Rearrange and Adjacent XOR

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。この整数列に対して以下の操作を N-1 回行って長さ 1 の整数列を得ることを考えます。

  • nA の長さとする。はじめに A 内の要素を好きなように並び替える。 その後、 A を長さ n-1 の非負整数列 (A_1 \oplus A_2, A_2 \oplus A_3, \dots, A_{n-1} \oplus A_n) に置き換える

ただしここで、 \oplus はビット単位 \mathrm{XOR} 演算を表します。

N-1 回の操作後に得られる長さ 1 の整数列が含む項の値を X としたとき、X として考えられる値の最大値を求めてください。

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 2 \leq N \leq 100
  • 0 \leq A_i < 2^{60}
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4
1 2 3 4

出力例 1

7

以下のような 3 回の操作により AA=(7) とできます。

  • 1 回目の操作にて、 A=(1,2,3,4)(3,1,4,2) と並び替える。 A(3 \oplus 1, 1 \oplus 4, 4 \oplus 2) = (2,5,6) に置き換わる。
  • 2 回目の操作にて、 A=(2,5,6)(2,6,5) と並び替える。 A(2 \oplus 6, 6 \oplus 5) = (4,3) に置き換わる。
  • 3 回目の操作にて、 A=(4,3)(4,3) と並び替える。 A(4 \oplus 3) = (7) に置き換わる。

入力例 2

13
451745518671773958 43800508384422957 153019271028231120 577708532586013562 133532134450358663 619750463276496276 615201966367277237 943395749975730789 813856754125382728 705285621476908966 912241698686715427 951219919930656543 124032597374298654

出力例 2

1152905479775702586

Score: 800 points

Problem Statement

You are given a sequence of N non-negative integers A=(A_1,A_2,\dots,A_N). Consider performing the following operation N-1 times on this sequence to obtain a sequence of length 1:

  • Let n be the length of A. First, rearrange the elements in A in any order you like. Then, replace A with a sequence of n-1 non-negative integers (A_1 \oplus A_2, A_2 \oplus A_3, \dots, A_{n-1} \oplus A_n).

Here, \oplus represents the bitwise \mathrm{XOR} operation.

Let X be the value of the term contained in the sequence of length 1 obtained after N-1 operations. Find the maximum possible value of X.

What is the bitwise \mathrm{XOR} operation?

The bitwise \mathrm{XOR} of two non-negative integers A and B, denoted as A \oplus B, is defined as follows:

  • In the binary representation of A \oplus B, the digit at the 2^k (k \geq 0) position is 1 if the digit at the 2^k position is 1 in A or B but not both, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 2 \leq N \leq 100
  • 0 \leq A_i < 2^{60}
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4
1 2 3 4

Sample Output 1

7

The sequence A can be transformed into A=(7) by the following three operations:

  • In the first operation, rearrange A=(1,2,3,4) to (3,1,4,2). A is replaced with (3 \oplus 1, 1 \oplus 4, 4 \oplus 2) = (2,5,6).
  • In the second operation, rearrange A=(2,5,6) to (2,6,5). A is replaced with (2 \oplus 6, 6 \oplus 5) = (4,3).
  • In the third operation, rearrange A=(4,3) to (4,3). A is replaced with (4 \oplus 3) = (7).

Sample Input 2

13
451745518671773958 43800508384422957 153019271028231120 577708532586013562 133532134450358663 619750463276496276 615201966367277237 943395749975730789 813856754125382728 705285621476908966 912241698686715427 951219919930656543 124032597374298654

Sample Output 2

1152905479775702586
F - Select and Split

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

正整数からなる集合が書かれている黒板があります。はじめ、黒板には集合 S=\lbrace 1,2,\dots,A,A+1,A+2,\dots,A+B\rbrace が書き込まれています。

高橋君は以下の操作を N-1 回行って、黒板に書き込まれている集合を N 個にしたいです。

  • 黒板に書かれている整数集合の中から、A 以下、 A+1 以上の要素をそれぞれ 1 つ以上持つ集合を 1 つ選ぶ。選んだ集合 S_0 の中から A 以下、 A+1 以上の要素を 1 つずつ選び、それぞれ a,b とする。黒板から集合 S_0 を消し、以下の条件をすべて満たすような 2 つの集合 S_1,S_2 を好きに選んで、黒板に書き込む
    • S_1,S_2 の和集合は S_0 であり、共通の要素を持たない
    • a \in S_1, b \in S_2

一連の操作の方法として考えられるものの数を 998244353 で割った余りを求めてください。

ただし、一連の操作はある i\ (1 \leq i \leq N-1) であって、 i 回目の操作で選んだ S_0,a,b,S_1,S_2 のいずれかが異なるものが存在するときに区別します。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A,B \leq 2 \times 10^5
  • N \leq A+B
  • 与えられる入力はすべて整数

入力

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

N A B

出力

答えを出力せよ。


入力例 1

3 2 4

出力例 1

1728

操作の一例として以下のようなものが考えられます。

  • S_0=\lbrace 1,2,3,4,5,6\rbrace を選び、 a=2,b=5 とし、 S_1 =\lbrace 1,2,3,6\rbrace, S_2=\lbrace 4,5\rbrace とする。黒板に書かれている整数集合は \lbrace 1,2,3,6\rbrace,\lbrace 4,5\rbrace2 つになる。
  • S_0=\lbrace 1,2,3,6\rbrace を選び、 a=1,b=3 とし、 S_1 = \lbrace1,2\rbrace, S_2=\lbrace 3,6\rbrace とする。黒板に書かれている整数集合は \lbrace 1,2\rbrace,\lbrace 3,6\rbrace,\lbrace 4,5\rbrace3 つになる。

入力例 2

4 1 3

出力例 2

6

1 回目の操作で a=1,b=2 とし、 S_1 = \lbrace 1\rbrace,S_2 = \lbrace 2,3,4\rbrace とすると、 2 回目以降の操作ができなくなります。

このように N-1 回操作をやりきらずに操作ができなくなるようなものは数えません。


入力例 3

5 6 6

出力例 3

84486693

入力例 4

173173 173173 173173

出力例 4

446948086

Score: 1200 points

Problem Statement

There is a blackboard with a set of positive integers written on it. Initially, the set S=\lbrace 1,2,\dots,A,A+1,A+2,\dots,A+B\rbrace is written on the blackboard.

Takahashi wants to perform the following operation N-1 times to have N sets on the blackboard:

  • From the sets of integers written on the blackboard, choose a set S_0 that contains at least one element not greater than A and at least one element not less than A+1. From the chosen set S_0, choose one element a not greater than A and one element b not less than A+1. Erase the set S_0 from the blackboard and write two sets S_1 and S_2 of his choice that satisfy all of the following conditions:
    • The union of S_1 and S_2 is S_0, and they have no common elements.
    • a \in S_1, b \in S_2

Find the number of possible ways to perform the series of operations, modulo 998244353.

Here, two series of operations are distinguished when there is an i\ (1 \leq i \leq N-1) such that S_0, a, b, S_1, or S_2 chosen in the i-th operation of one series is different from that of the other series.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A,B \leq 2 \times 10^5
  • N \leq A+B
  • All input values are integers.

Input

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

N A B

Output

Print the answer.


Sample Input 1

3 2 4

Sample Output 1

1728

One possible series of operations is as follows:

  • Choose S_0=\lbrace 1,2,3,4,5,6\rbrace, let a=2 and b=5, and let S_1 =\lbrace 1,2,3,6\rbrace and S_2=\lbrace 4,5\rbrace. There are now two sets of integers written on the blackboard: \lbrace 1,2,3,6\rbrace and \lbrace 4,5\rbrace.
  • Choose S_0=\lbrace 1,2,3,6\rbrace, let a=1 and b=3, and let S_1 = \lbrace1,2\rbrace and S_2=\lbrace 3,6\rbrace. There are now three sets of integers written on the blackboard: \lbrace 1,2\rbrace, \lbrace 3,6\rbrace, and \lbrace 4,5\rbrace.

Sample Input 2

4 1 3

Sample Output 2

6

If we let a=1 and b=2 and let S_1 = \lbrace 1\rbrace and S_2 = \lbrace 2,3,4\rbrace in the first operation, we can no longer perform the second and subsequent operations.

Do not count these cases where we cannot complete N-1 operations.


Sample Input 3

5 6 6

Sample Output 3

84486693

Sample Input 4

173173 173173 173173

Sample Output 4

446948086