<p>Given an array of integers arr, find the sum of <code>min(b)</code>, where <code>b</code> ranges over every (contiguous) subarray of <code>arr</code>. Since the answer may be large, return the answer <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> arr = [3,1,2,4]
<strong>Output:</strong> 17
<strong>Explanation:</strong> 
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Sum is 17.
</pre>

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

<pre>
<strong>Input:</strong> arr = [11,81,94,43,3]
<strong>Output:</strong> 444
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= 3 * 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int sumSubarrayMins(vector<int>& arr) {
        return subarrayaux(arr, 0, arr.size()) % 1000000007;
    }

    int subarrayaux(vector<int>& arr, int start, int end) {
        if (end <= start)
            return 0;

        int min_value = arr[start];
        int min_idx = start;
        for (int i = start + 1; i < end; i++) {
            if (arr[i] < min_value) {
                min_value = arr[i];
                min_idx = i;
            }
        }

        long long result = min_value % 1000000007;
        result = (result * (min_idx - start + 1)) % 1000000007;
        result = (result * (end - min_idx)) % 1000000007;

        // Adding the results from recursive calls with modulo
        long long left = subarrayaux(arr, start, min_idx) % 1000000007;
        long long right = subarrayaux(arr, min_idx + 1, end) % 1000000007;

        return (result + left + right) % 1000000007;
    }
};

43300