#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int nr[1001000], n, per[1001000], pre, to[1001000], cnt, cont[2][1000100],
    nx[1001000], pr[1001001];
vector<int> vec[1000001];
int main() {
  int i, j, s, p, q;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) nr[i] = 1;
  pre = -1;
  for (i = 2; i <= n; i++) {
    if (vec[i].size() == 0) {
      if (pre < 0 || n / pre != n / i) pre = i;
      if (n / i == 1)
        to[i] = 1;
      else
        to[i] = pre;
      cont[0][to[i]]++;
      for (j = i; j <= n; j += i) {
        vec[j].push_back(i);
        nr[j] *= i;
      }
    }
    cont[1][nr[i]]++;
  }
  vec[1].push_back(1);
  cont[0][1]++;
  cont[1][1]++;
  nr[1] = 1;
  to[1] = 1;
  for (i = 1; i <= n; i++) {
    scanf("%d", &per[i]);
    if (per[i] > 0) {
      if (vec[per[i]].size() != vec[i].size()) {
        puts("0");
        return 0;
      }
      for (j = 0; j < vec[i].size() - 1; j++) {
        if (vec[i][j] != vec[per[i]][j]) {
          puts("0");
          return 0;
        }
      }
      int a = vec[i][vec[i].size() - 1],
          b = vec[per[i]][vec[per[i]].size() - 1];
      if (to[a] != to[b]) {
        puts("0");
        return 0;
      }
      if (nx[a] == 0 && pr[b] == 0) {
        nx[a] = b;
        pr[b] = a;
        cont[0][to[a]]--;
      } else {
        if (nx[a] != b || pr[b] != a) {
          puts("0");
          return 0;
        }
      }
      cont[1][nr[i]]--;
    }
  }
  int ans = 1;
  for (i = 1; i <= n; i++) {
    if (nr[i] == i) {
      for (j = 1; j <= cont[1][i]; j++)
        ans = (int)((long long)ans * (long long)j % mod);
    }
    if (to[i] == i) {
      for (j = 1; j <= cont[0][i]; j++)
        ans = (int)((long long)ans * (long long)j % mod);
    }
  }
  printf("%d\n", ans);
  return 0;
}
