#agc052c. [agc052_c]Nondivisible Prefix Sums
[agc052_c]Nondivisible Prefix Sums
Problem Statement
You are given a prime number , which you don't like.
Let's call an array of integers good, if it is possible to reorder the elements in such a way that no prefix sum is divisible by (that is, there is no with and after the reordering).
Consider all arrays of length with elements from to . How many of them are good?
As this number can be very big, output it modulo .
Constraints
- is a prime.
Input
Input is given from Standard Input in the following format:
Output
Output the number of good arrays modulo .
Sample Input 1
2 5
Sample Output 1
12
There are good arrays: \[1, 1\], \[1, 2\], \[1, 3\], \[2, 1\], \[2, 2\], \[2, 4\], \[3, 1\], \[3, 3\], \[3, 4\], \[4, 2\], \[4, 3\], \[4, 4\].
Sample Input 2
4 3
Sample Output 2
8
There are good arrays: \[1, 1, 1, 2\], \[1, 1, 2, 1\], \[1, 2, 1, 1\], \[2, 1, 1, 1\], ], \[2, 2, 1, 2\], \[2, 1, 2, 2\], \[1, 2, 2, 2\].
Sample Input 3
5000 99999989
Sample Output 3
51699346
Sample Input 4
2021 307
Sample Output 4
644635349