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

type Index = Int
type Memo = S.Set Index

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

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

solve :: [Integer] -> Integer
solve l = total result
  where
    n = length l
    l' = sort $ zip l [1..]
    props = Props 0 (S.fromList [0, n + 1])
    result = foldl' step props l'

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