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

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

Examples

Input

4
1 5
5 2
3 7
7 3


Output

6


Input

5
1 10
2 10
3 10
4 10
5 5


Output

5

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 
/**
 * Solution to http://codeforces.com/contest/859/problem/E
 * based on description in http://codeforces.com/blog/entry/54572
 */
import java.util.*;
import java.io.*;

public class DeskDisorder
{
    @SuppressWarnings("unchecked")
    public static void main(String []av) {
        Scanner s = new Scanner();
        int N = s.nextInt();
        List<Integer> [] adj = new List[2*N];
        for (int i = 0; i < 2*N; i++)
            adj[i] = new ArrayList<>();

        final long MOD = 1000000007L;
        for (int i = 0; i < N; i++) {
            int a = s.nextInt() - 1;
            int b = s.nextInt() - 1;
            adj[a].add(b);
            adj[b].add(a);
        }

        boolean [] visited = new boolean[2 * N];
        long answer = 1;
        for (int i = 0; i < 2 * N; i++) if (!visited[i] && adj[i].size() > 0) {
            Deque<Integer> dfs = new ArrayDeque<>();
            Deque<Integer> dfsParent = new ArrayDeque<>();
            dfs.push(i);
            dfsParent.push(-1);
            visited[i] = true;
            int ccNodeCount = 1;
            int ccEdgeCount = 0;
            boolean hasSelfEdge = false;
            while (!dfs.isEmpty()) {
                int u = dfs.pop();
                int uparent = dfsParent.pop();

                for (int v : adj[u]) {
                    if (u == v) {
                        hasSelfEdge = true;
                        continue;
                    }

                    if (v != uparent)
                        ccEdgeCount++;

                    if (!visited[v]) {
                        visited[v] = true;
                        ccNodeCount++;
                        dfs.push(v);
                        dfsParent.push(u);
                    }
                }
            }
            if (!hasSelfEdge) {
                if (ccNodeCount - 1 == ccEdgeCount) {
                    answer = (answer * ccNodeCount) % MOD;
                } else {
                    answer = (answer * 2) % MOD;
                }
            }
            // System.err.println(ccNodeCount + " " + ccEdgeCount + " " + hasSelfEdge);
        }
        System.out.println(answer);
    }

    //-----------Scanner class for faster input----------
    /* Provides similar API as java.util.Scanner but does not
     * use regular expression engine.  
     */
    public static class Scanner {
        BufferedReader br;
        StringTokenizer st;

        public Scanner(Reader in) {
            br = new BufferedReader(in);
        }

        public Scanner() { this(new InputStreamReader(System.in)); }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() { return Integer.parseInt(next()); }
        long nextLong() { return Long.parseLong(next()); }
        double nextDouble() { return Double.parseDouble(next()); }

        // Slightly different from java.util.Scanner.nextLine(),
        // which returns any remaining characters in current line,
        // if any.  
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    //--------------------------------------------------------    
}
