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