Solve in Python:
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarray of A.
Since the answer may be large, return the answer modulo 10^9 + 7.
 
Example 1:
Input: [3,1,2,4]
Output: 17
Explanation: 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.
 
Note:

1 <= A.length <= 30000
1 <= A[i] <= 30000
class Solution:
    def sumSubarrayMins(self, A: List[int]) -> int:
        n = len(A)
        left = [0] * n
        right = [0] * n
        s1 = []
        s2 = []
        mod = 10 ** 9 + 7
        for i in range(n):
            cnt = 1
            while s1 and s1[-1][0] > A[i]:
                cnt += s1.pop()[1]
            s1.append((A[i], cnt))
            left[i] = cnt
        for i in range(n-1, -1, -1):
            cnt = 1
            while s2 and s2[-1][0] >= A[i]:
                cnt += s2.pop()[1]
            s2.append((A[i], cnt))
            right[i] = cnt
        return sum(A[i] * left[i] * right[i] for i in range(n)) % mod