MemSQL Start[c]UP 3.0 - Round 1
E
Desk Disorder
Codeforces
2100
原题
← DF →
翻译中…

A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.

翻译中…

How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo $1000000007 = 10^{9} + 7$.

翻译中…

Input

翻译中…

Input will begin with a line containing $*N*$ ($1 ≤ *N* ≤ 100000$), the number of engineers.

翻译中…

$*N*$ lines follow, each containing exactly two integers. The $*i*$-th line contains the number of the current desk of the $*i*$-th engineer and the number of the desk the $*i*$-th engineer wants to move to. Desks are numbered from $1$ to $2·*N*$. It is guaranteed that no two engineers sit at the same desk.

翻译中…

Output

翻译中…

Print the number of possible assignments, modulo $1000000007 = 10^{9} + 7$.

翻译中…

Note

翻译中…

These are the possible assignments for the first example:

翻译中…
  • 1 5 3 7
  • 1 2 3 7
  • 5 2 3 7
  • 1 5 7 3
  • 1 2 7 3
  • 5 2 7 3
翻译中…

样例

Input #1
4
1 5
5 2
3 7
7 3
Output #1
6
InputCopy OutputCopy
Input #2
5
1 10
2 10
3 10
4 10
5 5
Output #2
5
InputCopy OutputCopy
题目信息
题号
859E
时间限制
2 s
内存限制
256 MB
标签
组合数学
DFS 及类似
并查集
图论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。