#!/bin/python3 import sys A_global=[] iList=[] jList=[] tempList=[] finalList=[] def initList(size): global iList,jList for i in range (0,size): iList.append(i) jList.append(i) def max(i,j,A): max_index=i for k in range(i,j+1): if(A[k]>A[max_index]): max_index=k return max_index def F(B): global tempList,iList,jList size=len(B) iList[:]=[] jList[:]=[] initList(size) temp_size=size iptr=0 jptr=0 for i in range(0,int(size*(size+1)/2)): """if(max(iList[iptr],jList[jptr],A)==iptr): finalList.append(A[iList[iptr]]) else: finalList.append(A[jptr])""" finalList.append(B[max(iList[iptr],jList[jptr],B)]) iptr=iptr+1 jptr=jptr+1 if(iptr==temp_size): del iList[temp_size-1] del jList[0] temp_size=temp_size-1 iptr=0 jptr=0 def S(A): global tempList,iList,jList size=len(A) initList(size) temp_size=size iptr=0 jptr=0 for i in range(0,int(n*(n+1)/2)): """if(max(iList[iptr],jList[jptr],A)==iptr): tempList.append(A[iList[iptr]]) else: tempList.append(A[jptr])""" tempList.append(A[max(iList[iptr],jList[jptr],A)]) iptr=iptr+1 jptr=jptr+1 if(iptr==temp_size): del iList[temp_size-1] del jList[0] temp_size=temp_size-1 iptr=0 jptr=0 def solve(A): #Return the sum of S(S(A)) modulo 10^9+7. S(A) B=tempList F(B) value=0 for l in finalList: value=value+l return value if __name__ == "__main__": n = int(input().strip()) A= list(map(int, input().strip().split(' '))) result = solve(A) print(result)