Tree Array
Contest: Codeforces Round 728 (Div. 1)
Difficulty rating: 2300
Tags: brute force, combinatorics, dp, graphs, math, probabilities, trees

You are given a tree consisting of $$$n$$$ nodes. You generate an array from the tree by marking nodes one by one.

Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree.

After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node.

It can be shown that the process marks all nodes in the tree.

The final array $$$a$$$ is the list of the nodes' labels in order of the time each node was marked.

Find the expected number of inversions in the array that is generated by the tree and the aforementioned process.

The number of inversions in an array $$$a$$$ is the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i > a_j$$$. For example, the array $$$[4, 1, 3, 2]$$$ contains $$$4$$$ inversions: $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(3, 4)$$$.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 200$$$) — the number of nodes in the tree.

The next $$$n - 1$$$ lines each contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le n$$$; $$$x \neq y$$$), denoting an edge between node $$$x$$$ and $$$y$$$.

It's guaranteed that the given edges form a tree.

Output format:
Output the expected number of inversions in the generated array modulo $$$10^9+7$$$.

Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

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

Note:
This is the tree from the first sample:

For the first sample, the arrays are almost fixed. If node $$$2$$$ is chosen initially, then the only possible array is $$$[2, 1, 3]$$$ ($$$1$$$ inversion). If node $$$3$$$ is chosen initially, then the only possible array is $$$[3, 1, 2]$$$ ($$$2$$$ inversions). If node $$$1$$$ is chosen initially, the arrays $$$[1, 2, 3]$$$ ($$$0$$$ inversions) and $$$[1, 3, 2]$$$ ($$$1$$$ inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is $$$\frac{1}{3}\cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot (\frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 1) = \frac{7}{6}$$$.

$$$166666669 \cdot 6 = 7 \pmod {10^9 + 7}$$$, so the answer is $$$166666669$$$.

This is the tree from the second sample:

This is the tree from the third sample:

Example input:
3
1 2
1 3

Example output:
166666669

Example input:
6
2 1
2 3
6 1
1 4
2 5

Example output:
500000009

Editorial:
Parsing through the problem statement, the process can be seen as choosing a starting node and "expanding" the subtree of marked nodes to nodes adjacent to the marked component.
Fixing a given root $$$r$$$, the expected value of the entire process is obviously the sum of the expected values for a fixed root divided by $$$n$$$.
Find the contribution of the inversion of two nodes $$$(a, b)$$$ where $$$a<b$$$. The expected contribution for any pair $$$(a, b)$$$ is equal to the probability that $$$b$$$ appears before $$$a$$$ with a given root.
Set $$$l = lca(a, b)$$$. Note that, until reaching $$$l$$$, every possible process still has the same probability of reaching $$$b$$$ before $$$a$$$ as it did when the first node was chosen. Therefore, we can assume that the process has reached $$$l$$$ and calculate the probability from there.
Once $$$l$$$ is reached, we now note that the probability that the process "gets closer" to $$$b$$$ is always equal to the probability of getting closer to $$$a$$$.
The problem can be rephrased as having two stacks of size $$$dist(l, a)$$$ and $$$dist(l, b)$$$ with an arbitrary $$$p$$$ to remove a node from one of the two stack (and $$$1-2p$$$ to nothing) and finding the probability that $$$dist(l, b)$$$ reaches zero before $$$dist(l, a)$$$.
However, it turns out that the actual probability $$$p$$$ does not matter. We propose a function $$$F[x][y]$$$ that defines the probability that a stack of size $$$y$$$ becomes $$$0$$$ before a stack of size $$$x$$$. In fact a function exists and it is defined as $$$F[x][y] = \frac{F[x-1][y]+F[x][y-1]}{2}$$$. Intuitively, this is because the probability of decreasing $$$x$$$ or decreasing $$$y$$$ is always the same, so the probability of transitioning the state we end up transitioning to is always the same, regardless of $$$p$$$.
So, the solution is clear. Iterate over and root at all nodes. Then at the given root, iterate over all pairs of node $$$a < b$$$ and add $$$F[dist(l, a)][dist(l, b)]$$$ to the answer. Finally, divide by $$$n$$$. In total, the solution works in $$$O(N^3 \log N)$$$ or $$$O(N^3)$$$.