Problem: You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?

* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.



Find the count modulo 10^9 + 7.

Constraints

* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N

Input

The input is given from Standard Input in the following format:


N
a_1 a_2 ... a_N


Output

Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.

Examples

Input

3
1 2 3


Output

4


Input

2
1 1


Output

1


Input

3
2 1 1


Output

2


Input

3
1 1 1


Output

0


Input

13
2 1 4 3 6 7 5 9 10 8 8 9 11


Output

6

Solution (in CPP):

Solution:
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)n; ++i)

int an = 1, cy[100001], md = 1e9 + 7, pairs[100001], fact[100001], invf[100001], inv[100001];
pair<int, int> y[100001], ln[100001];
bool vis[100001];

void no(){
	printf("0\n");
	exit(0);
}

int V;
bool go(int v, int m = 0, int d = 0){
	if (vis[v]){
		if (v == V) { ln[m].first = d; return true; }
		return false;
	}
	vis[v] = true;
	if (!y[v].first) { ln[m].second = d; return false; }
	bool k = go(y[v].first, y[v].second ? v : m, d + 1);
	if (y[v].second)ln[m].first = d, k |= go(y[v].second, v, d + 1);
	return k;
}

inline int ch(int n, int r) { return (ll)fact[n] * invf[r] % md * invf[n - r] % md; }

int pw(int x, int p){
	if (!p)return 1;
	int t = pw(x, p >> 1);
	t = (ll)t * t % md;
	if (p & 1)t = (ll)t * x % md;
	return t;
}

inline void ad(int s, int l){
	int a = 0, z = (s & 1 && s != 1) + 1;
	for (int i = 0; i <= l; i += 2){
		a += (ll)pw(z, l - i) * ch(l, i) % md * pw(s, i >> 1) % md * pairs[i] % md;
		if (a >= md)a -= md;
	}
	an = (ll)an * a % md;
}

int main(){
	int n;
	scanf("%d", &n);
	fact[0] = fact[1] = inv[1] = invf[1] = pairs[0] = invf[0] = 1;
	f(i, 2, n + 1){
		fact[i] = (ll)fact[i - 1] * i % md;
		inv[i] = md - md / i * (ll)inv[md % i] % md;
		invf[i] = (ll)invf[i - 1] * inv[i] % md;
		if (!(i & 1))pairs[i] = (ll)pairs[i - 2] * (i - 1) % md;
	}
	f(i, 1, n + 1){
		int t;
		scanf("%d", &t);
		if (y[t].first){
			if (y[t].second)no();
			y[t].second = i;
		}
		else y[t].first = i;
	}
	f(i, 1, n + 1)if (y[i].second){
		V = i;
		if (!vis[i] && !go(i))no();
		if (!ln[i].second)no();
		if (ln[i].first > ln[i].second){ if ((an <<= 1) >= md)an -= md; }
		else if (ln[i].first < ln[i].second)no();
	}
	f(i, 1, n + 1)if (!vis[i]){
		V = i;
		if (go(i, i))++cy[ln[i].first];
	}
	f(i, 1, n + 1)if (cy[i])ad(i, cy[i]);
	printf("%d\n", an);
}