Desk Disorder
Contest: MemSQL Start[c]UP 3.0 - Round 1
Difficulty rating: 2100
Tags: combinatorics, dfs and similar, dsu, graphs, trees

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 = 109 + 7.

Input format:
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 format:
Print the number of possible assignments, modulo 1000000007 = 109 + 7.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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

Example input:
4
1 5
5 2
3 7
7 3

Example output:
6

Example input:
5
1 10
2 10
3 10
4 10
5 5

Example output:
5

Editorial:
Consider an undirected graph where the desks are vertices and the engineers are edges. For each engineer an edge connects their current desk to their desired desk. Each connected component of this graph can be assigned independently, with the final result being the product of the number of assignments over the individual components.
What type of graphs are the connected components? We know because it's connected that E ≥ V - 1, where E and V are number of edges and vertices, respectively. Also note that E ≤ V due to the condition that no two engineers currently sit at the same desk. It follows that there are only 2 cases to consider.
Case 1: E = V - 1. In this case the component is a tree. We claim that the number of assignments is equal to V in this case. To see why this is true, consider that after we choose which of the V desks to leave empty, there's always exactly 1 way to assign engineers to the remaining desks.
Case 2: E = V. In this case the component has exactly one cycle. Engineers currently sitting at a desk on the cycle can either all stay at their current desk, or all move to their desired desk. Engineers currently sitting at a desk not on the cycle must remain at their current desks. Therefore there will be exactly 2 assignments, unless the cycle was a self-loop (an engineer whose desired desk is equal to their current desk), in which case there is only 1 assignment.
To compute the result, we can use a Disjoint-set data structure. For each connected component, we keep track of its size and what type of cycle (if any) is present. Initially each desk is in a component by itself. For each engineer, we merge the components containing their current and desired desks, or mark the component as containing a cycle if the current and desired desks were already in the same component. Finally we multiply together the numbers of assignments of the components.