#include using namespace std; typedef long long ll; const int mod = 1e9 + 7, N = 1e6 + 10; int n; ll a[N],sum[N]; ll pow3[N]; ll qmi(int a,int k) { ll sum = 1; while(k) { if(k & 1) sum = sum * a % mod; a = a * a % mod; k >>= 1; } return sum % mod; } int main() { scanf("%d",&n); for(int i = 0;i < n;i ++ ) scanf("%lld",&a[i]); sum[0] = a[0]; for(int i = 1;i < n;i ++ ) sum[i] = (sum[i - 1] % mod * a[i] % mod) % mod; pow3[0] = 1; for(int i = 1;i <= n;i ++ ) pow3[i] = 3ll * pow3[i - 1] % mod; ll ans = 0; for(int i = 0;i < n;i ++ ) { if(i != n - 1) { ans %= mod; ans += (sum[i] % mod * 2ll % mod * pow3[n - i - 2] % mod) % mod; ans %= mod; } else { ans %= mod; ans += (sum[i] % mod); ans %= mod; } } printf("%lld\n",ans); return 0; }