#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define MXN 200005
int d[100005];
int degree[100005];
int Size[100005];
int f[100005];
int mod=1e9+7;
int Find(int x){
    if(f[x]==x)return x;
    return f[x]=Find(f[x]);
}
int cnt[100005];
LL f_pow(LL a,LL b){
    LL res=1,temp=a;
    while(b){
        if(b&1)res=res*temp%mod;
        temp=temp*temp%mod;
        b>>=1;
    }
    return res;
}
LL fra[100005];
LL inv[100005];
void build(){
    fra[0]=1;
    for(int i = 1;i<100005;i++)
    fra[i]=fra[i-1]*i%mod;
    inv[100000]=f_pow(fra[100000],mod-2);
    for(int i =99999;i>=0;i--)
        inv[i]=inv[i+1]*(i+1)%mod;
}
LL C(int a,int b){
    return fra[a]*inv[a-b]%mod;
}
vector<int> v[100005];
vector<int> stk;
int circle[100005];
int a[100005];
int vis[100005];
int val[100005];
vector<vector<int> > cir;
void dfs(int x){
   // printf("%d ",x);
    vis[x]=1;
    stk.pb(x);
    if(!vis[a[x]])dfs(a[x]);
    else{
        vector<int> tmp;
        while(stk.back()!=a[x]){
            circle[stk.back()]=1;
            tmp.pb(stk.back());
            stk.pop_back();
        }
        circle[stk.back()]=1;
        tmp.pb(stk.back());
        stk.pop_back();
        stk.clear();
        reverse(tmp.begin(),tmp.end());
        cir.pb(tmp);
    }
}
void dfs2(int x,int sum){
    if(circle[x]){
        val[x]=sum;
        return ;
    }
    dfs2(a[x],sum+1);
}
int main(){
    int n;
    scanf("%d",&n);
 
    build();
    for(int i = 1;i<=n;i++){
        scanf("%d",&a[i]);
        degree[a[i]]++;
        d[a[i]]++;
        Size[i]=1;
        f[i]=i;
    }
    for(int i = 1;i<=n;i++){
        int x=Find(i),y=Find(a[i]);
        if(x!=y){
            f[x]=y;
            Size[y]+=Size[x];
            degree[y]=max(degree[x],degree[y]);
        }
    }
    LL ans=1;
    for(int i = 1;i<=n;i++){
        if(f[i]==i&&degree[i]==1){
            cnt[Size[i]]++;
          //  printf("%d\n",Size[i]);
        }
        if(degree[i]>2)ans=0;
        //printf("%d %d %d\n",i,f[i],degree[i]);
        if(f[i]==i&&degree[i]>=2){
          //  printf("%d\n",i);
            dfs(i);
        }
    }
    for(int i = 1;i<=n;i++){
        if(d[i]==2&&!circle[i]){
            printf("0\n");
            return 0;
        }
    }
    for(int i = 1;i<=n;i++){
        if(d[i]==0)dfs2(i,0);
    }
    for(auto it:cir){
        int st=0;
        int cnt=0;
        for(int j=0;j<it.size();j++){
           // printf("%d ",it[j]);
            if(st==0)
            it.pb(it[j]);
            if(st){
                if(val[it[j]]){
                    if(cnt>=val[it[j]])ans=ans*2%mod;
                    else if(cnt==val[it[j]]-1);
                    else ans=0;
                    cnt=0;
                }
                else{
                    cnt++;
                }
            }
            if(val[it[j]]){
                st=1;
            }
        }
    }
    for(int i = 1 ;i<=n;i++){
        LL sum=0;
        for(int j=0;j<=cnt[i];j+=2){
            if(i!=1&&i%2==1)
            sum+=C(cnt[i],j)*f_pow(f_pow(2,j/2),mod-2)%mod*f_pow(i,j/2)%mod*f_pow(2,cnt[i]-j)%mod*inv[j/2]%mod;
            else
            sum+=C(cnt[i],j)*f_pow(f_pow(2,j/2),mod-2)%mod*f_pow(i,j/2)%mod*inv[j/2]%mod;
            sum%=mod;
         //   printf("%d %d %d %d %d\n",sum,C(cnt[i],j),cnt[i],j,i);
        }
        ans=ans*sum%mod;
    }
    printf("%lld\n",ans);
}

Measured memory_bytes: 17008