Problem Statement
There are an integer N and M pairs of integers: (a1,b1),(a2,b2),dots,(aM,bM). Each pair (ai,bi) satisfies 1leqailtbileqN.
Initally, you have all N! permutations of (1,2,dots,N).
You will perform M operations. The i-th operation is as follows.
- Do the following for each of your N! permutations.
- Compare the ai-th and bi-th elements. If the former is greater, swap the two elements.
For each 1leqileqM, let Si be the number of permutations of yours that are already sorted in ascending order at the end of the i-th operation.
Print S1,S2,dots,SM.
Here, the input gives you pairs of integers (xi,yi) instead of (ai,bi).
The values of (ai,bi) can be obtained using xi, yi, and Si−1 as follows. (Let S0=1 for convenience.)
- Let ci=((xi+Si−1)bmodN)+1.
- Let di=((yi+Si−1times2)bmodN)+1. (Here, it is guaranteed that cineqdi.)
- Let ai=min(ci,di).
- Let bi=max(ci,di).
Constraints
- 2leqNleq15
- 1leqMleq5times105
- 1leqailtbileqN
- 0leqxi,yileqN−1
The input is given from Standard Input in the following format:
N M
x1 y1
x2 y2
vdots
xM yM
Output
Print M lines. The i-th line should contain Si.
2 1
1 1
Sample Output 1
2
You start with the permutations (1,2) and (2,1).
We have (a1,b1)=(1,2). At the end of the first operation, you have two copies of (1,2), so you should print 2.
3 4
0 1
2 1
1 1
0 1
Sample Output 2
2
4
4
6
(ai,bi) in order are (1,2),(2,3),(1,3),(1,2).
5 5
4 4
0 4
1 1
2 4
1 2
Sample Output 3
2
4
4
8
16
(ai,bi) in order are (1,2),(3,4),(1,5),(2,3),(4,5).