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