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 
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Queue;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Yuan Lei
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        static final int MO = (int) (1e9 + 7);

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] seat = new int[2 * n + 1];
            int[] want = new int[n];
            Arrays.fill(seat, -1);
            List<Integer>[] edges = new ArrayList[2 * n + 1];
            for (int i = 1; i <= 2 * n; ++i) edges[i] = new ArrayList<>();
            for (int i = 0; i < n; ++i) {
                int x = in.nextInt();
                seat[x] = i;
                want[i] = in.nextInt();
                edges[want[i]].add(x);
            }
            boolean[] used = new boolean[2 * n + 1];
            Arrays.fill(used, false);
            long ans = 1;
            for (int i = 1; i <= 2 * n; ++i) {
                if (used[i]) continue;
                if (seat[i] == -1 && edges[i].size() > 0) {
                    Queue<Integer> queue = new LinkedList<>();
                    int count = 0;
                    queue.add(i);
                    while (!queue.isEmpty()) {
                        int x = queue.poll();
                        used[x] = true;
                        ++count;
                        for (int y : edges[x]) {
                            queue.add(y);
                        }
                    }
                    ans = ans * count % MO;
                }
            }
            for (int i = 1; i <= 2 * n; ++i) {
                if (used[i]) continue;
                if (seat[i] >= 0 && edges[i].size() > 0) {
                    int now = i;
                    Queue<Integer> queue = new LinkedList<>();
                    while (!used[now]) {
                        used[now] = true;
                        queue.add(now);
                        int person = seat[now];
                        now = want[person];
                    }
                    while (!queue.isEmpty()) {
                        int x = queue.poll();
                        used[x] = true;
                        for (int y : edges[x]) {
                            if (!used[y]) queue.add(y);
                        }
                    }
                    if (want[seat[now]] == now) continue;
                    ans <<= 1;
                    ans %= MO;
                }
            }
            out.println(ans);
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public boolean hasNext() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    final String line = reader.readLine();
                    if (line == null) return false;
                    tokenizer = new StringTokenizer(line);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        }

        public String next() {
            if (hasNext()) return tokenizer.nextToken();
            return null;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

