output_description: Print the answer.
  
  Note that the answer may not fit into a 32-bit integer.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      a_1 a_2 ... a_N
attempt: # -*- coding: utf-8 -*-
  import bisect
  import heapq
  import math
  import random
  import sys
  from collections import Counter, defaultdict, deque
  from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
  from functools import lru_cache, reduce
  from itertools import combinations, combinations_with_replacement, product, permutations
  from operator import add, mul, sub
  
  sys.setrecursionlimit(10000)
  
  
  def read_int():
      return int(input())
  
  
  def read_int_n():
      return list(map(int, input().split()))
  
  
  def read_float():
      return float(input())
  
  
  def read_float_n():
      return list(map(float, input().split()))
  
  
  def read_str():
      return input().strip()
  
  
  def read_str_n():
      return list(map(str, input().split()))
  
  
  def error_print(*args):
      print(*args, file=sys.stderr)
  
  
  def mt(f):
      import time
  
      def wrap(*args, **kwargs):
          s = time.time()
          ret = f(*args, **kwargs)
          e = time.time()
  
          error_print(e - s, "sec")
          return ret
  
      return wrap
  
  
  @mt
  def slv(N, A):
      import numpy as np
  
      A = np.array(A)
  
      def f(A):
          n = len(A)
          if not n:
              return 0
          if n == 1:
              return A[0]
  
          i = np.argmin(A)
          return A[i] * (i + 1) * (n - i) + f(A[:i]) + f(A[i + 1 :])
  
      return f(A)
  
  
  def main():
      N = read_int()
      A = read_int_n()
      print(slv(N, A))
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
  his friend.
  
  Find the following:
  
  ![](https://atcoder.jp/img/agc005/520049e1a049bb9810b398b35d7dcb7f.png)
samples: [{"input": "3\n    2 1 3", "output": "9\n    \n\n* * *"}, {"input": "4\n    1 3 2 4", "output": "19\n    \n\n* * *"}, {"input": "8\n    5 4 8 1 2 6 7 3", "output": "85"}]