Tree Array (Codeforces 1541D)
Tags: Math, probabilities

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

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:   

Sample Inputs:
3
1 2
1 3

6
2 1
2 3
6 1
1 4
2 5

5
1 2
1 3
1 4
2 5

Sample Outputs:
166666669

500000009

500000007

Solution (Python):
from sys import stdin, stdout
from collections import deque
from math import gcd
N = 1000000007

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        (g, y, x) = egcd(b % a, a)
        return (g, x - b // a * y, y)

def modinv(a, m=N):
    (g, x, y) = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
n = int(stdin.readline())
n_inv = modinv(n)
two_inv = modinv(2)
probs = [[0] * n for i in range(n)]
probs[1][1] = two_inv
for l in range(2, n):
    probs[l][1] = (two_inv + two_inv * probs[l - 1][1]) % N
for r in range(2, n):
    probs[1][r] = two_inv * probs[1][r - 1] % N
for l in range(2, n):
    for r in range(2, n):
        probs[l][r] = (two_inv * probs[l - 1][r] + two_inv * probs[l][r - 1]) % N
edges = {i: [] for i in range(1, n + 1)}
for _ in range(n - 1):
    (v, w) = [int(x) for x in stdin.readline().split()]
    edges[v].append(w)
    edges[w].append(v)
answer = 0
subtree_size = [0] * (n + 1)
root = [0] * (n + 1)
parent = [0] * (n + 1)
for i in range(1, n + 1):
    subtree_size[i] = {v: 0 for v in edges[i]}
    root[i] = [-1] * (n + 1)
    parent[i] = [-1] * (n + 1)
    q = deque()
    for v in edges[i]:
        subtree_size[i][v] += 1
        root[i][v] = v
        for w in edges[v]:
            if w != i:
                parent[i][w] = v
                q.append(w)
    while len(q) > 0:
        v = q.popleft()
        root[i][v] = root[i][parent[i][v]]
        subtree_size[i][root[i][v]] += 1
        for w in edges[v]:
            if w != parent[i][v]:
                parent[i][w] = v
                q.append(w)
for i in range(1, n):
    for j in range(i + 1, n + 1):
        n2 = 1
        for v in edges[j]:
            if v != root[j][i]:
                n2 += subtree_size[j][v]
        answer = (answer + n2 * n_inv) % N
        path = [j]
        while path[-1] != root[i][j]:
            path.append(parent[i][path[-1]])
        path.append(i)
        for r in range(1, len(path) - 1):
            l = len(path) - 1 - r
            size = 1
            for v in edges[path[r]]:
                if v != path[r - 1] and v != path[r + 1]:
                    size += subtree_size[path[r]][v]
            answer = (answer + size * n_inv * probs[l][r]) % N
stdout.write(str(answer) + '\n')

Solution (Python):
m = 10 ** 9 + 7
n = int(input())
R = range(n)
r = range(-~n)
d = [n * [m] for _ in R]
for _ in R[1:]:
    (a, b) = map(int, input().split())
    d[a - 1][b - 1] = d[b - 1][a - 1] = 1
for i in R:
    d[i][i] = 0
for k in R:
    for i in R:
        for j in R:
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
c = [[1] + n * [0] for _ in r]
for i in R:
    for j in R:
        c[i + 1][j + 1] = (c[i][j + 1] + c[i + 1][j]) * -~m // 2 % m
a = 0
for i in R:
    for j in R[i + 1:]:
        for k in R:
            (x, y) = (d[i][k], d[j][k])
            v = (x + y - d[i][j]) // 2
            a += c[x - v][y - v]
print(a * pow(n, m - 2, m) % m)

Solution (Python):
m = 10 ** 9 + 7
n = int(input())
R = range(n)
r = range(-~n)
d = [n * [m] for _ in R]
for _ in R[1:]:
    (a, b) = map(int, input().split())
    d[a - 1][b - 1] = d[b - 1][a - 1] = 1
for i in R:
    d[i][i] = 0
for k in R:
    for i in R:
        for j in R:
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])
c = [[1] + n * [0] for _ in r]
for i in r[1:]:
    for j in r[1:]:
        c[i][j] = (c[i - 1][j] + c[i][j - 1]) * -~m // 2 % m
a = 0
for i in R:
    for j in R[i + 1:]:
        for k in R:
            (x, y) = (d[i][k], d[j][k])
            v = (x + y - d[i][j]) // 2
            a += c[x - v][y - v]
print(a * pow(n, m - 2, m) % m)