classSolution{public:constlonglong mod =1e9+7;intsumSubarrayMins(vector<int>& arr){int n = arr.size();
vector<int> monostack;// 找每个数作为最小值能有多少子数组符合条件
vector<int>left(n),right(n);// 以arr[i]为最小值的左右边界长度for(int i =0; i < n; i++){while(!monostack.empty()&& arr[i]<= arr[monostack.back()]){
monostack.pop_back();}
left[i]= i -(monostack.empty()?-1: monostack.back());// 栈顶元素为左边第一个小于arr[i]的元素arr[j]
monostack.push_back(i);}
monostack.clear();for(int i = n-1; i >=0; i--){while(!monostack.empty()&& arr[i]< arr[monostack.back()]){
monostack.pop_back();}
right[i]=(monostack.empty()? n : monostack.back())- i;
monostack.push_back(i);}longlong ans =0;for(int i =0; i < n; i++){
ans =(ans +(longlong)left[i]*right[i]*arr[i])%mod;}return ans;}};