How do we know INVERSIONS-COUNT algorithm implemented in balanced tree really works?
At step $i$, the rank of $L[i]$ in $T$ is the number of elements in $L[i],\ldots,L[|L|-1]$ which are at most $L[i]$. Taking into account the $-|L|$ in the beginning, we see that the algorithm counts the number of pairs $i&lt;j$ such that $L[j] \leq L[i]$, which is exactly the number of inversions (assuming all elements are distinct).