const MOD: isize = 1e9 as isize + 7; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut dp = vec![vec![0isize; 2]; n]; dp[0][1] = a[0]; for i in 0..n-1 { dp[i+1][0] = (dp[i][0] * 3 + dp[i][1] * 2) % MOD; dp[i+1][1] = dp[i][1] * a[i+1] % MOD; } println!("{}", (dp[n-1][0] + dp[n-1][1]) % MOD); }