For each element arr[i], count how many subarrays have arr[i] as their minimum. That count times arr[i] is its contribution to the total sum.
An element is the minimum of a subarray if it's the smallest in that range. Find the nearest smaller element to the left (at index left) and to the right (at index right).
The element arr[i] is the minimum for all subarrays that start anywhere from left + 1 to i and end anywhere from i to right - 1. That's (i - left) × (right - i) subarrays.
Use two monotonic stacks to find left and right boundaries for each element.