#!/bin/ruby def solve(a) b=[] c=[] for k in 0...a.length for i in 0...a.length-k j=i+k b << a[i..j].max end end for k in 0...b.length for i in 0...b.length-k j=i+k c << b[i..j].max end end return c.inject(:+) # Return the sum of S(S(A)) modulo 10^9+7. end n = gets.strip.to_i a = gets.strip a = a.split(' ').map(&:to_i) result = solve(a) puts result