Region Separation
Contest: Codeforces Round 511 (Div. 1)
Difficulty rating: 2700
Tags: combinatorics, dp, number theory, trees

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 format:
The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the number of the cities.

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

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

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

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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\}$$$.

Example input:
4
1 1 1 1
1 2 3

Example output:
4

Example input:
4
1 1 1 1
1 2 2

Example output:
2

Editorial:
First, we try to find whether it's possible to seperate the kingdom into $$$k$$$ regions of level-$$$2$$$ with the same sum of values. We calculate $$$S$$$, the sum of all the values, so each region should have sum $$$\frac{S}{k}$$$. Let $$$s_i$$$ be the sum of values in the subtree of root $$$i$$$. We consider all the cities from the leaves to the root, when a subtree $$$i$$$ has sum $$$s_i=\frac{S}{k}$$$, seperating it from the tree. We find that only the $$$i$$$ with $$$s_i \equiv 0 \pmod {\frac{S}{k}})$$$ will be considered. So there should be at least $$$k$$$ cities satisfying this condition. And if there exist such $$$k$$$ cities, we cut the edge to their father (if it is not the root), so the size of each connected part will be multiple of $$$\frac{S}{k}$$$. Since all the values are positive, all the connected parts have size $$$\frac{S}{k}$$$. So we just need to count how many cities $$$i$$$ with $$$s_i \equiv 0 \pmod {\frac{S}{k}}$$$ there are. Let the equation be $$$\frac{s_i}{S}=\frac{x}{k}$$$, where $$$x$$$ is a positive integer, so $$$k$$$ should be a multiple of $$$\frac{S}{\mathrm{gcd}(S,s_i)}$$$. Now we can use a simple dp with $$$O(n \log n)$$$ time to find whether it's possible to seperate the kingdom into $$$k$$$ regions of level-$$$2$$$ for all the $$$k$$$.
And it's not difficult to find that there can be $$$k_i$$$ regions of level-$$$i$$$ if and only if there can be $$$k_i$$$ regions of level-$$$2$$$ and $$$k_{i-1}$$$ is a factor of $$$k_i$$$. Let $$$f_i$$$ be the number of plans with $$$i$$$ regions of the last level, and we enumrate the factors of $$$i$$$ to transform. So the total time of this solution is $$$O(n \ (\log n+\log 10^9))$$$ (including the time to calculate the gcd).