C++, O(n) solution.
Intuition\nmonotonic stack for min element before and after, then consider the array with $a[i]$ as minimum, using prefix sum calculate subarray sum. \n$ans=max
# Intuition\nmonotonic stack for min element before and after, then consider the array with $a[i]$ as minimum, using prefix sum calculate subarray sum. \n$ans=max(ans,a[i]*sub\\_array\\_sum)$\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n $$O(n)$$ \n\n# Code\n```\nclass Solution {\npublic:\n    int maxSumMinProduct(vector<int>& nums) \n    {\n        int mod=1e9+7;\n        int n=nums.size();\n        vector<int> prev(n,-1),next(n,n);\n\n        stack<int>st;\n\n        for(int i=0;i<n;i++)\n        {\n            while(st.size() && nums[st.top()]>nums[i])\n            st.pop();\n\n            if(st.empty())\n            prev[i]=-1;\n\n            else \n            prev[i]=st.top();\n\n            st.push(i);\n        }    \n\n        while(st.size())\n        st.pop();\n\n        for(int i=n-1;i>=0;i--)\n        {\n            while(st.size() && nums[st.top()]>=nums[i])\n            st.pop();\n\n            if(st.empty())\n            next[i]=n;\n\n            else \n            next[i]=st.top();\n\n            st.push(i);\n\n        }\n\n        vector<long long int> p(n+2,0);\n        p[0]=nums[0];\n        \n        for(int i=1;i<n;i++)\n        {\n            p[i]+=(nums[i]+p[i-1]);\n            p[i]=p[i];\n        }\n\n        long long int ans=0;\n        for(int i=0;i<n;i++)\n        {\n            long long int u=0;\n            if(prev[i]!=-1)\n            u=(nums[i]*(p[next[i]-1]-p[prev[i]]));\n            else \n            u=(nums[i]*(p[next[i]-1]));\n\n            \n            ans=max(ans,u);\n        }\n        return ans%mod;\n    }\n};\n```
['C++']