from sys import stdin n, *a = map(int, stdin.read().split()) p = 10 ** 9 + 7 pow3 = [1 for i in range(n)] for i in range(1,n): pow3[i] = pow3[i-1] * 3 % p ans = 0 prod = 1 for i in range(n): prod = prod * a[i] % p if i < n - 1: ans = (ans + prod * 2 * pow3[n-i-2] % p) % p else: ans = (ans + prod) % p print("{}".format(ans))