import Foundation let n = Int(readLine()!)! var inputArray = readLine()!.split(separator: " ").map { Int($0)! } func S(_ A: [Int]) -> [Int] { var B = [Int]() for k in 0 ..< A.count { for i in 0 ..< A.count - k { let j = i + k let section = A[i...j] // Add max(section) to B var localMax = Int.min for number in section { if number > localMax { localMax = number } } B.append(localMax % 1_000_000_000_000) } } return B } func solve(_ A: [Int]) -> Int { let rawResult = S(S(A)) var sum = 0 for number in rawResult { sum += number sum = sum % 1_000_000_000_000 } return sum % (1_000_000_000 + 7) } print(solve(inputArray))