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

typedef long long ll;
typedef pair<int,int> P;

ll fact[100001];

ll extgcd(ll a,ll b,ll& x,ll& y){
	ll d=a;
	if(b!=0LL){
		d=extgcd(b,a%b,y,x);
		y-=(a/b)*x;
	}else{
		x=1;
		y=0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
	ll x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}

ll mod_fact(ll n,ll p,ll& e){
	e=0;
	if(n==0)return 1;
	ll res=mod_fact(n/p,p,e);
	e+=n/p;
	if(n/p%2!=0){
		return res*(p-fact[n%p])%p;
	}
	return res*fact[n%p]%p;
}

ll mod_comb(ll n,ll k,ll p){
	if(n<0 || k<0 || n<k)return 0;
	ll e1,e2,e3;
	ll a1=mod_fact(n,p,e1),a2=mod_fact(k,p,e2),a3=mod_fact(n-k,p,e3);
	if(e1>e2+e3)return 0;
	return a1*mod_inverse(a2*a3%p,p)%p;
}

int n;
vector<int> G[100001];
bool used[100001];
ll twopow[100001];
bool check[100001];
int cnt=0;
int last=0;
int a[100001];
bool flag;

void dfs(int v){
	used[v]=true;
	cnt++;
	last=v;
	for(int i=0;i<G[v].size();i++){
		if(!used[G[v][i]]){
			dfs(G[v][i]);
		}
	}
}

void dfs2(int v,int p){
	check[v]=true;
	int cc=0;
	for(int i=0;i<G[v].size();i++){
		if(!check[G[v][i]]){
			dfs2(G[v][i],v);
			cc++;
		}else{
			if(p!=G[v][i]){
				flag=false;
			}
		}
	}
	if(cc>1){
		flag=false;
	}
}

int main(void){
	twopow[0]=1;
	fact[0]=1;
	for(int i=1;i<=100000;i++){
		twopow[i]=twopow[i-1]*2LL%MOD;
		fact[i]=(ll)fact[i-1]*i%MOD;
	}
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			G[i].clear();
		}
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
			a[i]--;
		}
		for(int i=0;i<n;i++){
			if(a[a[i]]!=i || i<a[i]){
				G[i].push_back(a[i]);
				G[a[i]].push_back(i);
			}
		}
		memset(used,false,sizeof(used));
		memset(check,false,sizeof(check));
		int sum=0;
		flag=true;
		int v1=0,v2=0;
		for(int i=0;i<n;i++){
			if(!used[i]){
				cnt=0;
				dfs(i);
				sum++;
				dfs2(last,-1);
				if(cnt==2){
					v1++;
				}else{
					v2++;
				}
			}
		}
		if(!flag){
			printf("0\n");
		}else if(v2>0){
			ll res=0;
			for(int i=0;i<=v1;i++){
				ll val=(i/2)+1;
				ll val2=mod_comb(v1+v2-i-2,v1-i,MOD)*(v1+v2-i-1)%MOD;
				ll val3=2LL*mod_comb(v1+v2-i-1,v1-i,MOD)%MOD;
				if(v2>=2)res=(res+(val*val2%MOD))%MOD;
				res=(res+(val*val3%MOD))%MOD;
			}
			res=res*twopow[v1+v2]%MOD;
			res=res*fact[v1]%MOD;
			res=res*fact[v2]%MOD;
			printf("%lld\n",res);
		}else{
			ll res=twopow[v1];
			res=res*(v1/2LL+1LL)%MOD;
			res=res*fact[v1]%MOD;
			printf("%lld\n",res);
		}
	}
	return 0;
}