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.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException{
        Reader reader = new Reader();
        int N = reader.nextInt();
        Solution sol = new Solution(N);
        for(int i = 1; i <= N; ++i){
            sol.union(reader.nextInt(), reader.nextInt());
        }
        System.out.println(sol.solve());
    }
}

class Solution {
    int N;
    Node[] dsu;
    static final int MOD = 1000000007;
    
    Solution(int N){
        this.N = N;
        dsu = new Node[(N<<1) + 1];
        for(int i = 1; i <= (N<<1); ++i){
            dsu[i] = new Node(i);
        }
    }
    
    long solve(){
        long res = 1;
        for(int i = 1; i <= (N<<1); ++i){
            // System.out.println(i + ", " + dsu[i].size + ", " + dsu[i].parent +", " + dsu[i].cycleType);
            res = (res*(dsu[i].cycleType == 0 ? dsu[i].size : dsu[i].cycleType))%MOD;
        }
        return res;
    }
    
    int find(int a){
        if(dsu[a].parent == a)
            return a;
        return dsu[a].parent = find(dsu[a].parent);
    }
    
    void union(int a, int b){
        int par_a = find(a);
        int par_b = find(b);
        if(par_a == par_b){
            if(a == b)
                dsu[par_a].cycleType = 1;
            else
                dsu[par_a].cycleType = 2;
            return;
        }
        if(dsu[par_a].size < dsu[par_b].size){
            int temp = par_a;
            par_a = par_b;
            par_b = temp;
        }
        dsu[par_b].parent = par_a;
        dsu[par_a].size += dsu[par_b].size;
        dsu[par_b].size = 1;
        dsu[par_a].cycleType += dsu[par_b].cycleType;
        dsu[par_b].cycleType = 0;
    }
}

class Node {
    int cycleType = 0;
    int size = 1;
    int parent = -1;
    
    Node(int i){
        this.parent = i;
    }
}

class Reader {
    final private int BUFFER_SIZE = 1 << 16; 
    private DataInputStream din; 
    private byte[] buffer; 
    private int bufferPointer, bytesRead; 
 
    public Reader() 
    { 
        din = new DataInputStream(System.in); 
        buffer = new byte[BUFFER_SIZE]; 
        bufferPointer = bytesRead = 0; 
    } 
 
    public Reader(String file_name) throws IOException 
    { 
        din = new DataInputStream(new FileInputStream(file_name)); 
        buffer = new byte[BUFFER_SIZE]; 
        bufferPointer = bytesRead = 0; 
    } 
 
    public String readLine() throws IOException 
    { 
        byte[] buf = new byte[64]; // line length 
        int cnt = 0, c; 
        while ((c = read()) != -1) 
        { 
            if (c == '\n') 
                break; 
            buf[cnt++] = (byte) c; 
        } 
        return new String(buf, 0, cnt); 
    } 
 
    public int nextInt() throws IOException 
    { 
        int ret = 0; 
        byte c = read(); 
        while (c <= ' ') 
            c = read(); 
        boolean neg = (c == '-'); 
        if (neg) 
            c = read(); 
        do
        { 
            ret = ret * 10 + c - '0'; 
        }  while ((c = read()) >= '0' && c <= '9'); 
 
        if (neg) 
            return -ret; 
        return ret; 
    } 
 
    public long nextLong() throws IOException 
    { 
        long ret = 0; 
        byte c = read(); 
        while (c <= ' ') 
            c = read(); 
        boolean neg = (c == '-'); 
        if (neg) 
            c = read(); 
        do { 
            ret = ret * 10 + c - '0'; 
        } 
        while ((c = read()) >= '0' && c <= '9'); 
        if (neg) 
            return -ret; 
        return ret; 
    } 
 
    public double nextDouble() throws IOException 
    { 
        double ret = 0, div = 1; 
        byte c = read(); 
        while (c <= ' ') 
            c = read(); 
        boolean neg = (c == '-'); 
        if (neg) 
            c = read(); 
 
        do { 
            ret = ret * 10 + c - '0'; 
        } 
        while ((c = read()) >= '0' && c <= '9'); 
 
        if (c == '.') 
        { 
            while ((c = read()) >= '0' && c <= '9') 
            { 
                ret += (c - '0') / (div *= 10); 
            } 
        } 
 
        if (neg) 
            return -ret; 
        return ret; 
    } 
 
    private void fillBuffer() throws IOException 
    { 
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
        if (bytesRead == -1) 
            buffer[0] = -1; 
    } 
 
    private byte read() throws IOException 
    { 
        if (bufferPointer == bytesRead) 
            fillBuffer(); 
        return buffer[bufferPointer++]; 
    } 
 
    public void close() throws IOException 
    { 
        if (din == null) 
            return; 
        din.close(); 
    } 
}
