#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int mod = (int) 1e9 + 7;

int mul(int a, int b) {
  return (long long) a * b % mod;
}

void add(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}

const int maxn = (int) 1e5 + 5;
int n;
int a[maxn];
vector<int> gr[maxn];

int read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    gr[i].clear();
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
    gr[a[i]].push_back(i);
  }
  return true;
}

int dp[maxn];

int solveCycles(int len, int k) {
  dp[0] = 1;
  dp[1] = 1 + (len & 1) - (len == 1);
  for (int i = 2; i <= k; i++) {
    dp[i] = mul(dp[i - 1], 1 + (len & 1) - (len == 1));
    add(dp[i], mul(mul(i - 1, len), dp[i - 2]));
  }
  return dp[k];
}

int used[maxn];
int cnt[maxn];

void solve() {
  int res = 1;
  for (int i = 0; i < n; i++) {
    if (sz(gr[i]) > 2) {
      printf("0\n");
      return;
    }
  }
  for (int i = 0; i < n; i++) {
    used[i] = false;
  }
  for (int i = 0; i < n; i++) {
    if (used[i] || sz(gr[i]) < 2) {
      continue;
    }
    vector<int> cycle;
    for (int v = i; !used[v]; v = a[v]) {
      cycle.push_back(v);
      used[v] = true;
    }
    if (a[cycle.back()] != i) {
      printf("0\n");
      return;
    }
    vector<int> lens(sz(cycle));
    for (int j = 0; j < sz(cycle); j++) {
      int v = cycle[j];
      lens[j] = 0;
      if (sz(gr[v]) < 2) {
        continue;
      }
      int u = used[gr[v][0]] ? gr[v][1] : gr[v][0];
      while (true) {
        assert(!used[u]);
        used[u] = true;
        lens[j]++;
        if (sz(gr[u]) > 1) {
          printf("0\n");
          return;
        }
        if (gr[u].empty()) {
          break;
        }
        u = gr[u][0];
      }
    }
    lens.push_back(lens[0]);
    int lst = 0;
    for (int j = 1; j < sz(lens); j++) {
      if (!lens[j]) {
        continue;
      }
      if (lens[j] > j - lst) {
        printf("0\n");
        return;
      }
      if (lens[j] < j - lst) {
        res = mul(res, 2);
      }
      lst = j;
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (used[i]) {
      continue;
    }
    int len = 0;
    int v;
    for (v = i; !used[v]; v = a[v]) {
      used[v] = true;
      len++;
    }
    assert(v == i);
    cnt[len]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i]) {
      res = mul(res, solveCycles(i, cnt[i]));
    }
  }
  printf("%d\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 7800