atcoder#AGC052C. [AGC052C] Nondivisible Prefix Sums

[AGC052C] Nondivisible Prefix Sums

Score : 10001000 points

Problem Statement

You are given a prime number PP, which you don't like.

Let's call an array of integers A1,A2,,ANA_1, A_2, \dots, A_N good, if it is possible to reorder the elements in such a way that no prefix sum is divisible by PP (that is, there is no ii with 1iN1 \le i \le N and A1+A2++Ai0modPA_1 + A_2 + \dots + A_i \equiv 0 \bmod P after the reordering).

Consider all (P1)N(P-1)^N arrays of length NN with elements from 11 to P1P-1. How many of them are good?

As this number can be very big, output it modulo 998244353998244353.

Constraints

  • 1N50001 \le N \le 5000
  • 2P1082 \le P \le 10^8
  • PP is a prime.

Input

Input is given from Standard Input in the following format:

NN PP

Output

Output the number of good arrays modulo 998244353998244353.

2 5
12

There are 1212 good arrays: [1,1][1, 1], [1,2][1, 2], [1,3][1, 3], [2,1][2, 1], [2,2][2, 2], [2,4][2, 4], [3,1][3, 1], [3,3][3, 3], [3,4][3, 4], [4,2][4, 2], [4,3][4, 3], [4,4][4, 4].

4 3
8

There are 88 good arrays: [1,1,1,2][1, 1, 1, 2], [1,1,2,1][1, 1, 2, 1], [1,2,1,1][1, 2, 1, 1], [2,1,1,1][2, 1, 1, 1], [2,2,2,1[2, 2, 2, 1], [2,2,1,2][2, 2, 1, 2], [2,1,2,2][2, 1, 2, 2], [1,2,2,2][1, 2, 2, 2].

5000 99999989
51699346
2021 307
644635349