import sys input = lambda: sys.stdin.readline().rstrip() mod = 10**9 + 7 # ----------------------- # n = int(input()) A = list(map(int, input().split())) ans = 0 mul = 1 for i,a in enumerate(A): mul *= a mul %= mod if i == n-1: continue ans += mul * 2 * pow(3, max(0,n-i-2), mod) ans %= mod ans += mul ans %= mod print(ans)