#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}
template<unsigned MOD> struct ModInt {
    static const unsigned static_MOD = MOD;
    unsigned x;
    void undef() { x = (unsigned)-1; }
    bool isnan() const { return x == (unsigned)-1; }
    inline int geti() const { return (int)x; }
    ModInt() { x = 0; }
    ModInt(const ModInt &y) { x = y.x; }
    ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; }
    ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; }
    ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; }
    ModInt operator-() const { return (x ? MOD-x: 0); }

    ModInt inv() const {
	unsigned a = MOD, b = x; int u = 0, v = 1;
	while (b) {
	    int t = a / b;
	    a -= t * b; swap(a, b);
	    u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return ModInt(u);
    }
    ModInt pow(long long y) const {
	ModInt b = *this, r = 1;
	if (y < 0) { b = b.inv(); y = -y; }
	for (; y; y>>=1) {
	    if (y&1) r *= b;
	    b *= b;
	}
	return r;
    }
    friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
    friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
    friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
    friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
    friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
    friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
    friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};

const LL MOD = 1000000007;
//const LL MOD = 998244353;
typedef ModInt<MOD> Mint;

int N;
int A[100011];
VI R[100011];
int Y[100011];
int H[100011];
int C[100011];
int deg[100011];
bool use[100011];

void MAIN() {
    scanf("%d", &N);
    REP (i, N) scanf("%d", A+i), A[i]--;

    REP (i, N) R[A[i]].push_back(i);
    memset(Y, -1, sizeof Y);
    VI ord;
    Mint ans = 1;

    REP (i, N) if (!use[i]) {
	int s = i;
	ord.clear();
	ord.push_back(s);
	use[s] = true;
	for (int j=0; j<(int)ord.size(); j++) {
	    int v = ord[j];
	    if (!use[A[v]]) {
		ord.push_back(A[v]);
		use[A[v]] = true;
	    }
	    EACH (e, R[v]) if (!use[*e]) {
		ord.push_back(*e);
		use[*e] = true;
	    }
	}

	REP (t, ord.size()) s = A[s];

	VI cycle;
	while (Y[s] == -1) {
	    cycle.push_back(s);
	    Y[s] = s;
	    s = A[s];
	}
	if (cycle.size() == ord.size()) {
	    C[cycle.size()]++;
	} else {
	    EACH (e, ord) if (Y[*e] == -1) {
		deg[A[*e]]++;
		if (deg[A[*e]] == 2) {
		    ans = 0;
		}
	    }
	    EACH (e, ord) if (R[*e].empty()) {
		int v = *e;
		int cnt = 0;
		while (Y[v] == -1) {
		    cnt++;
		    v = A[v];
		}
		H[v] = cnt;
	    }
	    
	    int st = 0;
	    EACH (e, cycle) {
		if (H[*e] == 0) {
		    st++;
		} else {
		    st = 0;
		}
	    }
	    EACH (e, cycle) {
		if (H[*e] == 0) {
		    st++;
		} else {
		    if (st + 1 < H[*e]) {
			ans = 0;
		    } else if (st + 1 > H[*e]) {
			ans *= 2;
		    }
		    st = 0;
		}
	    }
	}
    }


    REP (len, N+1) if (C[len]) {
	Mint k = (len>=3 && len&1 ? 2: 1);
	Mint d0 = 1, d1 = 0, d2;
	REP (i, C[len]) {
	    d1 += d0 * k;
	    d2 = d0 * len * (C[len] - i - 1);
	    d0 = d1;
	    d1 = d2;
	}
	ans *= d0;
    }

    printf("%d\n", ans.geti());
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 8188