#include #include using namespace std; #include using mint = atcoder::modint1000000007; int main() { int N; cin >> N; mint ret = 0, prod = 1; const mint d23 = mint(2) / 3; for (int i = 0; i < N; i++) { ret *= 3; int a; cin >> a; prod *= mint(a); ret += prod * (i == N - 1 ? 1 : d23); } cout << ret.val() << '\n'; }