#include //#include using namespace std; //using namespace atcoder; using ll = long long; #define all(A) A.begin(),A.end() using vll = vector; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) ll mod = 1e9+7; int main() { ll N; cin>>N; ll an=0; vll T(N+1,1); rep(i,N)T[i+1]=(T[i]*3)%mod; ll S=1; rep(i,N-1){ ll A; cin>>A; S*=A; S%=mod; an+=S*2*T[N-i-2]; an%=mod; } ll A; cin>>A; S*=A; an+=S; cout<