using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int InversePairs (List<int> nums) {
        List<int> ectype = new List<int>(nums.Count);
        for (int i = 0; i < nums.Count; i++) ectype.Add(0);
        // Initialize ectype with zeros.

        List<int> numsCopy = new List<int>(nums);
        return SortMerge(0, nums.Count - 1, ectype, numsCopy);
    }

    public int SortMerge(int i, int j, List<int> ectype, List<int> nums) {
        if (i >= j) return 0;

        int mid = (i + j) >> 1;
        int res = SortMerge(i, mid, ectype, nums) + SortMerge(mid + 1, j, ectype, nums);

        int left = i;
        int right = mid + 1;

        for (int k = i; k <= j; k++) {
            ectype[k] = nums[k];
        }

        for (int k = i; k <= j; k++) {
            if (left == mid + 1)
                nums[k] = ectype[right++];
            else if (right == j + 1 || ectype[left] <= ectype[right])
                nums[k] = ectype[left++];
            else {
                nums[k] = ectype[right++];
                res += mid + 1 - left;
                res = res%1000000007;
            }
        }

        return res;
    }
}