This is a modified merge sort where you count inversions during the merge step. The key insight: when merging two sorted halves, if an element from the left half is placed after elements from the right half, those right elements are smaller and to its right.
Instead of sorting values directly, sort indices paired with values.
During merge, when placing a left element, count how many right elements were already placed.
That count represents smaller elements to the right.