atcoder#ARC140D. [ARC140D] One to One

[ARC140D] One to One

Score : 700700 points

Problem Statement

For an integer sequence X=(X1,X2,,XN)X=(X_1,X_2,\dots,X_N) of length NN whose elements are all between 11 and NN (inclusive), consider the question below, and let f(X)f(X) be the answer.

$$G$$$$N$$$$G$$$$N$$$$i$$$$i$$$$X_i$$$$G$$You are given an integer sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$, where each $A_i$ is an integer between $1$ and $N$ (inclusive) or $-1$. $$

Consider an integer sequence X=(X1,X2,,XN)X=(X_1,X_2,\dots,X_N) of length NN whose elements are all between 11 and NN such that Ai1Ai=XiA_i \neq -1 \Rightarrow A_i = X_i. Find the sum of f(X)f(X) over all such XX, modulo 998244353998244353.

Constraints

  • 1N20001 \le N \le 2000
  • AiA_i is between 11 and NN (inclusive) or 1-1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Prin the answer.

3
-1 1 3
5

There are three sequences XX satisfying the requirement, as follows.

  • X=(1,1,3)X=(1,1,3), for which the answer to the question is 22.
  • X=(2,1,3)X=(2,1,3), for which the answer to the question is 22.
  • X=(3,1,3)X=(3,1,3), for which the answer to the question is 11.

Thus, the answer is 2+2+1=52+2+1=5.

1
1
1
8
-1 3 -1 -1 8 -1 -1 -1
433760