#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int maxn = 1000100;
int fac[maxn + 5];
inline void prepare() {
  fac[0] = 1;
  for (int i = (0), i_end_ = (maxn); i < i_end_; ++i)
    fac[i + 1] = (long long)fac[i] * (i + 1) % Mod;
}
int n;
int a[maxn + 5];
bool notpr[maxn + 5];
int res[maxn + 5];
int ker[maxn + 5];
int bel[maxn + 5], cnt[maxn + 5], val[maxn + 5];
bool vis[maxn + 5];
inline int calc() {
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) res[i] = i, ker[i] = 1;
  memset(notpr, 0, sizeof notpr);
  int ret = 1;
  for (int i = 2; i * i <= n; ++i)
    if (!notpr[i])
      for (int j = i; j <= n; j += i) {
        ker[j] *= i;
        notpr[j] = 1;
        while (!(res[j] % i)) res[j] /= i;
      }
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    if (a[i] && ker[i] != ker[a[i]]) return 0;
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) ker[i] *= res[i];
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) ++cnt[ker[i]];
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    if (a[i]) --cnt[ker[i]];
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    ret = (long long)ret * fac[cnt[i]] % Mod;
  memset(cnt, 0, sizeof cnt);
  memset(vis, 0, sizeof vis);
  bel[1] = 1;
  for (int i = (2), i_end_ = (n + 1); i < i_end_; ++i)
    if (!notpr[i]) bel[i] = n / i;
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    if (bel[i]) ++cnt[bel[i]];
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    if (a[i]) {
      if (bel[res[i]] != bel[res[a[i]]]) return 0;
      if (i == 1 || res[i] > 1) {
        if (val[res[i]]) {
          if (val[res[i]] != res[a[i]]) return 0;
        } else {
          if (vis[res[a[i]]]) return 0;
          val[res[i]] = res[a[i]];
          vis[res[a[i]]] = 1;
          --cnt[bel[res[i]]];
        }
      }
    }
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
    ret = (long long)ret * fac[cnt[i]] % Mod;
  return ret;
}
int main() {
  prepare();
  scanf("%d", &n);
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) scanf("%d", a + i);
  printf("%d\n", calc());
  return 0;
}
