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;
long long n, i, j, k, m, q, timer, mx, v, tot, x, y, INF, sz, ans, sum;
long long a[1005000], dp[1005000], b[1050000];
vector<int> g[1005000];
vector<long long> divs;
const int N = 32000000;
int lp[N + 15];
vector<int> pr;
vector<long long> cur_primes, all, good;
struct Hashset {
  long long keys[300000];
  int values[300000];
  void update(long long key, int value) {
    int hsh = (key & 262143);
    while (keys[hsh] != -1 && keys[hsh] != key) {
      hsh++;
    }
    if (keys[hsh] == -1)
      keys[hsh] = key, values[hsh] = value;
    else {
      values[hsh] += value;
      if (values[hsh] >= 1000000007) values[hsh] -= 1000000007;
    }
  }
  int get(long long key) {
    int hsh = (key & 262143);
    while (keys[hsh] != -1 && keys[hsh] != key) {
      hsh++;
    }
    if (keys[hsh] == -1) return 0;
    return values[hsh];
  }
} cnt, tot_cnt, total_dp;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
void dfs(int v, int p = -1) {
  dp[v] = a[v];
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (to != p) {
      dfs(to, v);
      dp[v] += dp[to];
    }
  }
  cnt.update(gcd(dp[v], sum), 1);
}
void go(long long v, long long pos) {
  for (int i = pos; i < cur_primes.size(); i++) {
    long long u = cur_primes[i];
    if (v % u == 0) {
      go(v / u, i);
    }
  }
  all.push_back(v);
}
int main() {
  for (int i = 0; i < 300000; i++)
    cnt.keys[i] = tot_cnt.keys[i] = total_dp.keys[i] = -1;
  for (int i = 2; i <= N; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j)
      lp[i * pr[j]] = pr[j];
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    sum += a[i];
  }
  for (int i = 2; i <= n; i++) {
    int x, y;
    scanf("%d", &x);
    g[i].push_back(x);
    g[x].push_back(i);
  }
  dfs(1);
  long long x = sum;
  for (int i = 0; i < pr.size(); i++) {
    if (x % pr[i] == 0) {
      cur_primes.push_back(pr[i]);
      while (x % pr[i] == 0) x /= pr[i];
    }
  }
  if (x != 1) cur_primes.push_back(x);
  go(sum, 0);
  sort(all.begin(), all.end());
  reverse(all.begin(), all.end());
  for (int j = 0; j < all.size(); j++) {
    sz = 0;
    long long v = all[j];
    for (int i = 0; i < cur_primes.size(); i++)
      if (sum % (v * cur_primes[i]) == 0) {
        b[sz++] = cur_primes[i];
      }
    long long ans = 0;
    for (int i = 1; i < (1 << sz); i++) {
      int bits = 0;
      long long mul = 1;
      for (int k = 0; k < sz; k++)
        if ((i >> k) & 1) {
          bits++;
          mul *= b[k];
        }
      if (bits % 2)
        ans += tot_cnt.get(v * mul);
      else
        ans -= tot_cnt.get(v * mul);
    }
    tot_cnt.update(v, cnt.get(v) + ans);
  }
  for (int i = 0; i < all.size(); i++) {
    long long v = all[i];
    if (sum / v == tot_cnt.get(v)) {
      total_dp.update(v, 1);
      good.push_back(v);
    }
  }
  sort(good.begin(), good.end());
  for (int i = 0; i < good.size(); i++) {
    for (int j = i + 1; j < good.size(); j++)
      if (good[j] % good[i] == 0) {
        total_dp.update(good[j], total_dp.get(good[i]));
      }
  }
  cout << total_dp.get(sum) << endl;
  return 0;
}