#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, a[N], h[N], nxt[N], adj[N], fa[N], f[N], ans, t, cn[N], o[N];
long long s[N], d;
inline void dfs(const int x) {
  s[x] = a[x];
  for (register int i = h[x]; i; i = nxt[i]) dfs(adj[i]), s[x] += s[adj[i]];
}
inline void gcd(const long long a, const long long b) {
  return !b ? d = a, void() : gcd(b, a % b);
}
int main() {
  scanf("%d", &n);
  register int i, j;
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
  auto add = [&](const int u, const int v) {
    nxt[++t] = h[u], h[u] = t, adj[t] = v;
  };
  for (i = 2; i <= n; ++i) scanf("%d", &fa[i]), add(fa[i], i);
  dfs(1);
  for (i = 1; i <= n; ++i) gcd(s[1], s[i]), s[1] / d <= n ? ++cn[s[1] / d] : 0;
  for (i = 1; i <= n; ++i)
    for (j = i; j <= n; j += i)
      f[j] += cn[i], f[j] >= 1000000007 ? f[j] -= 1000000007 : 0;
  o[1] = 1;
  for (i = 1; i <= n; ++i)
    if (f[i] == i) {
      for (j = i + i; j <= n; j += i)
        o[j] += o[i], o[j] >= 1000000007 ? o[j] -= 1000000007 : 0;
      ans += o[i], ans >= 1000000007 ? ans -= 1000000007 : 0;
    }
  printf("%d", ans);
  return 0;
}
