//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
const int nax=1000*1007;
const ll mod=1000*1000*1000+7;

void ans(ll v)
{
	v%=mod;
	v+=mod;
	v%=mod;
	printf("%lld\n", v);
	exit(0);
}

int n;
int tab[nax];
vector <int> graf[nax];

int nac[nax];
int bylo[nax];

ll wyn=1;
int justcyk[nax];

ll pasuj(int a, int b)
{
	if (a>b)
		return 0;
	if (a==b)
		return 1;
	return 2;
}

void dfs1(int v)
{
	if (bylo[v])
		return;
	bylo[v]=1;
	for (int i : graf[v])
		dfs1(i);
}

int gl(int v)
{
	int ret=0;
	for (int i : graf[v])
		if (!nac[i])
			ret=gl(i)+1;
	return ret;
}

void solve(vector <int> wek)
{
	reverse(wek.begin(), wek.end());
	int g=-1;
	int k=wek.size();
	for (int i=0; i<k; i++)
		if (wek[i])
			g=i;
	if (g==-1)
	{
		justcyk[k]++;
		return;
	}
	for (int i=0; i<g; i++)
		wek.push_back(wek[i]);
	reverse(wek.begin(), wek.end());
	for (int i=0; i<g; i++)
		wek.pop_back();
	reverse(wek.begin(), wek.end());
	debug() << wek;
	int w=0;
	while(w<k)
	{
		int p=w+1;
		while(!wek[p%k])
			p++;
		//~ debug() << wek[w] << " " << p-w;
		wyn=(wyn*pasuj(wek[w], p-w))%mod;
		w=p;
	}
}

signed main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
	{
		scanf("%d", &tab[i]);
		graf[tab[i]].push_back(i);
	}
	for (int i=1; i<=n; i++)
	{
		if (bylo[i])
			continue;
		int v=i;
		int u=tab[i];
		while(v!=u)
		{
			v=tab[v];
			u=tab[tab[u]];
		}
		while(!nac[u])
		{
			nac[u]=1;
			u=tab[u];
		}
		dfs1(v);
	}
	for (int i=1; i<=n; i++)
	{
		if ((int)graf[i].size()>2)
			ans(0);
		if (!nac[i] && (int)graf[i].size()>1)
			ans(0);
	}
	for (int i=1; i<=n; i++)
		bylo[i]=0;
	for (int i=1; i<=n; i++)
	{
		if (bylo[i])
			continue;
		int v=i;
		int u=tab[i];
		while(v!=u)
		{
			v=tab[v];
			u=tab[tab[u]];
		}
		vector <int> cyk;
		while(cyk.empty() || v!=u)
		{
			cyk.push_back(u);
			u=tab[u];
		}
		vector <int> zbi;
		for (int i : cyk)
			zbi.push_back(gl(i));
		//~ debug() << zbi;
		solve(zbi);
		dfs1(v);
	}
	for (int h=1; h<=n; h++)
	{
		vector <ll> dp{1};
		int k=justcyk[h];
		for (int i=1; i<=k; i++)
		{
			dp.push_back(0);
			dp[i]+=dp[i-1]*1;
			if (i>1)
				dp[i]+=dp[i-2]*(((i-1LL)*h)%mod);
			if ((h&1) && h>2)
				dp[i]+=dp[i-1]*1;
			
			dp[i]%=mod;
		}
		wyn=(wyn*dp[k])%mod;
	}
	ans(wyn);
	return 0;
}


Measured memory_bytes: 41720