mod = int(1e9+7) n = int(input()) mul = 1 ans = 0 for i,a in enumerate(list(map(int,input().split()))): mul *= a if i == n-1: ans += mul else: ans += mul * 2 * pow(3,n-2-i,mod) ans %= mod print(ans)