<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p>

<p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0 
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1,2], range = 2 - 1 = 1
[2,3], range = 3 - 2 = 1
[1,2,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre>

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

<pre>
<strong>Input:</strong> nums = [1,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[3], range = 3 - 3 = 0
[3], range = 3 - 3 = 0
[1,3], range = 3 - 1 = 2
[3,3], range = 3 - 3 = 0
[1,3,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,-2,-3,4,1]
<strong>Output:</strong> 59
<strong>Explanation:</strong> The sum of all subarray ranges of nums is 59.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>

MOD =10000000007
class Stack:

    def __init__(self):
        self.arr = []

    def push(self, data):
        self.arr.append(data)

    def pop(self):
        if len(self.arr) == 0:
            return -1
        else:
            y = self.arr.pop()
            return y

    def len(self):
        return len(self.arr)

    def top(self):
        if len(self.arr) > 0:
            return self.arr[-1]
        else:
            return -1e9 - 1

    def show(self):
        return self.arr

def pse(n, a):
    # code here

    s = Stack()
    b = n * [0]

    for i in range(n):
        
        while s.len() > 0 and a[s.top()] > a[i]:
            s.pop()
        if s.len() == 0:
            b[i] = -1
            s.push(i)
        else:
            b[i] = s.top()
            s.push(i)
    return b


def nse(n, a):
    # code here

    s = Stack()
    b = n * [0]

    for i in range(n-1,-1,-1):
        while s.len() > 0 and a[s.top()] >= a[i]:
            s.pop()
        if s.len() == 0:
            b[i] = n
            s.push(i)
        else:
            b[i] = s.top()
            s.push(i)

    return b

def pge(n, a):
    # code here

    s = Stack()
    b = n * [0]

    for i in range(n):
        
        while s.len() > 0 and a[s.top()] < a[i]:
            s.pop()
        if s.len() == 0:
            b[i] = -1
            s.push(i)
        else:
            b[i] = s.top()
            s.push(i)
    return b
def nge(n, a):
    # code here

    s = Stack()
    b = n * [0]

    for i in range(n-1,-1,-1):
        
        while s.len() > 0 and a[s.top()] <= a[i]:
            s.pop()
        if s.len() == 0:
            b[i] = n
            s.push(i)
        else:
            b[i] = s.top()
            s.push(i)
    return b


class Solution:
    def subArrayRanges(self, nums: List[int]) -> int:
        arr = nums
        a1 = pse(len(arr),arr)
        a2 = nse(len(arr),arr)
        a3 = pge(len(arr),arr)
        a4 = nge(len(arr),arr)
        #print(a1,a2)
        ans = 0
        for i in range(len(arr)):
            ans =ans + (i-a3[i])*(a4[i]-i)*arr[i]-(i-a1[i])*(a2[i]-i)*arr[i]
        return ans
        
128