#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

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

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.0);

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

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

int mult(int x, int y) {
  return (long long) x * y % mod;
}

int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

const int maxn = (int) 1e5 + 10;
int twos[maxn], inv[maxn];

void precalc() {
  twos[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    twos[i] = twos[i - 1];
    add(twos[i], twos[i - 1]);

    inv[i] = myPower(i, mod - 2);
  }
}


int a[maxn];
int n;

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

int used[maxn];
vector<vector<int> > es;

int cnt[maxn];

void dfs(int v, vector<int> &vs) {
  vs.pb(v);
  used[v] = 1;
  for (int it = 0; it <= sz(es[v]); ++it) {
    int u = it < sz(es[v]) ? es[v][it] : a[v];
    if (used[u]) {
      continue;
    }
    dfs(u, vs);
  }
}

void solve() {
  es = vector<vector<int> >(n);
  for (int i = 0; i < n; ++i) {
    es[a[i]].pb(i);
  }
  for (int i = 0; i < n; ++i) {
    used[i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    cnt[i] = 0;
  }

  int res = 1;
  for (int i = 0; i < n; ++i) {
    if (used[i]) {
      continue;
    }
    vector<int> vs;
    dfs(i, vs);
    int v = -2;
    for (int x : vs) {
      if (sz(es[x]) == 2) {
        v = x;
        break;
      }
      if (sz(es[x]) != 1) {
        v = -1;
      }
    }
    if (v < 0) {
      if (v == -1) {
        printf("0\n");
        return;
      }
      cnt[sz(vs)] += 1;
      continue;
    }

    int v0 = v;
    while (1) {
      if (used[v] == 2) {
        if (v != v0) {
          printf("0\n");
          return;
        }
        break;
      }
      used[v] = 2;
      if (sz(es[v]) > 2) {
        printf("0\n");
        return;
      }
      assert(sz(es[v]) > 0);
      if (sz(es[v]) == 1) {
        v = es[v][0];
        continue;
      }
      int us[2] = {es[v][0], es[v][1]};
      int degs[2];
      while (1) {
        for (int i = 0; i < 2; ++i) {
          degs[i] = sz(es[us[i]]);
          if (degs[i] > 2) {
            printf("0\n");
            return;
          }
        }
        if (degs[0] == 1 && degs[1] == 1) {
          us[0] = es[us[0]][0];
          us[1] = es[us[1]][0];
          continue;
        }
        break;
      }
      if (degs[0] && degs[1]) {
        printf("0\n");
        return;
      }
      if (degs[0]) {
        swap(us[0], us[1]);
        swap(degs[0], degs[1]);
      }
      assert(degs[0] == 0);

      if (degs[1] == 1) {
        add(res, res);
        v = us[1];
        continue;
      }
      assert(degs[1] == 2);
      v = us[1];
    }
  }

  for (int k = 1; k <= n; ++k) {
    int cur = 0;

    int val = 1;
    for (int x = 0; x <= cnt[k]; x += 2) {
      int toadd = val;
      if (k > 1 && (k & 1)) {
        toadd = mult(toadd, twos[cnt[k] - x]);
      }
      add(cur, toadd);


      val = mult(val, x + 1);

      val = mult(val, cnt[k] - x);
      val = mult(val, inv[x + 1]);
      val = mult(val, cnt[k] - x - 1);
      val = mult(val, inv[x + 2]);
      val = mult(val, k);
    }
    eprintf("k = %d, cnt = %d, cur = %d\n", k, cnt[k], cur);
    res = mult(res, cur);
  }

  printf("%d\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 13044