N= int(input()) A = list(map(int,input().split())) mod = 10**9+7 mul = 1 ans = 0 for i in range(N-1): mul=mul*A[i]%mod ans = (ans+mul*pow(3,N-i-2,mod)*2)%mod ans = (ans + mul*A[-1])%mod print(ans)