<p>The <strong>min-product</strong> of an array is equal to the <strong>minimum value</strong> in the array <strong>multiplied by</strong> the array&#39;s <strong>sum</strong>.</p>

<ul>
	<li>For example, the array <code>[3,2,5]</code> (minimum value is <code>2</code>) has a min-product of <code>2 * (3+2+5) = 2 * 10 = 20</code>.</li>
</ul>

<p>Given an array of integers <code>nums</code>, return <em>the <strong>maximum min-product</strong> of any <strong>non-empty subarray</strong> of </em><code>nums</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>Note that the min-product should be maximized <strong>before</strong> performing the modulo operation. Testcases are generated such that the maximum min-product <strong>without</strong> modulo will fit in a <strong>64-bit signed integer</strong>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,<u>2,3,2</u>]
<strong>Output:</strong> 14
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
2 * (2+3+2) = 2 * 7 = 14.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,<u>3,3</u>,1,2]
<strong>Output:</strong> 18
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
3 * (3+3) = 3 * 6 = 18.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,<u>5,6,4</u>,2]
<strong>Output:</strong> 60
<strong>Explanation:</strong> The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
4 * (5+6+4) = 4 * 15 = 60.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
</ul>

"""
The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.

For example, the array [3,2,5] (minimum value is 2) has a min-product of 2 * (3+2+5) = 2 * 10 = 20.
Given an array of integers nums, return the maximum min-product of any non-empty subarray of nums. Since the answer may be large, return it modulo 10**9 + 7.

Note that the min-product should be maximized before performing the modulo operation. Testcases are generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.

A subarray is a contiguous part of an array.
"""

class Solution:
    def maxSumMinProduct(self, nums: List[int]) -> int:
        # n <= 10^6
        # nums[i] can be any positive integer

        # sum=14, min=2, score = 28
        # arr[l] = x
        # sum=12, min=3, score=36
        # [2, 3, 4, 5]

        # left[i] is the rightmost index j such that arr[j] < arr[i]
        n = len(nums)
        left = [0] * n
        right = [0] * n

        # (x, i)
        # increasing mono stack
        stack = [(float("-inf"), -1)]

        for i,x in enumerate(nums):
            while x <= stack[-1][0]:
                y,j = stack.pop()
                right[j] = i

            # [1,2,2,2,2,2,2,5,3]
            # (-inf, -1)

            left[i] = stack[-1][1]
            stack.append((x, i))
        
        while stack:
            y,j = stack.pop()
            right[j] = n

        prefix = [0]
        for x in nums:
            prefix.append(prefix[-1] + x)
        # prefix[i+1] stores the sum of arr[0...i]
        # print(prefix)
        
        
        ans = 0

        for i,x in enumerate(nums):
            l = left[i]+1
            r = right[i]-1
            # print(f"x={x} l={l} r={r} sum={prefix[r+1] - prefix[l]}")
            ans = max(ans, x * (prefix[r+1] - prefix[l]))
        
        # print(left)
        # print(right)

        return ans % (10**9+7)



32500