import Data.List
import Data.Maybe
import qualified Data.IntSet as S

type Index = Int
type Memo = S.IntSet

-- data Props = Props { total :: Int, memo :: Memo } deriving Show

step :: (Int, Memo) -> (Int, Index) -> (Int, Memo)
step (total, memo) (x, i) = -- traceShow (props, x, i)
  (total', memo')
  where
    h = fromJust (S.lookupGT i memo) - i
    w = i - fromJust (S.lookupLT i memo)
    total' = total + x * h * w
    memo' = S.insert i memo

solve :: [Int] -> Int
solve l = total
  where
    n = length l
    l' = sort $ zip l [1..]
    init = (0, (S.fromList [0, n + 1]))
    (total, memo) = foldl' step init l'

main = getContents >>= print . solve . map read . words . last . lines
