N = int(input()) mod = 10**9+7 ans = 0 now = 1 for i, a in enumerate(map(int, input().split())): now *= a now %= mod if i == N-1: ans += now continue ans += now * pow(3, N-i-2, mod) * 2 ans %= mod print(ans)