#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int T, n, mmh = 0, a[1100000], V[1100000], S1[1100000], S2[1100000], f[1100000],
          pr[1100000], ne[1100000];
vector<int> v[1100000];
bool bo[1100000];
inline int work() {
  for (int i = 1; i <= n; i++) S1[i] = S2[i] = pr[i] = ne[i] = 0;
  for (int i = 1; i <= n; i++)
    if (S2[V[i]]++, bo[i]) S1[(i == 1 ? 1 : n / i)]++;
  S1[1]++;
  for (int i = 1; i <= n; i++)
    if (a[i]) {
      if (v[i].size() != v[a[i]].size()) return 0;
      for (int j = 0; j < v[i].size() - 1; j++)
        if (v[i][j] != v[a[i]][j]) return 0;
      int x = v[i].back(), y = v[a[i]].back();
      if ((pr[x] && pr[x] != y) || (ne[y] && ne[y] != x) ||
          ((x == 1 ? 1 : n / x) != (y == 1 ? 1 : n / y)))
        return 0;
      if (!pr[x] && !ne[y]) S1[(x == 1 ? 1 : n / x)]--;
      pr[x] = y;
      ne[y] = x;
      S2[V[i]]--;
    }
  int mmh = 1;
  for (int i = 1; i <= n; i++)
    mmh = 1LL * mmh * f[S1[i]] % MOD * f[S2[i]] % MOD;
  return mmh;
}
int main() {
  T = 1;
  n = 1000000;
  f[0] = 1;
  for (int i = 1; i <= n; i++) V[i] = 1, f[i] = 1LL * f[i - 1] * i % MOD;
  for (int i = 2; i <= n; i++)
    if (!v[i].size()) {
      bo[i] = 1;
      for (int j = i; j <= n; j += i) v[j].push_back(i), V[j] *= i;
    }
  v[1].push_back(1);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    printf("%d\n", work());
  }
}
