#include <bits/stdc++.h>
using namespace std;
long long n, i, j, x, xx, yy, p = 1e9 + 7, pr[1000010], nt[1000010],
                              jc[1000010], pos[1000010], mu[1000010],
                              a[1000010], num[1000010], ans;
vector<long long> v[1000010];
int main() {
  scanf("%lld", &n);
  jc[0] = 1;
  for (i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % p;
  for (i = 1; i <= n; i++) mu[i] = 1;
  for (i = 2; i <= n; i++)
    if (!v[i].size()) {
      num[n / i]++;
      pos[i] = n / i;
      for (j = i; j <= n; j += i) v[j].push_back(i), mu[j] *= i;
    }
  for (i = 1; i <= n; i++) a[mu[i]]++;
  num[1]++;
  pos[1] = 1;
  v[1].push_back(1);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &x);
    if (!x) continue;
    a[mu[x]]--;
    if (v[x].size() != v[i].size()) return puts("0"), 0;
    for (j = 0; j < v[i].size() - 1; j++)
      if (v[x][j] != v[i][j]) return puts("0"), 0;
    xx = v[x].back();
    yy = v[i].back();
    if (pos[xx] != pos[yy]) return puts("0"), 0;
    if (pr[xx] && pr[xx] != yy) return puts("0"), 0;
    if (nt[yy] && nt[yy] != xx) return puts("0"), 0;
    if (!pr[xx] && !nt[yy]) num[pos[xx]]--;
    pr[xx] = yy;
    nt[yy] = xx;
  }
  ans = 1;
  for (i = 1; i <= n; i++) ans = ans * jc[a[i]] % p;
  for (i = 1; i <= n; i++) ans = ans * jc[num[i]] % p;
  printf("%lld", ans);
}
