import sys input = sys.stdin.buffer.readline def main(): N = int(input()); MOD = pow(10,9) + 7 A = list(map(int,input().split())) fac = 2*pow(3,N-2,MOD) inv3 = pow(3,MOD-2,MOD) ans = 0 a = 1 for i in range(N-1): a = a*A[i]%MOD #print(a,fac) ans += a*fac ans %= MOD #facの更新 fac = fac*inv3%MOD #最後はfac無し ans += a*A[N-1]%MOD print(ans%MOD) if __name__ == '__main__': main()