Problem: There are n cities in the Kingdom of Autumn, numbered from 1 to n. People can travel between any two cities using n-1 two-directional roads.

This year, the government decides to separate the kingdom. There will be regions of different levels. The whole kingdom will be the region of level 1. Each region of i-th level should be separated into several (at least two) regions of i+1-th level, unless i-th level is the last level. Each city should belong to exactly one region of each level and for any two cities in the same region, it should be possible to travel between them passing the cities in the same region only.

According to research, for each city i, there is a value a_i, which describes the importance of this city. All regions of the same level should have an equal sum of city importances.

Your task is to find how many plans there are to determine the separation of the regions that all the conditions are satisfied. Two plans are considered different if and only if their numbers of levels are different or there exist two cities in the same region of one level in one plan but in different regions of this level in the other plan. Since the answer may be very large, output it modulo 10^9+7.

Input

The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of the cities.

The second line contains n integers, the i-th of which is a_i (1 ≤ a_i ≤ 10^9) — the value of each city.

The third line contains n-1 integers, p_1, p_2, …, p_{n-1}; p_i (p_i ≤ i) describes a road between cities p_i and i+1.

Output

Print one integer — the number of different plans modulo 10^9+7.

Examples

Input

4
1 1 1 1
1 2 3


Output

4

Input

4
1 1 1 1
1 2 2


Output

2

Input

4
1 2 1 2
1 1 3


Output

3

Note

For the first example, there are 4 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}.

Plan 3: Level-1: \{1,2,3,4\}, Level-2: \{1\},\{2\},\{3\},\{4\}.

Plan 4: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}, Level-3: \{1\},\{2\},\{3\},\{4\}.

For the second example, there are 2 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1\},\{2\},\{3\},\{4\}.

For the third example, there are 3 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}.

Plan 3: Level-1: \{1,2,3,4\}, Level-2: \{1,3\},\{2\},\{4\}.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class t, class u>
void chmax(t& first, u second) {
  if (first < second) first = second;
}
template <class t, class u>
void chmin(t& first, u second) {
  if (second < first) first = second;
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using pi = pair<ll, ll>;
using vi = vc<ll>;
template <class t, class u>
ostream& operator<<(ostream& os, const pair<t, u>& p) {
  return os << "{" << p.first << "," << p.second << "}";
}
template <class t>
ostream& operator<<(ostream& os, const vc<t>& v) {
  os << "{";
  for (auto e : v) os << e << ",";
  return os << "}";
}
using uint = unsigned;
using ull = unsigned long long;
template <ll i, class T>
void print_tuple(ostream&, const T&) {}
template <ll i, class T, class H, class... Args>
void print_tuple(ostream& os, const T& t) {
  if (i) os << ",";
  os << get<i>(t);
  print_tuple<i + 1, T, Args...>(os, t);
}
template <class... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
  os << "{";
  print_tuple<0, tuple<Args...>, Args...>(os, t);
  return os << "}";
}
void print(ll x, ll suc = 1) {
  cout << x;
  if (suc == 1) cout << endl;
  if (suc == 2) cout << " ";
}
ll read() {
  ll i;
  cin >> i;
  return i;
}
vi readvi(ll n, ll off = 0) {
  vi v(n);
  for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;
  return v;
}
template <class T>
void print(const vector<T>& v, ll suc = 1) {
  for (ll i = ll(0); i < ll(v.size()); i++)
    print(v[i], i == ll(v.size()) - 1 ? suc : 2);
}
string readString() {
  string s;
  cin >> s;
  return s;
}
template <class T>
T sq(const T& t) {
  return t * t;
}
void yes(bool ex = true) {
  cout << "Yes" << endl;
  if (ex) exit(0);
}
void no(bool ex = true) {
  cout << "No" << endl;
  if (ex) exit(0);
}
constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }
const ll infLL = LLONG_MAX / 3;
const ll inf = infLL;
ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
ll popcount(signed t) { return __builtin_popcount(t); }
ll popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(ll i) { return i && (i & -i) == i; }
bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }
template <class t>
void mkuni(vc<t>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
ll rand_int(ll l, ll r) {
  static random_device rd;
  static mt19937 gen(rd());
  return uniform_int_distribution<ll>(l, r)(gen);
}
ll gcd(ll first, ll second) {
  return second ? gcd(second, first % second) : first;
}
ll lcm(ll first, ll second) { return first / gcd(first, second) * second; }
ll extgcd(ll first, ll second, ll& x, ll& y) {
  if (second == 0) {
    x = 1;
    y = 0;
    return first;
  } else {
    ll g = extgcd(second, first % second, y, x);
    y -= first / second * x;
    return g;
  }
}
vi pollard(ll x) {
  vi res;
  for (ll i = 2; i * i <= x; i += (i == 2 ? 1 : 2)) {
    while (x % i == 0) {
      x /= i;
      res.push_back(i);
    }
  }
  if (x > 1) res.push_back(x);
  sort(res.begin(), res.end());
  return res;
}
vc<ll> divs(vc<ll> f) {
  vc<ll> first;
  vi second;
  ll last = -1;
  for (auto v : f) {
    if (last != v) {
      first.push_back(v);
      second.push_back(0);
      last = v;
    }
    second.back()++;
  }
  vi res;
  function<void(ll, ll)> dfs = [&](ll i, ll cur) {
    if (i == ll(first.size())) {
      res.push_back(cur);
      return;
    }
    for (ll j = ll(0); j < ll(second[i] + 1); j++) {
      dfs(i + 1, cur);
      cur *= first[i];
    }
  };
  dfs(0, 1);
  return res;
}
using uint = unsigned;
using ull = unsigned long long;
const uint mod = 1000000007;
struct mint {
  uint v;
  mint(ll vv = 0) { s(vv % mod + mod); }
  mint& s(uint vv) {
    v = vv < mod ? vv : vv - mod;
    return *this;
  }
  mint operator-() const { return mint() - *this; }
  mint& operator+=(const mint& rhs) { return s(v + rhs.v); }
  mint& operator-=(const mint& rhs) { return s(v + mod - rhs.v); }
  mint& operator*=(const mint& rhs) {
    v = ull(v) * rhs.v % mod;
    return *this;
  }
  mint& operator/=(const mint& rhs) { return *this *= rhs.inv(); }
  mint operator+(const mint& rhs) const { return mint(*this) += rhs; }
  mint operator-(const mint& rhs) const { return mint(*this) -= rhs; }
  mint operator*(const mint& rhs) const { return mint(*this) *= rhs; }
  mint operator/(const mint& rhs) const { return mint(*this) /= rhs; }
  mint pow(ll n) const {
    mint res(1), x(*this);
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  mint inv() const { return pow(mod - 2); }
  friend ostream& operator<<(ostream& os, const mint& m) { return os << m.v; }
  bool operator<(const mint& r) const { return v < r.v; }
  bool operator==(const mint& r) const { return v == r.v; }
};
const ll vmax = (1 << 21) + 10;
mint fact[vmax], finv[vmax], invs[vmax];
void initfact() {
  fact[0] = 1;
  for (ll i = ll(1); i < ll(vmax); i++) {
    fact[i] = fact[i - 1] * i;
  }
  finv[vmax - 1] = fact[vmax - 1].inv();
  for (ll i = vmax - 2; i >= 0; i--) {
    finv[i] = finv[i + 1] * (i + 1);
  }
  for (ll i = vmax - 1; i >= 1; i--) {
    invs[i] = finv[i] * fact[i - 1];
  }
}
mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; }
mint binom(ll first, ll second) {
  return fact[first + second] * finv[first] * finv[second];
}
mint catalan(ll n) {
  return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0);
}
const ll nmax = 1000010;
vi tr[nmax];
ll first[nmax];
ll dfs(ll v, vi& dst) {
  ll s = first[v];
  for (auto c : tr[v]) s += dfs(c, dst);
  dst.push_back(s);
  return s;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  ll n;
  cin >> n;
  for (ll i = ll(0); i < ll(n); i++) cin >> first[i];
  ll sum = accumulate(first, first + n, ll(0));
  vi f = pollard(sum);
  vi d = divs(f);
  sort(d.begin(), d.end());
  mkuni(f);
  for (ll i = ll(1); i < ll(n); i++) {
    ll p;
    cin >> p;
    p--;
    tr[p].push_back(i);
  }
  auto idx = [&](ll v) {
    return lower_bound(d.begin(), d.end(), v) - d.begin();
  };
  vi vs;
  dfs(0, vs);
  ll m = d.size();
  vi cnt(m);
  void(0);
  for (auto v : vs) {
    ll i = idx(gcd(sum, v));
    cnt[i]++;
  }
  for (auto v : f) {
    for (ll i = ll(m) - 1; i >= 0; i--)
      if (d[i] % v == 0) {
        cnt[idx(d[i] / v)] += cnt[i];
      }
  }
  ll k = f.size();
  vvc<mint> dp(m, vc<mint>(k));
  dp[0][0] = 1;
  for (ll i = ll(0); i < ll(m); i++) {
    for (ll j = ll(0); j < ll(k - 1); j++) dp[i][j + 1] += dp[i][j];
    if (cnt[i] == sum / d[i]) {
      mint w = dp[i][k - 1];
      if (i == m - 1) cout << w << endl;
      for (ll j = ll(0); j < ll(k); j++) dp[i][j] += w;
    }
    for (ll j = ll(0); j < ll(k); j++) {
      if (sum % (d[i] * f[j]) == 0) dp[idx(d[i] * f[j])][j] += dp[i][j];
    }
  }
}