Count the number of inversions