using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static Int64 max(Int64[] A, Int64 i, Int64 j) { Int64 max = -1; for (Int64 k = i; k <= j; ++k) if (A[k] > max) max = A[k]; return max; } static Int64[] S(Int64[] A) { Int64[] res = new Int64[((A.LongLength + 1) * A.LongLength) / 2]; Int64 k = 0; for (Int64 i = 0; i < A.LongLength; ++i) for (Int64 j = 0; j < A.LongLength - i; ++j) res[k++] = max(A, j, i + j); return (res); } static int solve(Int64[] A) { return Convert.ToInt32(S(S(A)).Sum() % 1000000007); } static void Main(String[] args) { Console.ReadLine(); string[] a_temp = Console.ReadLine().Split(' '); Int64[] A = Array.ConvertAll(a_temp, Int64.Parse); var result = solve(A); Console.WriteLine(result); } }