<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:
    def rangeSum(self, nums, n, left, right):
        # MOD = 10**9 + 7

        # def subarrays_less_than_limit(limit):
        #     count = total_sum = window_sum = sum_to_end = 0
        #     start = 0
        #     for end, num in enumerate(nums):
        #         sum_to_end += num * (end - start + 1)
        #         window_sum += num
        #         while window_sum > limit:
        #             sum_to_end -= window_sum
        #             window_sum -= nums[start]
        #             start += 1
        #         count += end - start + 1
        #         total_sum += sum_to_end
        #     return count, total_sum

        # def sum_of_first_k_subarrays(k):
        #     low, high = min(nums), sum(nums)
        #     while low < high:
        #         mid = (low + high) // 2
        #         if subarrays_less_than_limit(mid)[0] < k:
        #             low = mid + 1
        #         else:
        #             high = mid
        #     count, total_sum = subarrays_less_than_limit(low)
        #     # there might be extra subarrays with sum == low
        #     return total_sum - low * (count - k)

        # res = sum_of_first_k_subarrays(right) - sum_of_first_k_subarrays(left - 1)
        # return res % MOD

        MOD = 10 ** 9 + 7
        count = [0] * (sum(nums) + 1)
        for i in range(n):
            cur_sum = 0
            for j in range(i, n):
                cur_sum += nums[j]
                count[cur_sum] += 1
        
        for i in range(1, len(count)):
            count[i] += count[i - 1]
        res = 0
        k = 1  # current subarray sum (1-indexed)
        for cur_sum in range(len(count)):
            while k <= count[cur_sum]:
                if left <= k:
                    res = (res + cur_sum) % MOD
                k += 1
                if k > right:
                    return res
        return res
18687