Sum of subarray ranges

Write codeHard35 min

Your task

Given an integer array, the range of a contiguous subarray is its maximum element minus its minimum element. Return the sum of the ranges over all contiguous subarrays. The array has at most 1000 elements; values may be negative. The answer fits in a 64-bit signed integer.

Examples

sum_subarray_ranges(arr: list[int]) -> int
Example 1
Inputsum_subarray_ranges([1, 2, 3])
Output4