#arc178d. D - Delete Range Mex

D - Delete Range Mex

Score : 700700 points

问题陈述

给定一个正整数 NN 和一个包含 MM 个非负整数的序列 A=(A1,A2,,AM)A = (A_{1}, A_{2}, \dots, A_{M})

这里,AA 中的所有元素都是介于 00N1N-1 之间的不同整数。

找出排列 PP 的数量,模 998244353998244353,满足以下条件的 (0,1,,N1)(0, 1, \dots, N-1)

  • 在将序列 B=(B1,B2,,BN)B = (B_{1}, B_{2}, \dots, B_{N}) 初始化为 PP 后,通过重复以下操作若干次,可以使 B=AB = A
    • 选择 llrr 使得 1lrB1 \leq l \leq r \leq |B|,如果 mex({Bl,Bl+1,,Br})\mathrm{mex}(\{B_{l}, B_{l+1}, \dots, B_{r}\}) 包含在 BB 中,则将其从 BB 中移除。

什么是 mex(X)\mathrm{mex}(X)?对于有限的非负整数集合 XXmex(X)\mathrm{mex}(X) 定义为不在 XX 中的最小非负整数。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a positive integer NN and a sequence of MM non-negative integers A=(A1,A2,,AM)A = (A_{1}, A_{2}, \dots, A_{M}).

Here, all elements of AA are distinct integers between 00 and N1N-1, inclusive.

Find the number, modulo 998244353998244353, of permutations PP of (0,1,,N1)(0, 1, \dots, N-1) that satisfy the following condition.

  • After initializing a sequence B=(B1,B2,,BN)B = (B_{1}, B_{2}, \dots, B_{N}) to PP, it is possible to make B=AB = A by repeating the following operation some number of times:
    • Choose ll and rr such that 1lrB1 \leq l \leq r \leq |B|, and if mex({Bl,Bl+1,,Br})\mathrm{mex}(\{B_{l}, B_{l+1}, \dots, B_{r}\}) is contained in BB, remove it from BB.

What is mex(X)\mathrm{mex}(X)? For a finite set XX of non-negative integers, mex(X)\mathrm{mex}(X) is defined as the smallest non-negative integer that is not in XX.

Constraints

  • 1MN5001 \leq M \leq N \leq 500
  • 0Ai<N0 \leq A_{i} < N
  • All elements of AA are distinct.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

A1A_{1} A2A_{2} \cdots AMA_{M}

Output

Print the answer.

Sample Input 1

4 2
1 3

Sample Output 1

8

After initializing B=(2,1,0,3)B = (2, 1, 0, 3), it is possible to make B=AB = A using the following steps:

  • Choose (l,r)=(2,4)(l, r) = (2, 4), remove mex({1,0,3})=2\mathrm{mex}(\{1, 0, 3\}) = 2 from BB, making B=(1,0,3)B = (1, 0, 3).
  • Choose (l,r)=(3,3)(l, r) = (3, 3), remove mex({3})=0\mathrm{mex}(\{3\}) = 0 from BB, making B=(1,3)B = (1, 3).

Thus, P=(2,1,0,3)P = (2, 1, 0, 3) satisfies the condition.

There are eight permutations PP that satisfy the condition, including the above, so print 88.

Sample Input 2

4 4
0 3 2 1

Sample Output 2

1

Only P=(0,3,2,1)P = (0, 3, 2, 1) satisfies the condition.

Sample Input 3

16 7
9 2 4 0 1 6 7

Sample Output 3

3520

Sample Input 4

92 4
1 67 16 7

Sample Output 4

726870122

Find the count modulo 998244353998244353.