def main() -> None: N = int(input()) A = list(map(int, input().split())) res = 0 cuprod = 1 mod = 10**9 + 7 for i in range(N): cuprod *= A[i] cuprod %= mod diff: int if N - 2 - i >= 0: diff = cuprod * 2 * pow(3, N - 2 - i, mod) else: diff = cuprod res += diff res %= mod print(res) if __name__ == '__main__': main()