Problem: You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?

* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.



Find the count modulo 10^9 + 7.

Constraints

* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N

Input

The input is given from Standard Input in the following format:


N
a_1 a_2 ... a_N


Output

Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.

Examples

Input

3
1 2 3


Output

4


Input

2
1 1


Output

1


Input

3
2 1 1


Output

2


Input

3
1 1 1


Output

0


Input

13
2 1 4 3 6 7 5 9 10 8 8 9 11


Output

6

Solution (in CPP):

Solution:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
    return powmod(a, MOD-2);
}
ll f[100010], invf[100010];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;
    invf[n]=inv(f[n]);
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
}
ll comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]%MOD*invf[x-y]%MOD;
}
ll p2[100010];
int main()
{
	int n; cin>>n;
  p2[0]=1;
  for(int i=1; i<=n; i++) p2[i]=p2[i-1]*2%MOD;
  //vector<int> g[100010];
  int deg[100010]={};
  int a[100010];
  for(int i=0; i<n; i++){
    cin>>a[i];
    a[i]--;
    //g[i].push_back(a);
    deg[a[i]]++;
  }
  for(int i=0; i<n; i++){
    if(deg[i]>=3){
      cout<<0<<endl;
      return 0;
    }
  }
  int myon[100010]={};
  int nuo[100010]={};
  bool used[100010]={};
  for(int i=0; i<n; i++){
    if(deg[i]==0){
      int x=i, cnt=0;
      while(deg[x]<2){
        used[x]=1;
        cnt++;
        x=a[x];
      }
      myon[x]=cnt;
    }
  }
  for(int i=0; i<n; i++){
    if(deg[i]==2){
      int x=i, cnt=0;
      do{
        used[x]=1;
        cnt++;
        x=a[x];
      }while(deg[x]<2);
      nuo[x]=cnt;
    }
  }
  map<int, int> mp;
  for(int i=0; i<n; i++){
    if(!used[i]){
      int x=i, cnt=0;
      while(!used[x]){
        used[x]=1;
        cnt++;
        x=a[x];
      }
      mp[cnt]++;
    }
  }
  ll ans=1;
  for(int i=0; i<n; i++){
    if(myon[i]){
      //cout<<nuo[i]<<" "<<myon[i]<<endl;
      if(nuo[i]<myon[i]){
        cout<<0<<endl;
        return 0;
      }
      if(nuo[i]>=myon[i]+1) (ans*=2)%=MOD;
    }
  }
  //cout<<"myon"<<endl;
  fac(n);
  for(auto p:mp){
    int d=p.first, m=p.second;
    //cout<<d<<" "<<m<<endl;
    ll pp=1, q=1, s=0;
    for(int i=0; i<=m/2; i++){
      if((d&1) && d>1) (s+=pp*q%MOD*comb(m, 2*i)%MOD*p2[m-2*i])%=MOD;
      else (s+=pp*q%MOD*comb(m, 2*i))%=MOD;
      (pp*=d)%=MOD;
      (q*=(2*i+1))%=MOD;
    }
    (ans*=s)%=MOD;
  }
  cout<<ans<<endl;
 	return 0;
}