Path Counting
Contest: Educational Codeforces Round 40 (Rated for Div. 2)
Difficulty rating: 2500
Tags: combinatorics, dp

You are given a rooted tree. Let's denote d(x) as depth of node x: depth of the root is 1, depth of any other node x is d(y) + 1, where y is a parent of x.

The tree has the following property: every node x with d(x) = i has exactly ai children. Maximum possible depth of a node is n, and an = 0.

We define fk as the number of unordered pairs of vertices in the tree such that the number of edges on the simple path between them is equal to k.

Calculate fk modulo 109 + 7 for every 1 ≤ k ≤ 2n - 2.

Input format:
The first line of input contains an integer n (2  ≤  n  ≤  5 000) — the maximum depth of a node.

The second line of input contains n - 1 integers a1,  a2,  ...,  an - 1 (2 ≤  ai  ≤ 109), where ai is the number of children of every node x such that d(x) = i. Since an = 0, it is not given in the input.

Output format:
Print 2n - 2 numbers. The k-th of these numbers must be equal to fk modulo 109 + 7.

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

Note:
This the tree from the first sample:

Example input:
4
2 2 2

Example output:
14 19 20 20 16 16

Example input:
3
2 3

Example output:
8 13 6 9

Editorial:
At first when we read the problem, a simple solution comes to our mind, take a look at the LCA (Lowest Common Ancestor) of that starting and ending vertices of the path and then use combinatorics to calculate the number of the paths, but after trying to implement this or solve this on paper it doesn't seem to be easy at all and it may even be impossible to implement this. So lets try to solve this problem in a different way.
For calculating the answer, we count the number of times each path starts or ends at every vertex, and then divide them by 2 to get the answer for each vertex.
For calculating the answer to the above, it is easy to see that all vertices with the same height have the same number of paths going through them, so if we calculate the number of paths going through one of them and then multiply it by the number of the vertices in that height (let it be ch) it gets equal to our answer.
We can calculate the answer for a certain height. So to do that, we divide the paths into two types, paths that go only into the subtree of a vertex (let's call it dh), and paths that go up (let's call it uh).
For the ones that are in the subtree, it is easy to see if there are at least k other vertices that go down, we can go all paths going down (let the number of them be ph, k), and the answer for this part, equals to:
For the ones that go up, we use dynamic-programming, and we define dph, k the number of paths that start at a vertex with height h and have length k and do not use the leftmost edge exiting the vertex at height h. To update this either we go down on one of the ah - 1 paths and then we go through a path of length k - 1, or we go up and get a path of length k - 1 starting at a vertex from height h - 1, so the answer for this one equals to:
Now $$f_k = \frac{d_k + u_k}{2}$$.
And the final complexity of the solution will be O(n2), but because of the large constant of the solution the time limit is higher.