class Solution {
public:
static const int maxm=3e4+5;
static const int mod=1e9+7;
int l[maxm],r[maxm];
int stk[maxm],h;
int sumSubarrayMins(vector<int>& a) {
int n=a.size();
a.insert(a.begin(),(int)-1e9);
a.push_back((int)-1e9);
h=0;
for(int i=1;i<=n+1;i++){
while(h&&a[stk[h]]>a[i]){//这里是>
r[stk[h--]]=i-1;
}
stk[++h]=i;
}
h=0;
for(int i=n;i>=0;i--){
while(h&&a[stk[h]]>=a[i]){//这里是>=
l[stk[h--]]=i+1;
}
stk[++h]=i;
}
int ans=0;
for(int i=1;i<=n;i++){
int lc=i-l[i]+1;
int rc=r[i]-i+1;
int cnt=1ll*lc*rc%mod;
ans=(ans+1ll*cnt*a[i]%mod)%mod;
}
return ans;
}
};