<p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>

<p><em>Return the sum of the numbers from index </em><code>left</code><em> to index </em><code>right</code> (<strong>indexed from 1</strong>)<em>, inclusive, in the new array. </em>Since the answer can be a huge number return it modulo <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>Output:</strong> 13 
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 3, right = 4
<strong>Output:</strong> 6
<strong>Explanation:</strong> The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 10
<strong>Output:</strong> 50
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
	<li><code>1 &lt;= left &lt;= right &lt;= n * (n + 1) / 2</code></li>
</ul>

class Solution {
public:
    typedef long long ll;
    ll merge_sort(vector<int>& nums, int fr, int to, int k, int &cnt)
    {
        if(fr==to)return 0;
        ll re=0,sum=0;
        int mid=(fr+to)>>1;
        re += merge_sort(nums, fr, mid, k, cnt);
        re += merge_sort(nums, mid+1, to, k, cnt);
        for(int i=fr;i<=mid;i++)sum+=nums[i];
        for(int j=mid+1,i=fr;j<=to;j++){
            while(i<=mid&&nums[j]-nums[i]>k)
                sum-=nums[i++];
            cnt+=(mid-i+1);
            re+=nums[j]*(mid-i+1)-sum;
        }
        inplace_merge(nums.begin()+fr, nums.begin()+mid+1, nums.begin()+to+1);
        return re;
    }
    ll kSum(vector<int> pfx_sum, int n, int k, int mn, int mx)
    {
        int l=mn,r=mx,mid, cnt;
        ll re_sum,re_cnt;
        while(l<=r){
            vector<int> tmp(pfx_sum);
            mid=(l+r)>>1;
            cnt=0;
            ll sum=merge_sort(tmp, 0, n, mid, cnt);
            if(cnt>=k) // mid is too large
                r=mid-1,re_sum=sum,re_cnt=cnt;
            else //mid is too little
                l=mid+1;
        }
        return re_sum - (l * (re_cnt-k));
    }
    
    int rangeSum(vector<int>& nums, int n, int left, int right) {
        vector<int> pfx_sum;
        pfx_sum.resize(n+1);
        pfx_sum[0]=0;
        int mn=INT_MAX,mx;
        for(int i=0;i<n;i++){
            mn=min(mn, nums[i]);
            pfx_sum[i+1]=pfx_sum[i]+nums[i];
        }
        mx=pfx_sum.back();
        return fmod(kSum(pfx_sum, n, right, mn, mx)-kSum(pfx_sum, n, left-1, mn, mx),1e9+7);
    }
};
25215