An inversion is a pair i < j with a[i] > a[j]. The count measures how unsorted a list is: 0 for sorted, n(n−1)/2 for reversed.
Implement count_inversions(a) in O(n log n) by counting during merge sort: when an element from the right half is merged before elements still waiting in the left half, each of those forms an inversion.
Then use it for kendall_tau_distance(rank_a, rank_b): given two orderings of the same items (lists of item names), the number of item pairs they order differently.