atcoder#ABC290F. [ABC290F] Maximum Diameter

[ABC290F] Maximum Diameter

Score : 500500 points

Problem Statement

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

  • A tree with NN vertices is said to be good if and only if the degree of the ii-th (1iN)(1 \leq i \leq N) vertex is XiX_i. If a good tree exists, f(X)f(X) is the maximum diameter of a good tree; if it doesn't, f(X)=0f(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 998244353998244353, of f(X)f(X) over all possible sequences XX of length NN consisting of positive integers. We can prove that the sum of f(X)f(X) is a finite value.

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

Constraints

  • 1T2×1051\leq T \leq 2\times 10^5
  • 2N1062 \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 testi\mathrm{test}_i denotes the ii-th test case:

TT

test1\mathrm{test}_1

test2\mathrm{test}_2

\vdots

testT\mathrm{test}_T

Each test case is given in the following format:

NN

Output

Print TT lines.

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

10
2
3
5
8
13
21
34
55
89
144
1
6
110
8052
9758476
421903645
377386885
881422708
120024839
351256142

If N=3N=3,

for example,

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

3-vertex tree

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