#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1000005;
int n, p[N], cnt[N], Cnt[N], base[N], To1[N], To2[N], jc[N], x, ans;
vector<int> fac[N];
void fail() {
  puts("0");
  exit(0);
}
void shai() {
  for (int i = 2; i <= n; i++)
    if (!p[i]) {
      cnt[n / i]++;
      for (int j = i; j <= n; j += i) {
        p[j] = 1;
        base[j] *= i;
        fac[j].push_back(i);
      }
    }
}
void check(int x, int y) {
  if (fac[x].size() != fac[y].size()) fail();
  for (int i = 0; i < fac[x].size(); i++) {
    int fu = fac[x][i], fv = fac[y][i];
    int u = (x == 1) ? 1 : n / fu, v = (y == 1) ? 1 : n / fv;
    if (u != v) fail();
    if (To1[fu] && To1[fu] != fv) fail();
    if (To2[fv] && To2[fv] != fu) fail();
    if (!To2[fv]) To1[fu] = fv, To2[fv] = fu, cnt[u]--;
  }
  Cnt[base[x]]--;
}
int main() {
  scanf("%d", &n);
  jc[0] = 1;
  cnt[1] = 1;
  for (int i = 1; i <= n; i++)
    jc[i] = (long long)jc[i - 1] * i % mod, base[i] = 1;
  shai();
  fac[1].push_back(1);
  for (int i = 1; i <= n; i++) Cnt[base[i]]++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x) check(i, x);
  }
  ans = 1;
  for (int i = 1; i <= n; i++)
    ans = (long long)ans * jc[cnt[i]] % mod * jc[Cnt[i]] % mod;
  printf("%d\n", ans);
  return 0;
}
