atcoder#ARC118E. [ARC118E] Avoid Permutations

[ARC118E] Avoid Permutations

Score : 800800 points

Problem Statement

Let us consider the problem below for a permutation P=(P1,P2,,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N), and denote the answer by f(P)f(P).

We have an (N+2)×(N+2)(N+2)\times (N+2) grid, where the rows are numbered 0,1,,N+10, 1, \ldots, N+1 from top to bottom and the columns are numbered 0,1,,N+10, 1, \ldots, N+1 from left to right. Let (i,j)(i,j) denote the square at the ii-th row and jj-th column.

Consider paths from (0,0)(0, 0) to (N+1,N+1)(N+1, N+1) where we repeatedly move one square right or one square down. However, for each 1iN1\leq i\leq N, we must not visit the square (i,Pi)(i, P_i). How many such paths are there?

You are given a positive integer NN and an integer sequence A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N), where each AiA_i is 1-1 or an integer between 11 and NN (inclusive).

Consider all permutations P=(P1,P2,,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N) satisfying Ai1    Pi=AiA_i\neq -1 \implies P_i = A_i. Find the sum of f(P)f(P) over all such PP, modulo 998244353998244353.

Constraints

  • 1N2001\leq N\leq 200
  • Ai=1A_i = -1 or 1AiN1\leq A_i\leq N.
  • AiAjA_i\neq A_j if Ai1A_i\neq -1 and Aj1A_j\neq -1, for iji\neq j.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

4
1 -1 4 -1
41

We want to find the sum of f(P)f(P) over two permutations P=(1,2,4,3)P = (1,2,4,3) and P=(1,3,4,2)P = (1,3,4,2). The figure below shows the impassable squares for each of them, where . and # represent a passable and impassable square, respectively.

......         ......
  .#....         .#....
  ..#...         ...#..
  ....#.         ....#.
  ...#..         ..#...
  ......         ......
P=(1,2,4,3)    P=(1,3,4,2)
  • For P=(1,2,4,3)P = (1,2,4,3), we have f(P)=18f(P) = 18.
  • For P=(1,3,4,2)P = (1,3,4,2), we have f(P)=23f(P) = 23.

The answer is the sum of them: 4141.

4
4 3 2 1
2

In this sample, we want to compute f(P)f(P) for just one permutation P=(4,3,2,1)P = (4,3,2,1).

3
-1 -1 -1
48
  • For P=(1,2,3)P = (1,2,3), we have f(P)=10f(P) = 10.
  • For P=(1,3,2)P = (1,3,2), we have f(P)=6f(P) = 6.
  • For P=(2,1,3)P = (2,1,3), we have f(P)=6f(P) = 6.
  • For P=(2,3,1)P = (2,3,1), we have f(P)=12f(P) = 12.
  • For P=(3,1,2)P = (3,1,2), we have f(P)=12f(P) = 12.
  • For P=(3,2,1)P = (3,2,1), we have f(P)=2f(P) = 2.

The answer is the sum of them: 4848.