103306: [Atcoder]ABC330 G - Inversion Squared

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $625$ points

Problem Statement

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

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

The inversion number of a permutation $P$ is the number of pairs of integers $(i,j)$ such that $1\leq i < j \leq N$ and $P_i > P_j$.

Constraints

  • $1\leq N\leq 3000$
  • $A_i = -1$ or $1\leq A_i \leq N$.
  • Each integer from $1$ to $N$ is contained at most once in $A$.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

$N$ 
$A_1$ $\ldots$ $A_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)$ and $P=(3,4,2,1)$, with inversion numbers $2$ and $5$, respectively.

Thus, the answer is $2^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 $998244353$.

Input

Output

得分:$625$分

问题描述

给你一个长度为$N$的序列$A = (A_1,\ldots,A_N)$。$A$中的每个元素要么是$-1$,要么是在$1$和$N$之间的整数,且每个从$1$到$N$的整数最多在$A$中出现一次。

一个排列$P=(P_1,\ldots,P_N)$是被称为< strong>好排列,当且仅当它满足$A_i \neq -1 \Rightarrow P_i = A_i$。找到所有好排列的逆序数平方和对$998244353$取模的结果。

一个排列$P$的逆序数是满足$1\leq i < j \leq N$且$P_i > P_j$的整数对$(i,j)$的数量。

约束

  • $1\leq N\leq 3000$
  • $A_i = -1$或$1\leq A_i \leq N$。
  • 每个从$1$到$N$的整数最多在$A$中出现一次。
  • 所有的输入值都是整数。

输入

从标准输入以如下格式获取输入:

$N$ 
$A_1$ $\ldots$ $A_N$

输出

打印答案。


样例输入1

4
3 -1 2 -1

样例输出1

29

有两个好排列:$P=(3,1,2,4)$和$P=(3,4,2,1)$,它们的逆序数分别为$2$和$5$。

因此,答案是$2^2 + 5^2 = 29$。


样例输入2

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

样例输出2

952235647

样例输入3

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

样例输出3

460544744

对$998244353$取模。

加入题单

算法标签: