#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
inline unsigned long long rnd() {
  unsigned long long ans = 0;
  for (int i = (int)(0); i <= (int)(5); i++) ans = ans << 15 ^ rand();
  return ans;
}
const int N = 100005, mod = 1000000007;
int a[N], f[N], fa[N], TO[N];
vector<int> pre[N], v[N];
long long fac[N], ni[N], ans = 1;
bool vis[N];
inline int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
void GG(int frog) {
  if (!frog) {
    puts("0");
    exit(0);
  }
}
int getpre(int x, int id) {
  for (int i = (int)(0); i <= (int)(1); i++)
    if (vis[pre[x][i]] == id) return pre[x][i];
  assert(0);
}
long long ksm(long long a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
void mer(int x, int y) { fa[gf(x)] = gf(y); }
int main() {
  int n = read();
  for (int i = (int)(fac[0] = 1); i <= (int)(n); i++)
    fac[i] = fac[i - 1] * i % mod;
  ni[n] = ksm(fac[n], mod - 2);
  for (int i = (int)(n); i >= (int)(1); i--) ni[i - 1] = ni[i] * i % mod;
  for (int i = (int)(1); i <= (int)(n); i++)
    pre[a[fa[i] = i] = read()].push_back(i);
  for (int i = (int)(1); i <= (int)(n); i++) mer(i, a[i]);
  for (int i = (int)(1); i <= (int)(n); i++) v[gf(i)].push_back(i);
  for (int i = (int)(1); i <= (int)(n); i++)
    if (fa[i] == i) {
      int f = 1;
      for (auto j : v[i])
        if (pre[j].size() != 1) f = 0;
      if (f)
        TO[v[i].size()]++;
      else {
        for (int j = i;; j = a[j]) {
          if (!vis[j])
            vis[j] = 1;
          else {
            for (auto k : v[i]) vis[k] = 0;
            vis[j] = 1;
            for (int k = a[j]; k != j; k = a[k]) vis[k] = 1;
            bool t = 0;
            int sz = 0;
            for (int k = j; t == 0 || k != j; k = a[k]) {
              t = 1;
              int f = 0, g = 0;
              if (pre[k].size() >= 2) {
                GG(pre[k].size() == 2);
                for (int l = getpre(k, 1); pre[l].size() == 1; l = pre[l][0])
                  f++;
                for (int l = getpre(k, 0); pre[l].size(); l = pre[l][0]) {
                  GG(pre[l].size() == 1);
                  g++;
                }
                g++;
                GG(g <= f + 1);
                if (g <= f) ans = ans * 2 % mod;
              }
            }
            break;
          }
        }
      }
    }
  for (int i = (int)(1); i <= (int)(n); i++)
    if (TO[i]) {
      int d = ((i & 1) && i > 1) ? 2 : 1;
      int sum = ksm(d, TO[i]);
      for (int j = (int)(1); j <= (int)(TO[i] / 2); j++)
        sum = (sum + fac[TO[i]] * ni[j] % mod * ni[TO[i] - j - j] % mod *
                         ksm(d, TO[i] - j - j) % mod *
                         ksm((long long)i * (mod + 1) / 2 % mod, j)) %
              mod;
      ans = ans * sum % mod;
    }
  cout << ans << endl;
}
