A - Antichain of Integer Strings

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数からなる集合 A は次の条件を満たすとき、良い集合であるといいます。

  • 任意の相異なる 2 要素 a, b \in A に対して、a10 進法表記した文字列は、b10 進法表記した文字列の部分文字列ではない
部分文字列とは? 部分文字列とは連続する部分列のことを指します。例えば 1, 12, 23123 の部分文字列ですが、2113123 の部分文字列ではありません。


正整数 L, R が与えられます。L 以上 R 以下の整数からなる良い集合 A の要素数の最大値を求めてください。

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

制約

  • 1\leq T\leq 10^4
  • 1\leq L\leq R\leq 10^{9}

入力

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

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

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

L R

出力

T 行出力してください。i 行目には、\text{case}_i に対する答えを出力してください。


入力例 1

3
3 8
3 18
1 1000

出力例 1

6
10
900

はじめの 2 つのテストケースについて、例えば次の A が要素数が最大であるような良い集合となります。

  • 1 つめのテストケース:A=\{3,4,5,6,7,8\}.
  • 2 つめのテストケース:A=\{3,4,6,8,9,10,11,12,15,17\}.

Score : 400 points

Problem Statement

A set A of positive integers is said to be good when it satisfies the following condition.

  • For any two distinct elements a, b \in A, the string representing a in base ten is not a substring of the string representing b in base ten.
What is a substring? A substring of a string is its contiguous subsequence. For example, 1, 12, and 23 are substrings of 123, while 21 and 13 are not.


You are given positive integers L and R. Find the maximum possible number of elements in a good set A consisting of integers between L and R (inclusive).

We will give you T test cases; solve each of them.

Constraints

  • 1\leq T\leq 10^4
  • 1\leq L\leq R\leq 10^{9}

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

L R

Output

Print T lines. The i-th line should contain the answer for \text{case}_i.


Sample Input 1

3
3 8
3 18
1 1000

Sample Output 1

6
10
900

For the first two cases, the following are good sets A with the maximum number of elements.

  • Case 1: A=\{3,4,5,6,7,8\}.
  • Case 2: A=\{3,4,6,8,9,10,11,12,15,17\}.
B - 2A + x

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 700

問題文

正整数列 A = (A_1, A_2, \ldots, A_N) および正整数 X が与えられます。あなたはこの数列に対して、次の操作を何度でも行うことができます(0 回でもよい):

  • 添字 i1\leq i\leq N)および、0\leq x\leq X となる非負整数 x を選ぶ。A_i2A_i+x に変更する。

操作結果の \max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\} としてありうる最小値を求めてください。

制約

  • 2\leq N\leq 10^5
  • 1\leq X\leq 10^9
  • 1\leq A_i\leq 10^9

入力

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

N X
A_1 A_2 \ldots A_N

出力

操作結果の \max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\} としてありうる最小値を出力してください。


入力例 1

4 2
5 8 12 20

出力例 1

6

A_i2A_i+x に変更する操作を (i, x) と表すことにします。最適な操作列の一例は次の通りです。

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

操作結果は A = (21, 18, 24, 20) となり、\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 6 が達成できます。


入力例 2

4 5
24 25 26 27

出力例 2

0

最適な操作列の一例は次の通りです。

  • (1,5), (1,5), (2,5), (2,1), (3,2), (3,3), (4,0), (4,3)

操作結果は A = (111,111,111,111) となり、\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 0 が達成できます。


入力例 3

4 1
24 25 26 27

出力例 3

3

一度も操作を行わないことにより、\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 3 が達成できます。


入力例 4

10 5
39 23 3 7 16 19 40 16 33 6

出力例 4

13

Score : 700 points

Problem Statement

You are given a sequence A = (A_1, A_2, \ldots, A_N) of positive integers and a positive integer X. You can perform the operation below any number of times, possibly zero:

  • Choose an index i (1\leq i\leq N) and a non-negative integer x such that 0\leq x\leq X. Change A_i to 2A_i+x.

Find the smallest possible value of \max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\} after your operations.

Constraints

  • 2\leq N\leq 10^5
  • 1\leq X\leq 10^9
  • 1\leq A_i\leq 10^9

Input

Input is given from Standard Input in the following format:

N X
A_1 A_2 \ldots A_N

Output

Print the smallest possible value of \max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\} after your operations.


Sample Input 1

4 2
5 8 12 20

Sample Output 1

6

Let (i, x) denote an operation that changes A_i to 2A_i+x. An optimal sequence of operations is

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

which yields A = (21, 18, 24, 20), achieving \max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 6.


Sample Input 2

4 5
24 25 26 27

Sample Output 2

0

An optimal sequence of operations is

  • (1,5), (1,5), (2,5), (2,1), (3,2), (3,3), (4,0), (4,3)

which yields A = (111,111,111,111), achieving \max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 0.


Sample Input 3

4 1
24 25 26 27

Sample Output 3

3

Performing no operations achieves \max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 3.


Sample Input 4

10 5
39 23 3 7 16 19 40 16 33 6

Sample Output 4

13
C - Increment or Xor

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

正整数 N および、2^N 項からなる数列 A = (A_0, A_1, \ldots, A_{2^N-1}) が与えられます。ここで各 A_i0 以上 2^N-1 以下の整数であり、i\neq j ならば A_i\neq A_j が成り立ちます。

あなたは数列 A に対して、次の 2 種類の操作を行うことができます:

  • 操作 +:すべての i に対して、A_i(A_i + 1) \bmod 2^N に変更する。
  • 操作 \oplus0 以上 2^N-1 以下の整数 x を選ぶ。すべての i に対して A_iA_i\oplus x に変更する。

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

ビット単位 \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)。


あなたの目的は、すべての i に対して A_i = i が成り立つようにすることです。目的を達成することが可能であるかを判定してください。本問題の制約下では、目的を達成することが可能な場合には、操作回数を 10^6 回以下にできることが証明できます。そのような操作列を出力してください。

制約

  • 1\leq N\leq 18
  • 0\leq A_i \leq 2^N - 1
  • i\neq j ならば A_i\neq A_j

入力

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

N
A_0 A_1 \ldots A_{2^N - 1}

出力

すべての i に対して A_i = i が成り立つようにすることが可能ならば Yes、そうでなければ No を出力してください。

Yes の場合には、さらに目的を達成するための操作列を次の形式で出力してください。

K
x_1 x_2 \ldots x_K

ここで K は操作の回数を表す非負整数で、0\leq K\leq 10^6 を満たす必要があります。また、x_ii 回目の操作を表す整数で、

  • i 回目に操作 + を行う場合には、x_i=-1
  • i 回目に操作 \oplus を行う場合には、x_i はその操作で選ぶ整数 x

と定めてください。

答が複数考えられる場合には、そのどれを出力しても正解となります。


入力例 1

3
5 0 3 6 1 4 7 2

出力例 1

Yes
4
-1 6 -1 1

出力の操作列によって、数列 A は次のように変化します。

  • 初期状態:A = (5,0,3,6,1,4,7,2)
  • 操作 +A = (6,1,4,7,2,5,0,3)
  • 操作 \oplus (x = 6):A = (0,7,2,1,4,3,6,5)
  • 操作 +A = (1,0,3,2,5,4,7,6)
  • 操作 \oplus (x = 1):A = (0,1,2,3,4,5,6,7)

入力例 2

3
2 5 4 3 6 1 0 7

出力例 2

No

どのように操作を行っても、目的を達成することはできません。


入力例 3

3
0 1 2 3 4 5 6 7

出力例 3

Yes
0

0 回の操作により目的が達成できます。なお、空行の出力の有無は、ジャッジ結果に影響しません。

Score : 1100 points

Problem Statement

You are given a positive integer N and a sequence A = (A_0, A_1, \ldots, A_{2^N-1}) of 2^N terms, where each A_i is an integer between 0 and 2^N-1 (inclusive) and A_i\neq A_j holds if i\neq j.

You can perform the following two kinds of operations on A:

  • Operation +: For every i, change A_i to (A_i + 1) \bmod 2^N.
  • Operation \oplus: Choose an integer x between 0 and 2^N-1. For every i, change A_i to A_i\oplus x.

Here, \oplus denotes bitwise \mathrm{XOR}.

What is bitwise \mathrm{XOR}?

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

  • When A \oplus B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110).


Your objective is to make A_i = i for every i. Determine whether it is achievable. It can be proved that, under the Constraints of this problem, one can achieve the objective with at most 10^6 operations if it is achievable at all. Print such a sequence of operations.

Constraints

  • 1\leq N\leq 18
  • 0\leq A_i \leq 2^N - 1
  • A_i\neq A_j, if i\neq j

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 \ldots A_{2^N - 1}

Output

If it is possible to make A_i = i for every i, print Yes; otherwise, print No.

In the case of Yes, it should be followed by a sequence of operations that achieves the objective, in the following format:

K
x_1 x_2 \ldots x_K

Here, K is a non-negative integer representing the number of operations, which must satisfy 0\leq K\leq 10^6; x_i is an integer representing the i-th operation, which should be set as follows.

  • If the i-th operation is Operation +, x_i=-1.
  • If the i-th operation is Operation \oplus, x_i should be the integer chosen in that operation.

If there are multiple possible solutions, you may print any of them.


Sample Input 1

3
5 0 3 6 1 4 7 2

Sample Output 1

Yes
4
-1 6 -1 1

The sequence of operations above changes the sequence A as follows.

  • Initially: A = (5,0,3,6,1,4,7,2)
  • Operation +: A = (6,1,4,7,2,5,0,3)
  • Operation \oplus (x = 6):A = (0,7,2,1,4,3,6,5)
  • Operation +: A = (1,0,3,2,5,4,7,6)
  • Operation \oplus (x = 1):A = (0,1,2,3,4,5,6,7)

Sample Input 2

3
2 5 4 3 6 1 0 7

Sample Output 2

No

No sequence of operations achieves the objective.


Sample Input 3

3
0 1 2 3 4 5 6 7

Sample Output 3

Yes
0

Performing zero operations achieves the objective. Whether an empty line is printed or not does not affect the verdict.

D - Sum Avoidance

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

正整数 S, K が与えられます。正整数列 A = (A_1, A_2, \ldots, A_N) は、次の 2 条件を満たすとき、良い数列であるといいます。

  • 1\leq A_1 < A_2 < \cdots < A_N \leq S - 1 が成り立つ。
  • 任意の非負整数列 (x_1, x_2, \ldots, x_N) に対して \sum_{i=1}^NA_ix_i\neq S が成り立つ。

項数 N が最大であるような良い数列のうち、辞書順最小のものを A = (A_1, A_2, \ldots, A_N) とします。この数列の第 KA_K を出力してください。ただし K > N である場合には -1 と出力してください。

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

制約

  • 1\leq T\leq 1000
  • 3\leq S\leq 10^{18}
  • 1\leq K \leq S - 1

入力

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

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

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

S K

出力

T 行出力してください。i 行目には、\text{case}_i に対する答えを出力してください。


入力例 1

13
3 1
3 2
7 1
7 2
7 3
7 4
10 1
10 2
10 3
10 4
10 5
2022 507
1000000000000000000 999999999999999999

出力例 1

2
-1
2
4
6
-1
3
6
8
9
-1
1351
-1

S = 3, 7, 10 の場合には、A は次の数列になります。

  • S=3 の場合:A = (2)
  • S=7 の場合:A = (2,4,6)
  • S=10 の場合:A = (3,6,8,9)

Score : 1100 points

Problem Statement

You are given positive integers S and K. A sequence of positive integers A = (A_1, A_2, \ldots, A_N) is said to be a good sequence when satisfying the following two conditions.

  • 1\leq A_1 < A_2 < \cdots < A_N \leq S - 1 holds.
  • \sum_{i=1}^NA_ix_i\neq S holds for any sequence of non-negative integers (x_1, x_2, \ldots, x_N).

Let A = (A_1, A_2, \ldots, A_N) be the lexicographically smallest of the good sequences with the maximum number N of terms. Print the K-th term A_K of this sequence, or -1 if K > N.

We will give you T test cases; solve each of them.

Constraints

  • 1\leq T\leq 1000
  • 3\leq S\leq 10^{18}
  • 1\leq K \leq S - 1

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

S K

Output

Print T lines. The i-th line should contain the answer for \text{case}_i.


Sample Input 1

13
3 1
3 2
7 1
7 2
7 3
7 4
10 1
10 2
10 3
10 4
10 5
2022 507
1000000000000000000 999999999999999999

Sample Output 1

2
-1
2
4
6
-1
3
6
8
9
-1
1351
-1

For the cases S = 3, 7, 10, the sequence A will be as follows.

  • For S=3: A = (2).
  • For S=7: A = (2,4,6).
  • For S=10: A = (3,6,8,9).
E - RowCol/ColRow Sort

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

H\times W 行列 A = (A_{i,j}) (1\leq i\leq H, 1\leq j\leq W) に対して、次の操作を考えます。

  • 行ソート:すべての行を昇順にソートする。つまり、すべての i に対して A_{i,1},\ldots,A_{i,W} を昇順にソートする。
  • 列ソート:すべての列を昇順にソートする。つまり、すべての j に対して A_{1,j},\ldots,A_{H,j} を昇順にソートする。

H\times W 行列 B = (B_{i,j}) が与えられます。次の 2 条件をともに満たす H\times W 行列 A の総数を 998244353 で割った余りを求めてください。

  • A に対して行ソート、列ソートをこの順に行った結果は B に一致する。
  • A に対して列ソート、行ソートをこの順に行った結果は B に一致する。

制約

  • 1\leq H, W\leq 1500
  • 0\leq B_{i,j}\leq 9
  • 任意の 1\leq i\leq H および 1\leq j\leq W - 1 に対して B_{i,j}\leq B_{i,j+1}
  • 任意の 1\leq j\leq W および 1\leq i\leq H - 1 に対して B_{i,j}\leq B_{i+1,j}
  • 入力される値はすべて整数

入力

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

H W
B_{1,1} B_{1,2} \ldots B_{1,W}
\vdots
B_{H,1} B_{H,2} \ldots B_{H,W}

出力

条件を満たす行列 A の総数を 998244353 で割った余りを出力してください。


入力例 1

2 2
0 0
1 2

出力例 1

4

条件を満たす行列は次の 4 つです:\begin{pmatrix}0&0\\1&2\end{pmatrix}, \begin{pmatrix}0&0\\2&1\end{pmatrix}, \begin{pmatrix}1&2\\0&0\end{pmatrix}, \begin{pmatrix}2&1\\0&0\end{pmatrix}.

例えば、\begin{pmatrix}2&1\\0&0\end{pmatrix} が条件を満たすことは次のように確かめられます。

  • 行ソート、列ソートをこの順に行う場合:\begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}1&2\\0&0\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}.

  • 列ソート、行ソートをこの順に行う場合:\begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}0&0\\2&1\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}.


入力例 2

3 3
0 1 3
2 4 7
5 6 8

出力例 2

576

例えば \begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix} が条件を満たします。このことは次のように確かめられます。

  • 行ソート、列ソートをこの順に行う場合:\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}5&6&7\\0&1&3\\2&4&8\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}.

  • 列ソート、行ソートをこの順に行う場合:\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}3&0&1\\4&7&2\\5&8&6\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}.


入力例 3

3 5
0 0 0 1 1
0 0 1 1 2
0 1 1 2 2

出力例 3

10440

入力例 4

1 7
2 3 3 6 8 8 9

出力例 4

1260

Score : 1400 points

Problem Statement

Consider the following operations on an H\times W matrix A = (A_{i,j}) (1\leq i\leq H, 1\leq j\leq W).

  • Row-sort: Sort every row in ascending order. That is, sort A_{i,1},\ldots,A_{i,W} in ascending order for every i.
  • Column-sort: Sort every column in ascending order. That is, sort A_{1,j},\ldots,A_{H,j} in ascending order for every j.

You are given an H\times W matrix B = (B_{i,j}). Find the number of H\times W matrices A that satisfy both of the following conditions, modulo 998244353.

  • Performing row-sort and then column-sort on A produces B.
  • Performing column-sort and then row-sort on A produces B.

Constraints

  • 1\leq H, W\leq 1500
  • 0\leq B_{i,j}\leq 9
  • B_{i,j}\leq B_{i,j+1}, for any 1\leq i\leq H and 1\leq j\leq W - 1.
  • B_{i,j}\leq B_{i+1,j}, for any 1\leq j\leq W and 1\leq i\leq H - 1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
B_{1,1} B_{1,2} \ldots B_{1,W}
\vdots
B_{H,1} B_{H,2} \ldots B_{H,W}

Output

Print the number of matrices A that satisfy the conditions, modulo 998244353.


Sample Input 1

2 2
0 0
1 2

Sample Output 1

4

The four matrices that satisfy the conditions are:

\begin{pmatrix}0&0\\1&2\end{pmatrix}, \begin{pmatrix}0&0\\2&1\end{pmatrix}, \begin{pmatrix}1&2\\0&0\end{pmatrix}, \begin{pmatrix}2&1\\0&0\end{pmatrix}.

We can verify that \begin{pmatrix}2&1\\0&0\end{pmatrix}, for example, satisfies the conditions as follows.

  • Performing row-sort and then column-sort: \begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}1&2\\0&0\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}.

  • Performing column-sort and then row-sort:\begin{pmatrix}2&1\\0&0\end{pmatrix}\to \begin{pmatrix}0&0\\2&1\end{pmatrix} \to \begin{pmatrix}0&0\\1&2\end{pmatrix}.


Sample Input 2

3 3
0 1 3
2 4 7
5 6 8

Sample Output 2

576

\begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}, for example, satisfies the conditions, which can be verified as follows.

  • Performing row-sort and then column-sort: \begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}5&6&7\\0&1&3\\2&4&8\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}.

  • Performing column-sort and then row-sort: \begin{pmatrix}5&7&6\\3&0&1\\4&8&2\end{pmatrix}\to \begin{pmatrix}3&0&1\\4&7&2\\5&8&6\end{pmatrix} \to \begin{pmatrix}0&1&3\\2&4&7\\5&6&8\end{pmatrix}.


Sample Input 3

3 5
0 0 0 1 1
0 0 1 1 2
0 1 1 2 2

Sample Output 3

10440

Sample Input 4

1 7
2 3 3 6 8 8 9

Sample Output 4

1260
F - Reflection

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1800

問題文

互いに区別のできない 3 つの石が、数直線上の座標が整数の点に置かれています。これらの石に対して次の操作を考えます:

  • 3 つの石を、その時点で座標の小さい方から順に(同じ位置にある石については適当な順に) A, B, C とする。次のいずれかを行う。
    • AB に関して対称な位置に移動する。
    • CB に関して対称な位置に移動する。

はじめに 3 つの石が置かれている座標 a, b, c が与えられます。操作を何度でも行える(0 回でもよい)とき、操作結果の 3 つの石の座標の組合せとしてありうるものの個数を 998244353 で割った余りを求めてください。

石が互いに区別できないことに注意してください。より厳密にいえば、3 つの石の座標の組合せを多重集合として数え上げてください。

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

制約

  • 1\leq T\leq 10^5
  • -10^{18}\leq a\leq b\leq c\leq 10^{18}

入力

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

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

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

a b c

出力

T 行出力してください。i 行目には、\text{case}_i に対する答えを出力してください。


入力例 1

6
1 3 5
-2 -2 5
0 1 3
31 41 59
-123456789 0 987654321
-1000000000000000000 0 1000000000000000000

出力例 1

5
2
9
70
182333351
5

(a,b,c) = (1,3,5) である場合、操作結果の 3 つの石の座標の組合せとしてありうるのは次の 5 通りです:

  • (1,3,5), (1,1,3), (-1,1,1), (3,5,5), (5,5,7)

次の図も参考にしてください。


Score : 1800 points

Problem Statement

There are three indistinguishable stones at integer coordinates on a number line. Consider the following operation on these stones:

  • Let A, B, C be the three stones in ascending order of coordinate (ties broken arbitrarily). Perform one of the following.
    • Move A to the symmetric position with respect to B.
    • Move C to the symmetric position with respect to B.

You are given the initial coordinates a, b, c of the three stones. Find the number, modulo 998244353, of possible combinations of coordinates of the three stones after zero or more operations.

Note that the stones are indistinguishable. More strictly speaking, count the possible multisets of coordinates of the three stones.

We will give you T test cases; solve each of them.

Constraints

  • 1\leq T\leq 10^5
  • -10^{18}\leq a\leq b\leq c\leq 10^{18}

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

a b c

Output

Print T lines. The i-th line should contain the answer for \text{case}_i.


Sample Input 1

6
1 3 5
-2 -2 5
0 1 3
31 41 59
-123456789 0 987654321
-1000000000000000000 0 1000000000000000000

Sample Output 1

5
2
9
70
182333351
5

For (a,b,c) = (1,3,5), the five possible combinations of the three stones after operations are:

  • (1,3,5), (1,1,3), (-1,1,1), (3,5,5), (5,5,7).

The figure below may be helpful.