#abc330g. G - Inversion Squared

G - Inversion Squared

Score : 625625 points

问题描述

给定一个长度为 NN 的序列 A=(A1,,AN)A = (A_1,\ldots,A_N)。序列 AA 中的每个元素要么是 1-1,要么是介于 11NN 之间的整数,并且从 11NN 的每个整数最多出现一次。

若一个排列 P=(P1,,PN)P=(P_1,\ldots,P_N) 满足 Ai1Pi=AiA_i \neq -1 \Rightarrow P_i = A_i,则称其为 良排列。请计算所有良排列的倒序数平方和,结果对 998244353998244353 取模。

排列 PP 的倒序数是指满足条件 1i<jN1\leq i < j \leq NPi>PjP_i > P_j 的整数对 (i,j)(i,j) 的数量。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence A=(A1,,AN)A = (A_1,\ldots,A_N) of length NN. Each element of AA is either 1-1 or an integer between 11 and NN, and each integer from 11 to NN is contained at most once.

A permutation P=(P1,,PN)P=(P_1,\ldots,P_N) of (1,,N)(1,\ldots,N) is called a good permutation if and only if it satisfies Ai1Pi=AiA_i \neq -1 \Rightarrow P_i = A_i. Find the sum of the squares of the inversion numbers of all good permutations, modulo 998244353998244353.

The inversion number of a permutation PP is the number of pairs of integers (i,j)(i,j) such that 1i<jN1\leq i < j \leq N and Pi>PjP_i > P_j.

Constraints

  • 1N30001\leq N\leq 3000
  • Ai=1A_i = -1 or 1AiN1\leq A_i \leq N.
  • Each integer from 11 to NN is contained at most once in AA.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 \ldots ANA_N

Output

Print the answer.

Sample Input 1

4
3 -1 2 -1

Sample Output 1

29

There are two good permutations: P=(3,1,2,4)P=(3,1,2,4) and P=(3,4,2,1)P=(3,4,2,1), with inversion numbers 22 and 55, respectively.

Thus, the answer is 22+52=292^2 + 5^2 = 29.

Sample Input 2

10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Sample Output 2

952235647

Sample Input 3

15
-1 -1 10 -1 -1 -1 2 -1 -1 3 -1 -1 -1 -1 1

Sample Output 3

460544744

Find the sum modulo 998244353998244353.

update @ 2024/3/10 01:14:57