<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>

import numpy as np
class Solution:
    def rangeSum(self, nums, n, left, right):
        # iterating through all subarrays
        nums = np.array(nums)
        sum_nums = np.zeros(n * (n + 1) // 2, dtype=np.int64)
        
        # first traverse once through the nums array
        sum_nums[0] = nums[0]
        for i in range(1, n):
            sum_nums[i] = sum_nums[i-1] + nums[i]

        #sum_nums[n:2 * n - 1] = sum_nums[1:n] - sum_nums[0]
        #sum_nums[2 * n: 3 * n - 2] = sum_nums[n + 1:2 * n - 1] - sum_nums[n]

        for i in range(1, n):
            # erst 4 bis 7      diff 3          n - 1       4 = n
            # dann 8 bis 10     diff 2          n - 2       8 = 2n
            # dann 11 bis 12    diff 1          n - 3       11 = 3n -1

            # Bei 5 wäre es...
            # erst 6 bis 11         6 = n + 1       (i + 1) * (n - i/2)     i = 0 falsch..
            # dann 12 bis 16        12 = 2n                                 i = 1 - > 10 falsch
            # dann 17 bis 20        17 = 3n -1      (i + 1) * n  - i * (i + 1) / 2
            # dann 21 bis 23        21 = 4n - 3
            # dann 24 bis 25        24 = 5n - 6
            to_fill = (i + 1) * n  - i * (i + 1) // 2         # = 11 für i = 1
            filled = to_fill - (n - i)
            #print(to_fill)
            #print(filled)
            sum_nums[filled:to_fill] = sum_nums[filled - (n-i):filled] - nums[i-1]

        #print(sum_nums)
        sum_nums.sort()
        result = np.sum(sum_nums[left-1:right])

        return (result % (int(1e9) + 7))
29382