<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 {
private:
    
    void display(long long arr[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << ' ';
        }
        cout << '\n' << '\n';
    }
public:
    void merge(long long arr[], int start, int last, int mid)
{
    int m = mid - start + 1;
    int n = last - mid;

    int left[m], right[n];

    for (int i = 0; i < m; i++)
    {
        left[i] = arr[start + i];
    }
    for (int j = 0; j < n; j++)
    {
        right[j] = arr[mid + 1 + j];
    }

    int i = 0, j = 0, k = start;

    while ((i < m) && (j < n))
    {
        if (left[i] <= right[j])
        {
            arr[k] = left[i];
            i++;
        }
        else
        {
            arr[k] = right[j];
            j++;
        }
        k++;
    }

    while (i < m)
    {
        arr[k] = left[i];
        i++, k++;
    }
    while (j < n)
    {
        arr[k] = right[j];
        j++, k++;
    }

    // delete[] left;
    // delete[] right;
}
    void mergeSort(long long arr[], int start, int last)
{
    if (start >= last)
    {
        return;
    }
    int mid = start + (last - start) / 2;
    mergeSort(arr, start, mid);
    mergeSort(arr, mid + 1, last);
    merge(arr, start, last, mid);
}
    int rangeSum(vector<int>& nums, int n, int left, int right) {
        long long sums[n * (n + 1) / 2], sum = 0;
        int count = 0;       
        for (int i = n - 1; i >= 0; i--) 
        {
            for (int j = i; j >= 0; j--)
            {
                sum += nums[j];
                sums[count++] = sum;
                // count++;
            }
            sum = 0;
        }
        // sum = 0;
        mergeSort(sums, 0, (n * (n + 1) / 2) - 1);
        // display(sums, n * (n + 1) / 2);
        // if (left == 1) && (right == )
        while (left <= right)
        {
            sum += sums[left++ - 1];
            // left++;
        }
        if (sum > (1e9 + 7))
        {
            return int(sum % ((long long)(1e9 + 7)));
        }
        return int(sum);
    }
};
16526