mod = 10**9+7 n, *A = map(int, open(0).read().split()) ans = 0 t = 1 L = [1, 2] for _ in range(n-2): L.append(L[-1] * 3 % mod) for i in range(n): t = t*A[i]%mod ans += t*L[~i] ans %= mod print(ans)