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 
/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class DeskDisorderRedo implements Runnable {
    public static void main(String[] args) throws Exception {
        new Thread(null, new DeskDisorderRedo(), ": )", 1 << 28).start();
    }

    public void run() {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        adjList = new ArrayList[2 * N];
        Arrays.setAll(adjList, i -> new ArrayList<>());
        boolean[] exists = new boolean[2 * N];
        for (int i = 0; i < N; i++) {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            adjList[u].add(v);
            adjList[v].add(u);
            exists[u] = exists[v] = true;
        }

        long ans = 1;
        vis = new boolean[2 * N];
        for (int root = 0; root < 2 * N; root++) {
            if (exists[root] && !vis[root]) {
                fakeCycle = false;
                edgeCt = 0; nodeCt = 0;
                /*
                 actually, our prior method of detecting cycles does NOT
                 work when we transformed the graph to be an undirected graph
                 We need undirectedness in order to find all the components,
                 but now we still need to know whether there might be a cycle
                 pair like 1<->2. That won't be detected, but we can be clever
                 by just double all the edges to be in both direction (which
                 is how we transformed it in the first place). Now, we can
                 undo that by dividing edges by 2, and this'll tell us if it
                 contains a cycle
                 */
                long ways = solve(root, -1);
                ASSERT(edgeCt % 2 == 0); // we count each edge twice
                edgeCt /= 2;
//                System.out.println(edgeCt +" " +nodeCt);
                if (edgeCt == nodeCt - 1) {
                    ans = (ans * ways) % mod;
                } else {
                    ASSERT(edgeCt == nodeCt);
                    if (!fakeCycle) {
                        ans = (2L * ans) % mod;
                    }
                }
                    /*
                     Need to IGNORE single loops!!!
                     Any component with a cycle can only have that cycle have movement
                     Since everything must lead into the cycle (the cycle has no
                     outgoing edges, since each node is using up its single edge
                     to go toward another member of the cycle).
                     Therefore once the cycle has shifted around, everything else
                     cannot move. So, each cycle can either move or not (2 ways).
                     UNLESS the cycle is a self-loop, in which case moving is indistinguishable
                     so it's only 1 way!

                     Also, only one cycle can exist in a component
                     since each node again can only have outdegree 1, so it has to
                     lead to the one cycle. It can't branch into another.
                     */
            }
        }
        out.println(ans);
        out.close();
    }

    static ArrayList<Integer>[] adjList;
    static long mod = (long) 1e9 + 7;
    static boolean[] vis;
    static boolean fakeCycle;
    static int edgeCt, nodeCt;

    static int solve(int node, int par) {
//        System.out.println("Visiting " + node);
        if (!vis[node]) {
            vis[node] = true;
            nodeCt++;
            edgeCt += adjList[node].size();
            int ways = 1; // do nothing
            for (int adj : adjList[node]) {
                if (adj != par) {
                    ways += solve(adj, node); // only one of them can lead into me, so add ways
                }
                if (adj == node) {
                    fakeCycle = true;
                }
            }
            return ways;
        } else {
            return 0;
        }
    }

    static class FastScanner {
        private int BS = 1 << 16;
        private char NC = (char) 0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            } catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar() {
            while (bId == size) {
                try {
                    size = in.read(buf);
                } catch (Exception e) {
                    return NC;
                }
                if (size == -1) return NC;
                bId = 0;
            }
            return (char) buf[bId++];
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt = 1;
            boolean neg = false;
            if (c == NC) c = getChar();
            for (; (c < '0' || c > '9'); c = getChar()) {
                if (c == '-') neg = true;
            }
            long res = 0;
            for (; c >= '0' && c <= '9'; c = getChar()) {
                res = (res << 3) + (res << 1) + c - '0';
                cnt *= 10;
            }
            return neg ? -res : res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c != '.' ? cur : cur + nextLong() / cnt;
        }

        public double[] nextDoubles(int N) {
            double[] res = new double[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextDouble();
            }
            return res;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while (c <= 32) c = getChar();
            while (c > 32) {
                res.append(c);
                c = getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while (c <= 32) c = getChar();
            while (c != '\n') {
                res.append(c);
                c = getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if (c > 32) return true;
            while (true) {
                c = getChar();
                if (c == NC) return false;
                else if (c > 32) return true;
            }
        }
    }

    static void ASSERT(boolean assertion, String message) {
        if (!assertion) throw new AssertionError(message);
    }

    static void ASSERT(boolean assertion) {
        if (!assertion) throw new AssertionError();
    }
}