/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	
	public static int N;
	public static int[] leftSmaller(int[] arr) {
        int n = arr.length;
        int[] left = new int[n];
        Stack<Integer> st = new Stack<>();

        for (int i = 0; i < n; i++) {

            while (!st.isEmpty() && arr[st.peek()] > arr[i]) {
                st.pop();
            }

            left[i] = st.isEmpty() ? -1 : st.peek();

            st.push(i);
        }

        return left;
    }

    public static int[] rightSmaller(int[] arr) {
        int n = arr.length;
        int[] right = new int[n];
        Stack<Integer> st = new Stack<>();

        for (int i = n - 1; i >= 0; i--) {

            while (!st.isEmpty() && arr[st.peek()] > arr[i]) {
                st.pop();
            }

            right[i] = st.isEmpty() ? N : st.peek();

            st.push(i);
        }

        return right;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		int[] arr = {1, 8, 7, 6, 3, 4, 5, 2};
		N = arr.length;
		int[] contribution = new int[N];
		
		int[] left_smaller = leftSmaller(arr);
		int[] right_smaller = rightSmaller(arr);
		
		int ans = 0;
		
		for(int i = 0; i < N; i++){
			int i1 = left_smaller[i];
			int j1 = right_smaller[i];
			i1++;
			j1--;
			
			int x = Math.abs(i-i1);
			int y = Math.abs(i-j1);
			
			contribution[i] = (x+1)*(y+1);
			
			ans += contribution[i] * arr[i];
			
		}
		System.out.print(ans);
	}
}