#jag2017summerday3k. [jag2017summer_day3_k]Permutation Period

[jag2017summer_day3_k]Permutation Period

MathJax.Hub.Config({ tex2jax: { inlineMath: [["","",""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 16px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }

Problem Statement

You have a permutation pp of NN integers. Initially p_i=ip\_i = i holds for 1leileN1 \\le i \\le N. For each j(1lejleN)j \\ (1 \\le j \\le N), let's denote p0_j=jp^0\_j = j and pk_j=pk1_p_jp^k\_j = p^{k-1}\_{p\_j} for any kge1k \\ge 1. The period of pp is defined as the minimum positive integer kk which satisfies pk_j=jp^k\_j = j for every j(1lejleN)j \\ (1 \\le j \\le N).

You are given QQ queries. The ii-th query is characterized by two distinct indices x_ix\_i and y_iy\_i. For each query, swap p_x_ip\_{x\_i} and p_y_ip\_{y\_i} and then calculate the period of updated pp modulo 109+710^9 + 7 in the given order.

It can be proved that the period of pp always exists.


Input

The input consists of a single test case of the following format.

NQN \\ Q x_1y_1x\_1 \\ y\_1 vdots\\vdots x_Qy_Qx\_Q \\ y\_Q

The first line consists of two integers NN and QQ (2leNle105,1leQle1052 \\le N \\le 10^5, 1 \\le Q \\le 10^5). The (i+1)(i+1)-th line consists of two integers x_ix\_i and y_iy\_i (1lex_i,y_ileN,x_iney_i1 \\le x\_i, y\_i \\le N, x\_i \\ne y\_i).

Output

Print the answer in one line for each query.


Sample Input 1

5 4
2 5
2 4
1 3
1 2

Output for Sample Input 1

2
3
6
5

pp changes as follows: $\[1,2,3,4,5\] \\to \[1,5,3,4,2\] \\to \[1,4,3,5,2\] \\to \[3,4,1,5,2\] \\to \[4,3,1,5,2\]$.


Sample Input 2

2 2
1 2
1 2

Output for Sample Input 2

2
1

pp changes as follows: \[1,2\] \\to \[2,1\] \\to \[1,2\].


Sample Input 3

10 10
5 6
5 9
8 2
1 6
8 1
7 1
2 6
8 1
7 4
8 10

Output for Sample Input 3

2
3
6
4
6
7
12
7
8
9