Permutation Period
Time limit5sMemory limit512 MB
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 of integers. Initially holds for . For each , let and for any . The period of is the minimum positive integer such that for every .
You are given queries. The -th query is given by two distinct indices and . For each query, swap and , then compute the period of the updated modulo , in the given order.
The period of 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 and (). The -th line contains two integers and ().
Output
For each query, print the answer on its own line.