atcoder#ARC112E. [ARC112E] Cigar Box

[ARC112E] Cigar Box

Score : 700700 points

Problem Statement

We applied the following operation mm times on the sequence (1,2,,n)(1,2,\dots,n), and it resulted in (a1,,an)(a_1,\dots ,a_n).

  • Erase one element. Then, add the erased element to the beginning or end of the sequence.

Find the number of possible sequences of the mm operations, modulo 998244353998244353.

Here, two sequences of operations are considered the same when, in every operation, the same element is chosen and added to the same position.

Constraints

  • 2n30002\le n \le 3000
  • 1m30001\le m \le 3000
  • a1,,ana_1,\dots ,a_n is a permutation of 1,,n1,\dots,n.

Input

Input is given from Standard Input in the following format:

nn mm

a1a_1 \dots ana_n

Output

Print the number of possible sequences of the mm operations, modulo 998244353998244353.

5 2
1 2 4 5 3
5

There are five possible sequences of operations, as follows:

  • Erase 11 and add it to the beginning, which results in (1,2,3,4,5)(1,2,3,4,5). Then, erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3).
  • Erase 33 and add it to the beginning, which results in (3,1,2,4,5)(3,1,2,4,5). Then, erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3).
  • Erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3). Then, erase 11 and add it to the beginning, which results in (1,2,4,5,3)(1,2,4,5,3).
  • Erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3). Then, erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3).
  • Erase 55 and add it to the end, which results in (1,2,3,4,5)(1,2,3,4,5). Then, erase 33 and add it to the end, which results in (1,2,4,5,3)(1,2,4,5,3).
2 16
1 2
150994942

Two of the four kinds of operations have no effect on the sequence, and the other two swap the two elements. From this fact, we can show that there are 4m/2=231=21474836484^m/2 = 2^{31} = 2147483648 sequences of operations - half of all possible sequences - that we want to count. Thus, the answer is 21474836482147483648 modulo 998244353998244353, that is, 150994942150994942.

10 3000
3 7 10 1 9 5 4 8 6 2
129989699