Count the number of inversions using modified merge sort.