{"problem":{"name":"Count Sorted Arrays","description":{"content":"There are an integer $N$ and $M$ pairs of integers: $(a_1, b_1), (a_2, b_2), \\dots, (a_M, b_M)$. Each pair $(a_i, b_i)$ satisfies $1 \\leq a_i \\lt b_i \\leq N$. Initally, you have all $N!$ permutations ","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc160_f"},"statements":[{"statement_type":"Markdown","content":"There are an integer $N$ and $M$ pairs of integers: $(a_1, b_1), (a_2, b_2), \\dots, (a_M, b_M)$. Each pair $(a_i, b_i)$ satisfies $1 \\leq a_i \\lt b_i \\leq N$.\nInitally, you have all $N!$ permutations of $(1,2,\\dots,N)$.  \nYou will perform $M$ operations. The $i$\\-th operation is as follows.\n\n*   Do the following for each of your $N!$ permutations.\n    *   Compare the $a_i$\\-th and $b_i$\\-th elements. If the former is greater, swap the two elements.\n\nFor each $1 \\leq i \\leq M$, let $S_i$ be the number of permutations of yours that are already sorted in ascending order at the end of the $i$\\-th operation.  \nPrint $S_1, S_2, \\dots, S_M$.\nHere, the input gives you pairs of integers $(x_i, y_i)$ instead of $(a_i, b_i)$.  \nThe values of $(a_i, b_i)$ can be obtained using $x_i$, $y_i$, and $S_{i-1}$ as follows. (Let $S_0 = 1$ for convenience.)\n\n*   Let $c_i = ((x_i + S_{i-1}) \\bmod N) + 1$.\n*   Let $d_i = ((y_i + S_{i-1} \\times 2) \\bmod N) + 1$. (Here, it is guaranteed that $c_i \\neq d_i$.)\n*   Let $a_i = \\min(c_i, d_i)$.\n*   Let $b_i = \\max(c_i, d_i)$.\n\n## Constraints\n\n*   $2 \\leq N \\leq 15$\n*   $1 \\leq M \\leq 5 \\times 10^5$\n*   $1 \\leq a_i \\lt b_i \\leq N$\n*   $0 \\leq x_i, y_i \\leq N - 1$\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$N$ $M$\n$x_1$ $y_1$\n$x_2$ $y_2$\n$\\vdots$\n$x_M$ $y_M$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc160_f","tags":[],"sample_group":[["2 1\n1 1","2\n\nYou start with the permutations $(1, 2)$ and $(2, 1)$.  \nWe have $(a_1, b_1) = (1, 2)$. At the end of the first operation, you have two copies of $(1, 2)$, so you should print $2$."],["3 4\n0 1\n2 1\n1 1\n0 1","2\n4\n4\n6\n\n$(a_i, b_i)$ in order are $(1, 2), (2, 3), (1, 3), (1, 2)$."],["5 5\n4 4\n0 4\n1 1\n2 4\n1 2","2\n4\n4\n8\n16\n\n$(a_i, b_i)$ in order are $(1, 2), (3, 4), (1, 5), (2, 3), (4, 5)$."]],"created_at":"2026-03-03 11:01:14"}}