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>
using namespace std;

typedef long long ll;

ll n;
ll x[100005];
ll d[100005];
ll y[100005];
ll i;

ll mod = 1000000007;

ll fact[500005],rfact[500005];
vector <ll> G[1000005];
bool used[1000005];
bool used2[1000005];

ll ynum;

ll pow(ll x,ll n){
	if(n == 0){
		return 1;
	}else if(n == 1){
		return x;
	}
	if(n % 2 == 1){
		return (pow(x,n-1) * x)% mod;
	}else{
		ll num = pow(x,n/2);
		return (num*num) % mod;
	}
}


void init(){
	fact[0] = 1;
	for(ll i=1;i<=500000;i++){
		fact[i] = fact[i-1] * i;
		fact[i] %= mod;
	}
	rfact[500000] = pow(fact[500000],mod-2);
	for(ll i=499999;i>=0;i--){
		rfact[i] = rfact[i+1] * (i + 1);
		rfact[i] %= mod;
	}
}

ll dfs(ll v){
	used[v] = true;
	ll sum = 1;
	for(int i=0;i<G[v].size();i++){
		ll nv = G[v][i];
		if(used[nv])continue;
		sum += dfs(nv);
	}
	return sum;
}

ll dfs2(ll v){
	used2[v] = true;
	ll sum = G[v].size();
	for(int i=0;i<G[v].size();i++){
		ll nv = G[v][i];
		if(used2[nv])continue;
		sum += dfs2(nv);
	}
	return sum;
}

ll c3(ll n,ll r){
	//nCr
	ll res1 = fact[n];
	ll res2 = rfact[r];
	ll res3 = rfact[n-r];
	return (((res1 * res2) % mod) * res3) % mod;
}


bool solve(){
	cin >> n;
	ynum = 0;
	if(n == 0){
		return false;
	}
	d[0] = 0;
	for(int i=0;i<n;i++){
		cin >> x[i];
		d[i+1] = 0;
		y[i] = 0;
		G[i].clear();
		used[i] = false;
		used2[i] = false;
	}
	used[n] = false;
	used2[n] = false;


	G[n].clear();
	y[n] = 0;
	for(i=0;i<n;i++){
		if(find(G[i+1].begin(),G[i+1].end(),x[i]) == G[i+1].end()){
			G[i+1].push_back(x[i]);
			G[x[i]].push_back(i+1);
			d[x[i]]++;
			d[i+1]++;
		}
	}
	for(i=1;i<=n;i++){
		if(d[i] >= 3){
			cout << 0 << endl;
			return true;
		}
	}
	for(i=1;i<=n;i++){
		if(!used[i]){
			y[ynum] = dfs(i);
			if(dfs2(i) != 2 * y[ynum] - 2){
				cout << 0 << endl;
				return true;
			}
			ynum ++;
		}
	}
	ll two = 0,three = 0;
	for(int i=0;i<ynum;i++){
		if(y[i] == 2){
			two++;
		}else if(y[i] >= 3){
			three++;
		}else{
			cout << 0 << endl;
			return true;
		}
		//cout << y[i] << " ";
	}
	//cout << endl;
	ll ans = 0;
	//cout << endl;
	for(int i=0;i<=two/2;i++){
		ll res1 = (three + 1) * fact[two + three - 2 * i];
		res1 %= mod;
		res1 *= pow(2ll,two + three);
		res1 %= mod;
		res1 *= c3(two,2*i);
		res1 %= mod;
		res1 *= fact[2*i];
		res1 %= mod;
		//cout << "res = " << res1 << endl;
		ans += res1;
		ans %= mod;
		//cout << ans << endl;
	}
	ans %= mod;
	cout << ans << endl;
	return true;

}




int main(){
	init();
	while(solve()){}
	return 0;
}