atcoder#ARC151B. [ARC151B] A < AP

[ARC151B] A < AP

Score : 500500 points

Problem Statement

You are given a permutation P=(P1,P2,,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N).

Print the number of integer sequences A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of length NN that satisfy both of the conditions below, modulo 998244353998244353.

  • 1AiM1 \leq A_i \leq M for every i=1,2,,Ni = 1, 2, \ldots, N.
  • The integer sequence AA is lexicographically smaller than the integer sequence (AP1,AP2,,APN)(A_{P_1}, A_{P_2}, \ldots, A_{P_N}).
What is lexicographical order?

An integer sequence X=(X1,X2,,XN)X = (X_1,X_2,\ldots,X_N) is lexicographically smaller than an integer sequence Y=(Y1,Y2,,YN)Y = (Y_1,Y_2,\ldots,Y_N) when there is an integer 1iN1 \leq i \leq N that satisfies both of the conditions below.

  • For all integers jj (1ji11 \leq j \leq i-1), Xj=YjX_j=Y_j.
  • Xi<YiX_i < Y_i

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1M1091 \leq M \leq 10^9
  • 1PiN1 \leq P_i \leq N
  • ij    PiPji \neq j \implies P_i \neq P_j
  • All values in the input are integers.

Input

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

NN MM

P1P_1 P2P_2 \ldots PNP_N

Output

Print the number of integer sequences AA of length NN that satisfy both of the conditions in the Problem Statement, modulo 998244353998244353.

4 2
4 1 3 2
6

Six integer sequences AA satisfy both of the conditions: (1,1,1,2)(1, 1, 1, 2), (1,1,2,2)(1, 1, 2, 2), (1,2,1,2)(1, 2, 1, 2), (1,2,2,2)(1, 2, 2, 2), (2,1,1,2)(2, 1, 1, 2), and (2,1,2,2)(2, 1, 2, 2). For instance, when A=(1,1,1,2)A = (1, 1, 1, 2), we have $(A_{P_1}, A_{P_2}, A_{P_3}, A_{P_4}) = (2, 1, 1, 1)$, which is lexicographically larger than AA.

1 1
1
0
20 100000
11 15 3 20 17 6 1 9 5 19 10 16 7 8 12 2 18 14 4 13
55365742