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.util.*;
import java.io.*;
import java.math.*;
 
public class Main implements Runnable
{
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Main(),"Main",1<<26).start(); //To increase recursion-stack size in java
	}
 	long total=0;
 	int flag=0;
 	int flag1=0;

	public void run()
	{
		InputReader sc = new InputReader(System.in);      //For fast I/O
		PrintWriter w = new PrintWriter(System.out);

		int n=sc.nextInt();
		ArrayList<Integer> list[] = new ArrayList[2*n+1];

		for(int i=0;i<2*n+1;i++)
			list[i]= new ArrayList<>();

		for(int i=0;i<n;i++)
		{
			int u=sc.nextInt();
			int v=sc.nextInt();

			list[u].add(v);
			list[v].add(u);
		}

		n*=2;

		int visit[]=new int[n+1];

		long ans=1;

		long mod=(long)1e9+7;

		for(int i=1;i<=n;i++)
		{
			if(visit[i]==0)
			{
				total=0;
				flag=0;
				flag1=0;
				dfs(i,i,list,visit);

				if(flag1!=0)
					continue;

				if(flag!=0)
					ans*=2;
				else
					ans*=total;
				ans%=mod;
			}
		}
		w.println(ans);
		w.close();
	}

	void dfs(int i,int parent,ArrayList<Integer> list[],int visit[])
	{
		if(visit[i]==0)
		{
			total++;
			visit[i]++;

			for(int in:list[i])
			{
				if(in==i)
					flag1++;
				if(in!=parent)
					dfs(in,i,list,visit);
			}
		}
		else
		{
			flag++;
		}
	}

	static class Pair implements Comparable<Pair>
	{
		int value;
		int index;
		int actualDifference;

		public Pair(int actualDifference,int index)
		{
			this.value=Math.abs(actualDifference);
			this.actualDifference=actualDifference;
			this.index=index;
		}

		public int compareTo(Pair pair)
		{
			if(Integer.compare(value,pair.value)==0)
			{
				if(Integer.compare(actualDifference,pair.actualDifference)==0)
					return Integer.compare(index,pair.index);

				return Integer.compare(actualDifference,pair.actualDifference);
			}

			return Integer.compare(value,pair.value);
		}
	}
 
	static class InputReader 
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		
		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}
		
		public int read()
		{
			if (numChars==-1) 
				throw new InputMismatchException();
			
			if (curChar >= numChars)
			{
				curChar = 0;
				try 
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}
				
				if(numChars <= 0)				
					return -1;
			}
			return buf[curChar++];
		}
	 
		public String nextLine()
		{
			String str = "";
	        try
	        {
				str=new BufferedReader(new InputStreamReader(stream)).readLine();
	        }
	        catch (IOException e)
	        {
	            e.printStackTrace();
	        }
	        return str;
		}
		public int nextInt()
		{
			int c = read();
			
			while(isSpaceChar(c)) 
				c = read();
			
			int sgn = 1;
			
			if (c == '-') 
			{
				sgn = -1;
				c = read();
			}
			
			int res = 0;
			do 
			{
				if(c<'0'||c>'9') 
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c)); 
			
			return res * sgn;
		}
		
		public long nextLong() 
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') 
			{
				sgn = -1;
				c = read();
			}
			long res = 0;
			
			do 
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}
		
		public double nextDouble() 
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') 
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.') 
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.') 
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c)) 
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}
		
		public String readString() 
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do 
			{
				res.appendCodePoint(c);
				c = read();
			} 
			while (!isSpaceChar(c));
			
			return res.toString();
		}
	 
		public boolean isSpaceChar(int c) 
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	 
		public String next() 
		{
			return readString();
		}
		
		public interface SpaceCharFilter 
		{
			public boolean isSpaceChar(int ch);
		}
	}
 
} 