class Solution:
def reversePairs(self, nums: List[int]) -> int:
A = list(set(nums))
A.sort()
m = len(A)
C = [0] * m
mapping = {n: i + 1 for i, n in enumerate(A)}
res = []
for i in range(len(nums) - 1, -1, -1):
num = nums[i]
c_index = mapping[num]
while c_index <= m:
C[c_index - 1] += 1
c_index += c_index & (-c_index)
t_res = 0
c_index = mapping[num] - 1
while c_index > 0:
t_res += C[c_index - 1]
c_index -= c_index & (-c_index)
res.append(t_res)
return sum(res)