Like the trees ܶ

and ܶ

above, gene trees are modeled as unrooted trees where each internal node (non-leaf node) has degree three. A path-length between two leaf nodes is the sum of the phylogenetic lengths of the edges along the unique path between them. In ܶ

, the path-length between Human and Cow is 2 + 3 = 5 and the path-length between Human and Goldfish is 2 + 4 + 8 + 10 = 24. These lengths indicate that Human is much closer to Cow than to Goldfish genetically. From ܶ

, we can guess that the primate closest to Human is Chimpanzee.
Researchers are interested in measuring the distance between genes in the tree. A famous distance measure is the sum of squared path-lengths of all unordered leaf pairs. More precisely, such a distance ݀d(ܶT) is defined as follows:
%3D%5Csum_%7Bunordered%5C%2Cpair(u%2Cv)%7Dp%5E2_%7Bu%2Cv%7D)
where

is a path-length between two leaf nodes u and v in ܶT. Note that ݀d(ܶT) is the sum of the squared path-lengths

over all unordered leaf pairs u and v in ܶT. For the gene tree ܶ

in Figure B.1, there are six paths over all unordered leaf pairs, (Human, Chimpanzee), (Human, Gorilla), (Human, Orangutan), (Chimpanzee, Gorilla), (Chimpanzee, Orangutan), and (Gorilla, Orangutan). The sum of squared path-lengths is

, so ݀
)
= 111.
Given an unrooted gene tree T, write a program to output ݀d(T).