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:
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
#define INF 1e+18
#define MOD 1000000007
#define int long long

typedef pair<int, int> P;

int mod_inv(int X) {
	int a = X, b = MOD, u = 1, v = 0;
	while (b) {
		int t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return u;
}

vector<int> fact, factinv;

void nCr_init(int N) {
	N = min(N, (int)MOD);
	fact.resize(N + 1);
	factinv.resize(N + 1);
	fact[0] = 1;
	for (int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i % MOD;
	factinv[N] = mod_inv(fact[N]);
	for (int i = N; i >= 1; i--) factinv[i - 1] = factinv[i] * i % MOD;
}

int nCr(int ncr_n, int ncr_r) {
	return ncr_r > ncr_n ? 0 : fact[ncr_n] * factinv[ncr_n - ncr_r] % MOD * factinv[ncr_r] % MOD;
}

vector<int> G[100010];
bool flag = true;
int cnt;
bool visited[100010];

void dfs(int v, int par) {
	cnt++;
	visited[v] = true;
	for (int to : G[v]) {
		if (to != par) {
			if (visited[to]) flag = false;
			else dfs(to, v);
		}
	}
}

signed main() {
	int n;
	nCr_init(100010);
	while (cin >> n, n) {
		int x = 0, y = 0, ans = 0;
		set<P> st;
		for (int i = 0; i < n; i++) G[i].clear();
		for (int i = 0; i < n; i++) {
			int a;
			cin >> a; a--;
			if (!st.count(P(min(i, a), max(i, a)))) {
				G[a].push_back(i);
				G[i].push_back(a);
				st.insert(P(min(i, a), max(i, a)));
			}
		}
		flag = true;
		for (int i = 0; i < n; i++) {
			if (G[i].size() >= 3) flag = false;
		}
		if (!flag) {
			cout << 0 << endl;
			continue;
		}
		for (int i = 0; i < n; i++) visited[i] = false;
		for (int i = 0; i < n; i++) {
			if (!visited[i]) {
				cnt = 0;
				dfs(i, -1);
				if (cnt == 2) y++;
				else x++;
			}
		}
		if (!flag) {
			cout << 0 << endl;
			continue;
		}
		if (x >= 1) {
			for (int i = 0; i <= y; i++) {
				if (x >= 2) ans = (ans + (x + y - 1 - i) * x % MOD * (x - 1) % MOD * fact[x + y - 2 - i] % MOD * fact[i + 1] % MOD * (int)((i + 2) / 2) % MOD * mod_inv(i + 1) % MOD * nCr(y, i)) % MOD;
				//cout << ans << endl;
				ans = (ans + 2 * x % MOD * fact[x + y - 1 - i] % MOD * fact[i + 1] % MOD * (int)((i + 2) / 2) % MOD * mod_inv(i + 1) % MOD * nCr(y,i)) % MOD;
				//cout << ans << endl;
			}
		}
		else ans = fact[y + 1] * (int)((y + 2) / 2) % MOD * mod_inv(y + 1) % MOD;
		for (int i = 0; i < x + y; i++) ans = ans * 2 % MOD;
		cout << ans << endl;
		//cout << endl;
	}
}