// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long i64;



const int MAXN = 100000 + 5;

const int MOD = 1e9 + 7;



int N;

i64 answer, dp[MAXN];

int degree[MAXN], lenCnt[MAXN];

int pre[MAXN], nxt[MAXN];

bool vis[MAXN];



bool noSolution = false;



void findCycle()

{

	for (int i = 1; i <= N; i++)

	{

		if (degree[i] > 2)

			return noSolution = true, (void) 0;

		if (degree[i] != 2 || vis[i])

			continue;



		int cur = i;



		do

		{

			if (vis[cur])

				return noSolution = true, (void) 0;

			vis[cur] = true;

			pre[nxt[cur]] = cur;

			cur = nxt[cur];

		} while (cur != i);

	}

}



void processCircleBasedTree()

{

	for (int i = 1; i <= N; i++)

	{

		if (degree[i])

			continue;



		int cur = i, footLen = 0, cycleLen = 0;

		while (!vis[cur])

			vis[cur] = true,

			cur = nxt[cur],

			footLen++;

		do

			cur = pre[cur],

			cycleLen++;

		while (degree[cur] == 1);

		if (footLen < cycleLen)

			answer = answer * 2 % MOD;

		if (footLen > cycleLen)

			return noSolution = true, (void) 0;

	}

}



void countCycle()

{

	for (int i = 1; i <= N; i++)

	{

		if (vis[i])

			continue;

		int cur = i, len = 0;

		do

		{

			vis[cur] = true;

			cur = nxt[cur];

			len++;

		} while (cur != i);

		lenCnt[len]++;

	}

}



int main()

{

	answer = 1;

	ios::sync_with_stdio(false), cin.tie(NULL);

	cin >> N;



	for (int i = 1; i <= N; i++)

		cin >> nxt[i], degree[nxt[i]]++;



	findCycle();

	if (noSolution)

		return cout << 0 << endl, 0;

	processCircleBasedTree();

	if (noSolution)

			return cout << 0 << endl, 0;

	countCycle();

	if (noSolution)

		return cout << 0 << endl, 0;



	for (int i = 1; i <= N; i++)

	{

		dp[0] = 1LL;

		for (int j = 1; j <= lenCnt[i]; j++)

		{

			dp[j] = dp[j - 1] * (((i % 2 == 0) || i == 1) ? 1 : 2);

			if (j >= 2)

				dp[j] = (dp[j] + ((((dp[j - 2] * (j - 1)) % MOD) * i) % MOD))

						% MOD;

		}



		answer = answer * dp[lenCnt[i]] % MOD;

	}

	cout << answer << endl;

}




// Faster C++ solution for the same problem (measured speedup: 5.449369676838847x):
#include<cstdio>

#include<cctype>

const int N=100007,P=1000000007;

int a[N],deg[N],vis[N],pre[N],cnt[N],f[N];

int read(){int x=0,c=getchar();while(isspace(c))c=getchar();while(isdigit(c))(x*=10)+=c&15,c=getchar();return x;}

int add(int a,int b){return a+=b-P,a+(a>>31&P);}

int mul(int a,int b){return 1ll*a*b%P;}

int main()

{

    int n=read(),ans=1;

    for(int i=1;i<=n;++i) ++deg[a[i]=read()];

    for(int i=1,p;(p=i)<=n;++i)

    {

	if(deg[i]>2) return puts("0"),0;

	if(deg[i]<2||vis[i]) continue;

	do

	{

	    if(vis[p]) return puts("0"),0;

	    vis[p]=1,pre[a[p]]=p,p=a[p];

	}while(p^i);

    }

    for(int i=1,p,l1,l2;i<=n;++i)

	if(!deg[i])

	{

	    for(p=i,l1=0,l2=0;!vis[p];p=a[p]) vis[p]=1,++l1;

	    do ++l2,p=pre[p]; while(deg[p]^2);

	    if(l1<l2) ans=add(ans,ans); else if(l1>l2) return puts("0"),0;

	}

    for(int i=1;i<=n;++i)

	if(!vis[i])

	{

	    int p=i,l=0;

	    do ++l,p=a[p],vis[p]=1; while(p^i);

	    ++cnt[l];

	}

    for(int i=1;i<=n;ans=mul(ans,f[cnt[i++]]))

    {

	f[0]=1,f[1]=1+(i^1&&i&1);

	for(int j=2;j<=cnt[i];++j) f[j]=add(mul(i,mul(j-1,f[j-2])),mul(f[1],f[j-1]));

    }

    printf("%d",ans);

}