This page is still under construction.

Parts of this page are still being built. What you see may change.

Permutation Period

Time limit5sMemory limit512 MB

Summary
Starting from the identity permutation, apply swaps one at a time and after each swap report the permutation's period modulo 1e9+7, where the period is the LCM of its cycle lengths.
Level

Hard8 of 10

Topics
Math, Number theory, Union-find, Implementation
Solved
No attempts yet

Problem

You have a permutation pp of NN integers. Initially pi=ip_i = i holds for 1≤i≤N1 \le i \le N. For each j (1≤j≤N)j \ (1 \le j \le N), let pj0=jp^0_j = j and pjk=ppjk−1p^k_j = p^{k-1}_{p_j} for any k≥1k \ge 1. The period of pp is the minimum positive integer kk such that pjk=jp^k_j = j for every j (1≤j≤N)j \ (1 \le j \le N).

You are given QQ queries. The ii-th query is given by two distinct indices xix_i and yiy_i. For each query, swap pxip_{x_i} and pyip_{y_i}, then compute the period of the updated pp modulo 109+710^9 + 7, in the given order.

The period of pp always exists.

Input

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

$N \ Q$
$x_1 \ y_1$
$\vdots$
$x_Q \ y_Q$

The first line contains two integers NN and QQ (2≤N≤105,1≤Q≤1052 \le N \le 10^5, 1 \le Q \le 10^5). The (i+1)(i+1)-th line contains two integers xix_i and yiy_i (1≤xi,yi≤N,xi≠yi1 \le x_i, y_i \le N, x_i \ne y_i).

Output

For each query, print the answer on its own line.

Examples3

  1. Example 1

    Input
    5 4
    2 5
    2 4
    1 3
    1 2
    
    Expected output
    2
    3
    6
    5
    
  2. Example 2

    Input
    2 2
    1 2
    1 2
    
    Expected output
    2
    1
    
  3. Example 3

    Input
    10 10
    5 6
    5 9
    8 2
    1 6
    8 1
    7 1
    2 6
    8 1
    7 4
    8 10
    
    Expected output
    2
    3
    6
    4
    6
    7
    12
    7
    8
    9