using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { const int MOD = 1000000007; static int solve(int n, int[] A) { // Return the sum of S(S(A)) modulo 10^9+7. int ret = 0; int[] B = new int[n*(n+1)/2]; int pos = 0; for (int i=0; i < n; i++) B[pos++] = A[i]; int end = n-1; while (end > 0) { for (int i=0; i < end; i++) { B[pos++] = Math.Max(A[i], A[i+1]); } for (int i=0; i < end; i++) { A[i] = B[pos-end+i]; } end--; } //foreach(int x in B) Console.WriteLine(x); int nn = B.Length; int[] C = new int[nn]; end = nn; while (end > 0) { for (int i=0; i < end; i++) ret = (ret + B[i] % MOD); end--; for (int i=0; i < end; i++) { C[i] = Math.Max(B[i], B[i+1]); } for (int i=0; i < end; i++) { B[i] = C[i]; } } return ret; } static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); string[] a_temp = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(a_temp,Int32.Parse); int result = solve(n, a); Console.WriteLine(result); } }