SIZE=10**5+1; MOD=10**9+7 #998244353 #ここを変更する inv = [0]*SIZE # inv[j] = j^{-1} mod MOD fac = [0]*SIZE # fac[j] = j! mod MOD finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 for i in range(2,SIZE): fac[i] = fac[i-1]*i%MOD finv[-1] = pow(fac[-1],MOD-2,MOD) for i in range(SIZE-1,0,-1): finv[i-1] = finv[i]*i%MOD inv[i] = finv[i]*fac[i-1]%MOD def choose(n,r): # nCk mod MOD の計算 if 0 <= r <= n: return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD else: return 0 def perm(n,r): # nPr mod MOD if 0 <= r <= n: return fac[n]*finv[n-r]%MOD else: return 0 n = int(input()) *p, = map(int,input().split()) for i in range(n): p[i] -= 1 res = [] used = [0]*n for i in range(n): if used[i]: continue c = 0 while used[i]==0: used[i] = 1 i = p[i] c += 1 res.append(c) #print(res) ans = fac[n] for x in res: ans *= inv[x] ans %= MOD from collections import Counter d = Counter(res) for v in d.values(): ans *= finv[v] ans %= MOD for x in res: if x%2==0: break else: if len(res) >= 2: ans *= inv[2] ans %= MOD print(ans)