#P11460. Maximin Permutation Count with Fixed Positions

    ID: 13542 Type: Default 1000ms 256MiB

Maximin Permutation Count with Fixed Positions

Maximin Permutation Count with Fixed Positions

You are given an integer N (2 ≤ N ≤ 2000) and K restrictions of the form p[i] = j (0 ≤ i, j < N). Consider all permutations p = [p0, p1, …, pN-1] of the set {0, 1, …, N−1}.

For a permutation p, define

f(p)=min0iN2pipi+1. f(p)=\min_{0\le i \le N-2}|p_i-p_{i+1}|.

Let S be the set of permutations for which f(p) is maximized – that is, f(p) attains its maximum possible value. It can be shown that the maximum possible value is N2.\lfloor \frac{N}{2}\rfloor.

It turns out that when N is even the optimal permutation is unique up to reversal (exactly 2 solutions) and when N is odd there are exactly 6 optimal permutations (for N = 3 the 6 permutations are all optimal, and for N ≥ 5 the 6 solutions are characterized by a very rigid structure).

Your task is to count how many permutations in S satisfy all the given restrictions. Since the answer can be very large, output it modulo 109+7.

Note: The time limit stated in the problem is 4 seconds (usually two times the typical 2‐second limit).

inputFormat

The first line contains two integers N and K.

The following K lines each contain two integers i and j, indicating the restriction p[i] = j.

outputFormat

Output one integer, the number of optimal permutations that satisfy all the given restrictions, taken modulo 109+7.

sample

3 0
6