#include #include using namespace std; using namespace atcoder; using ll=long long; using ld=long double; using vvi=vector>; using vi=vector; using vpi=vector>; using vvl=vector>; using vl=vector; using vpl=vector>; using vb=vector; using vvb=vector; using mint=modint1000000007; using mint9=modint998244353; using P=pair; using Pi=pair; #define outs cout << " "; #define outl cout << endl; ll mod1=1000000007,mod2=998244353; ll intmax=2147483647,llmax=9223372036854775807; #define rep(i, s, n) for (ll i = s; i < (ll)(n); i++) #define dep(i, s, n) for (ll i = n-1; i >=(ll)(s); i--) #define rem(i, s, n, t) for(ll i = s; i < (ll)(n); t, i++) #define dem(i, s, n, t) for(ll i = n-1; i >=(ll)(s); t, i--) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define nr cout << "No" << endl;return 0; #define print(a) cout << a << " "; #define np next_permutation #define fi first #define se second #define sor(a) sort(all(a)) #define sorr(a) sort(allr(a)) #define grl greater #define gri greater #define sizel(a) ll(a.size()) #define pb push_back #define alout(a) {rep(i,0,a.size())cout << a[i] << " ";outl;} #define chmax(a,b) a=max(a,b); #define chmin(a,b) a=min(a,b); void fix(ll a){cout << fixed << setprecision(a);} void yn(bool a){if(a){cout<<"Yes"< s){cout << s.fi << " " << s.se << endl;} void pout(pair s){cout << s.fi << " " << s.se << endl;} ll keta(ll i,ll j){i=i%ll(pow(10,j))/ll(pow(10,j-1));return i;}// j = 0 は ダメ ll pm1(ll x,ll y){ll s=1;for(ll i=0;i0;)if(a96)s[i]=s[i]-32; return s; } ll MAX,mod=mod1; vl fac,finv,inv; void COMinit(){ MAX++; fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; rep(i,2,MAX){ fac[i]=fac[i-1]*i%mod; inv[i]=mod-inv[mod%i]*(mod/i)%mod; finv[i]=finv[i-1]*inv[i]%mod; } } ll com(ll n,ll k){ if(nb) ll l=0,r=a.size()-1; while(l+1-1;i--){ n+=s[i]*pow(10,i); } return n; } ll syou(ll n){ vi s; while(n){ s.push_back(n%10); n/=10; } sort(all(s)); rep(i,0,s.size()){ n+=s[i]*pow(10,s.size()-i-1); } return n; } ll yakusuu(ll a){ ll co=0; for(ll i=1;i*i>a.fi>>a.se; } bool prime(ll a){ rep(i,2,sqrt(a)+1)if(a%i==0)return 0; return 1; } ll LIS(vl a){ vl ret; for(ll u:a){ auto itr=lower_bound(all(ret),u); if(itr!=ret.end())*itr=u; else ret.push_back(u); } return ll(ret.size()); } // priority_queue greater ll sl=1,ls=0,inf=1e18; // segtree l~r-1 P f(P a,P b){ return make_pair(max(a.fi,b.fi),(a.fi>=b.fi?a.se:b.se)); //return ret; } P s(){ return make_pair(ls,ls); //return ret; } int main(){ ll n;cin>>n; vl a(n); rep(i,0,n)cin>>a[i]; mint ans=0,as=1; rep(i,0,n){ as*=a[i]; ans+=as*(i==n-1?1:2)*modpow(3,max(0ll,n-i-2)); } cout << ans.val() << endl; }