human
Can you provide a Python program to find the sum of min(B), where B ranges over every (contiguous) subarray of A given the following:
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
assistant
#Here is Python program to find the sum of min(B), where B ranges over every (contiguous) subarray of A.

class Solution:
    def sumSubarrayMins(self, A: List[int]) -> int:
        mod = 10 ** 9 + 7
        stack = []
        ans = dot = 0
        for idx, val in enumerate(A):
            count = 1
            while stack and stack[-1][0] >= val:
                x,c = stack.pop()
                count += c
                dot -= x*c
            
            stack.append((val,count))
            dot += val * count
            ans += dot
        return ans % mod