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

main = do
  n <- readLn
  a <- map read . words <$> getLine
  let xs = reverse $ sort $ zip a [1..]
      f s x (Just i) (Just j) = x*(r-j)*((i+1)-(l+1))
        where
          l = fromMaybe 0 $ S.lookupLT i s
          r = fromMaybe (n+1) $ S.lookupGT j s
      f _ _ _ _ = 0
      g _ [] = 0
      g s ((x,i):xs) = f s x l (Just i) + f s x (Just i) r + g t xs
        where 
          l = S.lookupLT i s
          r = S.lookupGT i s
          t = S.insert i s
  print $ g S.empty xs