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