Problem: Hey!

There is a new building with N + 1 rooms lined up in a row. Each room is a residence for one person, and all rooms are currently vacant, but N new people are scheduled to live here from next month. Therefore, when they start living, one room becomes vacant.

As a landlord, you want to propose many room allocations that suit their tastes. Here, the room allocation is a table that gives which room each person lives in. For example, when N = 3, "the first person is in room 4, the second person is in room 1, and the third person is in room 2".

Of course, it would be quick to propose all possible room allocations, but that would be meaningless, so some restrictions would be set in consideration of the time and effort of the landlord and the tastes of the residents.

First, after they start living in one of the proposed room allocations, they may be told, "I want to change to another proposed room allocation." The building is new and they know they prefer a new room with no one in it yet, so a different room allocation was suggested by simply moving one person to a vacant room. This can only happen if you can change it to. However, as a landlord, I want to avoid such troubles, so I would like to adjust the proposed room allocation so that such changes are not allowed. In other words, the proposed set of room allocations must satisfy the following. "For any two different proposed room allocations A and B, moving one person to a vacant room in room allocation A does not result in room allocation B."

Next, we know that each of the N people who will live in the future has exactly one preferred person. Therefore, all the proposed room allocations should be such that people who are preferable to all people live next to each other.

What is the maximum size of a set of room allocations that satisfy these conditions? Find the remainder divided by 1,000,000,007.

Input

The input consists of multiple datasets. Each dataset is represented in the following format.

> N
> a1 a2 ... aN
>

The first line of the dataset is given the integer N, which represents the number of people who will live in the building next month. This satisfies 2 ≤ N ≤ 100,000. The second line gives information on the number ai of the person who prefers to live in the next room for the i-th person. For this, 1 ≤ ai ≤ N and ai ≠ i are satisfied. Also, the number of datasets does not exceed 50. The end of the input is represented by a line consisting of only one zero.

> ### Output

For each data set, output the remainder of dividing the maximum number of room allocations that can be proposed at the same time by 1,000,000,007 in one line, as shown in the problem statement.

Sample Input


2
twenty one
3
3 1 2
Ten
2 1 1 1 1 1 1 1 1 1
8
2 1 4 3 3 7 6 6
twenty five
2 3 2 5 4 5 8 7 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24
Ten
2 1 4 3 6 5 8 7 10 9
0



Output for Sample Input


2
0
0
144
633544712
11520





Example

Input

2
2 1
3
3 1 2
10
2 1 1 1 1 1 1 1 1 1
8
2 1 4 3 3 7 6 6
25
2 3 2 5 4 5 8 7 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24
10
2 1 4 3 6 5 8 7 10 9
0


Output

2
0
0
144
633544712
11520

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;(i)<(n);++(i))

using namespace std;
using ll = long long;
const ll mod = 1000000007LL;

ll power(ll a,ll n){
	if(n < 0) return 0;
	ll b=1LL;
	while(n){
		if(n&1) b= b*a % mod;
		a = a*a % mod;
		n >>= 1;
	}
	return b;
}

template <class T> bool chmin(T &a,const T &b){return (b<a)?(a=b,1):0;}

const int limit = 100010;
ll fact[limit];
ll ifact[limit];

inline ll Fact(ll n){
	if(n < 0) return 0;
	return fact[n];
}

inline ll Comb(ll n,ll k){
	if(n <0 or k<0 or n-k <0) return 0LL;
	return fact[n] * ifact[k] % mod * ifact[n-k] % mod;
}

int main() {
	fact[0]=1LL;
	rep(i,limit-1) fact[i+1] = 1LL * (i+1) * fact[i] % mod;
	ifact[limit-1] = power(fact[limit-1],mod-2);
	for(int i = limit -2; i >=0 ; --i) ifact[i] = 1LL * (i+1) * ifact[i+1] % mod;

	int n;
	while(cin >> n,n){
		vector<int> a(n),ra(n,-1),used(n,0),num;

		rep(i,n) cin >> a[i],a[i]--;

		rep(i,n){
			if(i==a[a[i]]) continue;
			ra[a[i]]=i;
		}

		rep(i,n){
			const int j = a[i];
			if(i > j or i != a[j]) continue;

			used[i]=used[j]=true;
			int cnt = 2;

			{
				int cur = ra[i];
				while(cur!=-1){
					used[cur] = true;
					cnt++;
					cur = ra[cur];
				}
			}

			{
				int cur = ra[j];
				while(cur!=-1){
					used[cur] = true;
					cnt++;
					cur = ra[cur];
				}
			}

			num.push_back(cnt);
		}

		ll ans = 0LL,two = 0LL;
		
		bool ok = true;
		rep(i,n) if(used[i]==false) ok =false;
		for(auto &elem:num) if(elem==2) two++;
		
		const ll k = num.size();
		const ll three = k - two;

		if(k == 0 or ok == false){
			cout << 0LL << endl;
			continue;
		}

		//cerr << three << " " << two << endl;

		if(three == 0){
			ll cur = ((k+2)/2);
			cur = cur * Fact(k) % mod;
			ans = (ans + cur) % mod;
		}

		if(three >= 1){
			rep(i,k+1){
				ll cur = 2LL * ((i+2)/2LL);
				cur = cur * Comb(two,i) % mod;
				cur = cur * Fact(i) % mod;
				cur = cur * Comb(three,1) % mod;
				cur = cur * Fact(two-i+three-1) % mod;
				ans = (ans + cur) % mod;
			}
		}

		
		if(three >= 2){
			rep(i,k+1){
				ll cur = 2LL * ((i+2)/2LL);
				cur = cur * Comb(two,i) % mod;
				cur = cur * Fact(i) % mod;
				cur = cur * Comb(three,2) % mod;
				cur = cur * Fact(two-i+three-2) % mod;
				cur = cur * (two -i + three -2 + 1) % mod;
				ans = (ans + cur) % mod;
			}
		}

		ans = 1LL * ans * power(2,k) % mod;
		cout << ans << endl; 

	}			
	
	return 0;
}