#P7623. [AHOI2021初中组] 收衣服

[AHOI2021初中组] 收衣服

Description

Looking at so many clothes covered with dust, the two of them felt helpless. Also, some clothes cannot be washed together. To sort them into categories, Xiaokeke gave each piece of clothing a distinct label from 1n1 \sim n, where nn is the number of clothes. It would be more convenient to wash them after arranging the clothes in the order 1,2,,n1,2,\ldots,n.

Xiaokeke also thought that we can take out a continuous segment of the clothes rack, reverse its order by hand, and then put it back. As an OI contestant, you immediately abstracted Xiaokeke’s algorithm for sorting clothes: let the label of the ii-th piece of clothing from left to right initially be pip_i. Enumerate ii in the order 1,2,,n11,2,\ldots,n-1. Let the smallest label among pi,pi+1,,pnp_i,p_{i+1},\ldots,p_n be pjp_j. Then reverse pi,pi+1,,pj1,pjp_i,p_{i+1},\ldots,p_{j-1},p_j from left to right, turning it into pj,pj1,,pi+1,pip_j,p_{j-1},\ldots,p_{i+1},p_i.

Xiaoxue soon discovered that although Xiaokeke’s algorithm looks impressive, it is actually quite silly—in the dim light, nobody can tell the labels on the clothes. So they could only return to the room for rational enjoyment: assume the cost of reversing the interval [i,j][i,j] is wi,jw_{i,j}. The cost of one sorting process is the sum of the operation costs of all reversals. Now Xiaokeke wants to know, when pp ranges over all n!n! permutations, the total sorting cost over all cases.

You only need to output the answer modulo 998244353998244353 (=7×17×223+1=7 \times 17 \times 2^{23} + 1, a prime).

Input Format

The first line contains an integer nn.

The next n1n-1 lines: in line ii (1in)(1 \le i \le n), there are ni+1n - i + 1 space-separated integers, where the jj-th one denotes wi,jw_{i,j}.

Output Format

One line containing an integer, which is the answer modulo 998244353998244353.

5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1080
见附加文件的 sort2.in。 
见附加文件的 sort2.ans。

Hint

[Sample 1 Explanation]

Let us give an example. When p=[3,2,5,1,4]p=[3,2,5,1,4], the steps of the algorithm are as follows:

  • When executing i=1i=1, the minimum among p1,p2,p3,p4,p5p_1,p_2,p_3,p_4,p_5, i.e. 3,2,5,1,43,2,5,1,4, is p4=1p_4=1. We reverse interval [1,4][1,4], and pp becomes [1,5,2,3,4][1,5,2,3,4], with cost w1,4=4w_{1,4}=4.
  • When executing i=2i=2, the minimum among p2,p3,p4,p5p_2,p_3,p_4,p_5, i.e. 5,2,3,45,2,3,4, is p3=2p_3=2. We reverse interval [2,3][2,3], and pp becomes [1,2,5,3,4][1,2,5,3,4], with cost w2,3=2w_{2,3}=2.
  • When executing i=3i=3, the minimum among p3,p4,p5p_3,p_4,p_5, i.e. 5,3,45,3,4, is p4=3p_4=3. We reverse interval [3,4][3,4], and pp becomes [1,2,3,5,4][1,2,3,5,4], with cost w3,4=2w_{3,4}=2.
  • When executing i=4i=4, the minimum among p4,p5p_4,p_5, i.e. 5,45,4, is p5=4p_5=4. We reverse interval [4,5][4,5], and pp becomes [1,2,3,4,5][1,2,3,4,5], with cost w4,5=2w_{4,5}=2.

As you can see, after the ii-th step ends, the positions [1,i][1,i] of the sequence are exactly the clothes labeled [1,i][1,i]. After the algorithm finishes, pp is sorted. The total cost of this sorting is 4+2+2+2=104+2+2+2=10.

Note: The algorithm will always execute n1n-1 steps, and it will not terminate early even if it becomes sorted in the middle.

[Constraints and Hints]

Hint: The input size of this problem is large, so please avoid overly slow input methods.

  • For 25%25\% of the testdata, 1n91 \le n \le 9.
  • For 50%50\% of the testdata, 1n161 \le n \le 16.
  • For 70%70\% of the testdata, 1n501 \le n \le 50.
  • For another 15%15\% of the testdata, wi,j=1w_{i,j}=1.
  • For 100%100\% of the testdata, 1n5001 \le n \le 500, 0wi,j<9982443530 \le w_{i,j} < 998244353.

Translated by ChatGPT 5