#include <bits/stdc++.h>
using namespace std;
const int mo = 1000000007;
const int N = 1000005;
int n, a[N], bas[N];
int cnt[N], cntb[N];
int matl[N], matr[N];
vector<int> di[N];
void GG() {
  puts("0");
  exit(0);
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
  for (int i = (int)(1); i <= (int)(n); i++) bas[i] = 1;
  for (int i = (int)(2); i <= (int)(n); i++)
    if (bas[i] == 1) {
      ++cnt[n / i];
      for (int j = (int)(1); j <= (int)(n / i); j++) {
        bas[i * j] *= i;
        di[i * j].push_back(i);
      }
    }
  di[1].push_back(n);
  ++cnt[1];
  for (int i = (int)(1); i <= (int)(n); i++) ++cntb[bas[i]];
  for (int i = (int)(1); i <= (int)(n); i++)
    if (a[i]) {
      if (di[i].size() != di[a[i]].size()) GG();
      for (int j = (int)(0); j <= (int)(di[i].size() - 1); j++) {
        int v1 = di[i][j], v2 = di[a[i]][j];
        if (n / v1 != n / v2) GG();
        if (matl[v1] && matl[v1] != v2) GG();
        if (matr[v2] && matr[v2] != v1) GG();
        if (!matr[v2]) matl[v1] = v2, matr[v2] = v1, cnt[n / v1]--;
      }
      cntb[bas[i]]--;
    }
  int ans = 1;
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(1); j <= (int)(cnt[i]); j++) ans = 1ll * ans * j % mo;
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(1); j <= (int)(cntb[i]); j++) ans = 1ll * ans * j % mo;
  printf("%d\n", ans);
}
