Permutation Period

항등 순열에서 시작해 매 질의마다 두 원소를 교환한 뒤, 현재 순열의 위수(주기)를 10^9+7로 나눈 나머지를 구한다. 주기는 사이클 길이들의 최소공배수이므로 사이클 구조와 소인수 지수 테이블을 유지하며 갱신한다.

어려움8유니온 파인드수학정수론구현아직 제출이 없습니다시간 제한5초메모리 제한512 MB

문제

You have a permutation pp of NN integers. Initially p_i=ip\_i = i holds for 1iN1 \le i \le N. For each j (1jN)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 k1k \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 (1jN)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.

입력

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 consists of two integers NN and QQ (2N105,1Q1052 \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 (1x_i,y_iN,x_iy_i1 \le x\_i, y\_i \le N, x\_i \ne y\_i).

출력

Print the answer in one line for each query.