#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 2333333, mo = 1e9 + 7;
int n, a[N], f[N], fa[N];
long long size[N], dp[N], tot;
vector<int> e[N];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
void dfs(int u) {
  size[u] = a[u];
  for (register int i = ((int)e[u].size()) - 1; i >= 0; i--)
    dfs(e[u][i]), size[u] += size[e[u][i]];
  long long g = gcd(tot, size[u]), t = tot / g;
  if (t <= n) f[t]++;
}
int main() {
  read(n);
  for (register int i = 1; i <= n; i++) read(a[i]), tot += a[i];
  for (register int i = 2; i <= n; i++) read(fa[i]), e[fa[i]].push_back(i);
  dfs(1);
  for (register int i = n; i >= 1; i--)
    for (int j = i + i; j <= n; j += i) f[j] += f[i];
  dp[1] = 1;
  for (register int i = 1; i <= n; i++)
    if (dp[i])
      for (int j = i + i; j <= n; j += i)
        if (f[j] == j) dp[j] = (dp[j] + dp[i]) % mo;
  long long res = 0;
  for (register int i = 1; i <= n; i++) res = (res + dp[i]) % mo;
  cout << res;
  return 0;
}
