Local Maxima
Time limit4sMemory limit512 MB
Count permutations of 1 to n*m in an n by m grid that have exactly one local maximum, where a cell is a local maximum if it is at least every other cell in its row and column, modulo a prime P.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Greedy
- Solved
- No attempts yet
Problem
Given an integer matrix , a local maximum of is a location with and such that is no smaller than any other integer on the -th row or on the -th column.
For example, in the matrix there are three local maxima: locations , , and with values , , and , respectively.
An integer matrix is good if and only if it satisfies the following two conditions:
- There is exactly one local maximum in .
- Each integer from to occurs exactly once in .
Given , , and a prime number , count the number of good matrices of size modulo .
Input
The first line contains three integers, , , and , where and . It is guaranteed that is prime.
Output
Output a single line with a single integer: the number of good matrices modulo .