Problem:
Score : 1400 points

Problem Statement

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.

Sample Input 1

3
1 2 3

Sample Output 1

4

The following four permutations satisfy the condition:

(1, 2, 3)

(1, 3, 2)

(3, 2, 1)

(2, 1, 3)

For example, (1, 3, 2) satisfies the condition because p_1 = 1, p_{p_2} = 2 and p_{p_3} = 3.

Sample Input 2

2
1 1

Sample Output 2

1

The following one permutation satisfies the condition:

(2, 1)

Sample Input 3

3
2 1 1

Sample Output 3

2

The following two permutations satisfy the condition:

(2, 3, 1)

(3, 1, 2)

Sample Input 4

3
1 1 1

Sample Output 4

0

Sample Input 5

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

Sample Output 5

6

Solution in C++ (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std ;
#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )
#define re register
#define int long long
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 4e5 + 5 ; 
const int P = 1e9 + 7 ; 
int n, Ans, w[N], a[N], Go[N], fac[N], inv[N], deg[N], vis[N], cir[N], L[N] ;
int fpow( int x, int k ) {
	int ans = 1, base = x % P ; 
	while( k ) {
		if( k & 1 ) ans = ans * base % P ; 
		base = base * base % P, k >>= 1 ; 
	} return ans ; 
}  
int C( int x, int y ) {
	if( y > x ) return 0 ;
	return fac[x] * inv[y] % P * inv[x - y] % P ; 
}
int top, st[N], R[N] ; 
int solve( int x ) {
	int u = x, l = 0, r = 0, ans = 1 ; top = 0 ;
	while( !l || u != x ) ++ l, u = Go[u] ; 
	r = l, l = 0 ; 
	while( !l || u != x ) {
		if( L[u] ) st[++ top] = L[u], R[top] = r - l ; 
		++ l, u = Go[u] ; 
	}
	if( top == 1 ) {
		if( st[top] > l ) return 0 ;
		if( st[top] == l ) return 1 ; 
		return 2 ; 
	}
	reverse( st + 1, st + top + 1 ), reverse( R + 1, R + top + 1 ) ;
	for( re int i = 2; i <= top; ++ i ) {
		int ug = R[i - 1] + st[i - 1] ; 
		int uk = ( ug <= R[i] ) + ( ug < R[i] ) ;
		ans = ans * uk % P ; 
	}
	if( st[top] >= l ) return 0 ;
	int ug = R[top] + st[top] ;
	if( ug > l ) ug -= l ;
	int uk = ( ug <= R[1] ) + ( ug < R[1] ) ;
	ans = ans * uk ; 
	return ans ; 
}
signed main()
{
	n = 4 ; rep( i, 1, n ) a[i] = i, Go[i] = a[i], ++ deg[a[i]] ; 
	inv[0] = fac[0] = 1 ; 
	for( re int i = 1; i <= 2 * n; ++ i ) fac[i] = fac[i - 1] * i % P, inv[i] = fpow( fac[i], P - 2 ) ;
	rep( i, 1, n ) {
		if( vis[i] ) continue ; 
		int x = i ; while( !vis[x] ) vis[x] = i, x = Go[x] ; 
		if( vis[x] != i ) continue ;
		while( !cir[x] ) cir[x] = 1, x = Go[x] ; 
	}
	rep( i, 1, n ) if( ( cir[i] && deg[i] > 2 ) || ( !cir[i] && deg[i] > 1 ) ) { puts("0") ; exit(0) ; }
	rep( i, 1, n ) {
		if( deg[i] ) continue ; 
		int x = i, l = 0 ; while( !cir[x] ) ++ l, x = Go[x] ;
		L[x] = l ; 
	}
	Ans = 1 ;
	memset( vis, 0, sizeof(vis) ) ;
	rep( i, 1, n ) {
		if( !cir[i] ) continue ; 
		if( vis[i] ) continue ; 
		int l = 0, x = i, flag = 0 ; 
		while( !vis[x] ) ++ l, vis[x] = 1, x = Go[x], flag |= ( L[x] != 0 ) ; 
		if( !flag ) ++ w[l] ;
		else Ans = ( Ans * solve(i) ) % P ; 
	}
	for( re int i = 1; i <= n; ++ i ) {
		int ans = 0 ;
		for( re int j = 0; j <= w[i] / 2; ++ j ) {
			int num = C( w[i], 2 * j ) ;
			if( ( i & 1 ) && ( i != 1 ) ) num = ( num * fpow( 2, w[i] - 2 * j ) ) % P ; 
			num = ( num * fpow( i, j ) ) % P ; 
			num = ( num * fac[2 * j] ) % P ;
			num = ( num * fpow( fpow( 2, j ) % P, P - 2 ) ) % P ;
			num = ( num * inv[j] ) % P ;
			ans = ( ans + num ) % P ;
		}
		Ans = Ans * ans % P ;
	}
	printf("%lld\n", Ans ) ;
	return 0 ;
}