#include <bits/stdc++.h>
using namespace std;
const int mo = (int)1e9 + 7;
const int maxn = 1000010;
int n;
long long fac[maxn], ans;
int a[maxn], num[maxn], num1[maxn], pos[maxn], mul[maxn];
int pre[maxn], ne[maxn];
vector<int> v[maxn];
inline void init() {
  fac[0] = 1;
  for (int i = 1; i <= n + 2; ++i) fac[i] = fac[i - 1] * i % mo, mul[i] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!v[i].size()) {
      num[n / i]++;
      pos[i] = n / i;
      for (int j = i; j <= n; j += i) v[j].push_back(i), mul[j] *= i;
    }
  }
  for (int i = 1; i <= n; ++i) num1[mul[i]]++;
  num[1]++;
  pos[1] = 1;
  v[1].push_back(1);
}
int main() {
  scanf("%d", &n);
  init();
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i)
    if (a[i]) {
      num1[mul[a[i]]]--;
      if (v[a[i]].size() != v[i].size()) return puts("0"), 0;
      for (int j = 0; j < v[i].size() - 1; j++)
        if (v[a[i]][j] != v[i][j]) return puts("0"), 0;
      long long x = v[a[i]].back(), y = v[i].back();
      if (pos[x] != pos[y]) return puts("0"), 0;
      if (pre[x] && pre[x] != y) return puts("0"), 0;
      if (ne[y] && ne[y] != x) return puts("0"), 0;
      if (!pre[x] && !ne[y]) num[pos[x]]--;
      pre[x] = y;
      ne[y] = x;
    }
  ans = 1;
  for (int i = 1; i <= n; ++i)
    ans = ans * fac[num[i]] % mo, ans = ans * fac[num1[i]] % mo;
  printf("%lld\n", ans);
  return 0;
}
