#ARC132C. [ARC132C] Almost Sorted

[ARC132C] Almost Sorted

Score : 600600 points

Problem Statement

Given is a sequence a1,,ana_1,\dots,a_n consisting of 1,,n1,\dots, n and 1-1, along with an integer dd. How many sequences p1,,pnp_1,\dots,p_n satisfy the conditions below? Print the count modulo 998244353998244353.

  • p1,,pnp_1,\dots,p_n is a permutation of 1,,n1,\dots, n.
  • For each i=1,,ni=1,\dots,n, ai=pia_i=p_i if ai1a_i\neq -1. (That is, p1,,pnp_1,\dots,p_n can be obtained by replacing the 1-1s in a1,,ana_1,\dots,a_n in some way.)
  • For each i=1,,ni=1,\dots,n, piid|p_i - i|\le d.

Constraints

  • 1d51 \le d \le 5
  • d<n500d < n \le 500
  • 1ain1\le a_i \le n or ai=1a_i=-1.
  • aiid|a_i-i|\le d if ai1a_i\neq -1.
  • aiaja_i\neq a_j, if iji\neq j and ai,aj1a_i, a_j \neq -1.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

nn dd

a1a_1 \dots ana_n

Output

Print the number of sequences that satisfy the conditions, modulo 998244353998244353.

4 2
3 -1 1 -1
2

The conditions are satisfied by (3,2,1,4)(3,2,1,4) and (3,4,1,2)(3,4,1,2).

5 1
2 3 4 5 -1
0

The only permutation of 1,2,3,4,51,2,3,4,5 that can be obtained by replacing the 1-1 is (2,3,4,5,1)(2,3,4,5,1), whose fifth term violates the condition, so the answer is 00.

16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
794673086

Print the count modulo 998244353998244353.