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