N = int(input()) A = list(map(int, input().split())) md = 1000000007 ret, prod = 0, 1 d23 = 2 * pow(3, md - 2, md) % md for i, a in enumerate(A): ret = ret * 3 % md prod = prod * a % md ret += prod * (d23 if i < N - 1 else 1) % md print(ret % md)