// ---------- begin ModInt ---------- const MOD: u32 = 1_000_000_007; #[derive(Clone, Copy)] struct ModInt(u32); impl std::ops::Add for ModInt { type Output = ModInt; fn add(self, rhs: ModInt) -> Self::Output { let mut d = self.0 + rhs.0; if d >= MOD { d -= MOD; } ModInt(d) } } impl std::ops::AddAssign for ModInt { fn add_assign(&mut self, rhs: ModInt) { *self = *self + rhs; } } impl std::ops::Sub for ModInt { type Output = ModInt; fn sub(self, rhs: ModInt) -> Self::Output { let mut d = self.0 + MOD - rhs.0; if d >= MOD { d -= MOD; } ModInt(d) } } impl std::ops::SubAssign for ModInt { fn sub_assign(&mut self, rhs: ModInt) { *self = *self - rhs; } } impl std::ops::Mul for ModInt { type Output = ModInt; fn mul(self, rhs: ModInt) -> Self::Output { ModInt((self.0 as u64 * rhs.0 as u64 % MOD as u64) as u32) } } impl std::ops::MulAssign for ModInt { fn mul_assign(&mut self, rhs: ModInt) { *self = *self * rhs; } } impl std::ops::Neg for ModInt { type Output = ModInt; fn neg(self) -> Self::Output { ModInt(if self.0 == 0 {0} else {MOD - self.0}) } } impl std::fmt::Display for ModInt { fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::str::FromStr for ModInt { type Err = std::num::ParseIntError; fn from_str(s: &str) -> Result { let val = s.parse::()?; Ok(ModInt::new(val)) } } #[allow(dead_code)] impl ModInt { pub fn new(n: u32) -> ModInt { ModInt(n % MOD) } pub fn zero() -> ModInt { ModInt(0) } pub fn one() -> ModInt { ModInt(1) } pub fn pow(self, mut n: u32) -> ModInt { let mut t = ModInt::one(); let mut s = self; while n > 0 { if n & 1 == 1 { t *= s; } s *= s; n >>= 1; } t } pub fn inv(self) -> ModInt { assert!(self.0 > 0); self.pow(MOD - 2) } } // ---------- end ModInt ---------- // ---------- begin Matrix ---------- #[allow(dead_code)] mod matrix { use std::ops::{Add, Mul}; pub trait SemiRing: Add + Mul + Copy { fn zero() -> Self; fn one() -> Self; } pub const SIZE: usize = 4; #[derive(Clone)] pub struct SquareMatrix { buf: [[T; SIZE]; SIZE], } impl SquareMatrix { pub fn zero() -> Self { let z = T::zero(); SquareMatrix { buf: [[z; SIZE]; SIZE], } } pub fn identity() -> Self { let mut m = Self::zero(); for i in 0..SIZE { m.buf[i][i] = T::one(); } m } pub fn set_at(&mut self, i: usize, j: usize, v: T) { self.buf[i][j] = v; } pub fn get_at(&self, i: usize, j: usize) -> T { self.buf[i][j] } pub fn matmul(&self, rhs: &Self) -> Self { let mut res = Self::zero(); for (x, a) in res.buf.iter_mut().zip(self.buf.iter()) { for (a, b) in a.iter().zip(rhs.buf.iter()) { for (x, b) in x.iter_mut().zip(b.iter()) { *x = *x + *a * *b; } } } res } pub fn matadd(&self, rhs: &Self) -> Self { let mut c = Self::zero(); for (c, (a, b)) in c.buf.iter_mut().zip(self.buf.iter().zip(rhs.buf.iter())) { for (c, (a, b)) in c.iter_mut().zip(a.iter().zip(b.iter())) { *c = *a + *b; } } c } pub fn matpow(&self, mut n: usize) -> Self { let mut t = Self::identity(); let mut s = self.clone(); while n > 0 { if n & 1 == 1 { t = t.matmul(&s); } s = s.matmul(&s); n >>= 1; } t } } } // ---------- end Matrix ---------- //https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // use matrix::*; use std::io::Write; type Matrix = SquareMatrix::; impl SemiRing for ModInt { fn zero() -> Self { ModInt::zero() } fn one() -> Self { ModInt::one() } } fn solve(p: ModInt, n: usize) -> ModInt { let mut a = [ModInt::zero(); 4]; a[1] = ModInt::one(); for i in 2..4 { a[i] = p * a[i - 1] + a[i - 2]; } let mut b = [ModInt::zero(); 4]; for (i, &x) in a.iter().enumerate() { for (j, &y) in a.iter().enumerate() { if i + j < 4 { b[i + j] += x * y; } } } let n = n - 2; let mut mat = Matrix::zero(); for i in 1..4 { mat.set_at(i, i - 1, ModInt::one()); } mat.set_at(0, 0, ModInt(2) * p); mat.set_at(0, 1, ModInt(2) - p * p); mat.set_at(0, 2, -ModInt(2) * p); mat.set_at(0, 3, -ModInt::one()); let mat = mat.matpow(n); let mut ans = ModInt::zero(); for i in 0..4 { ans += mat.get_at(3, i) * b[3 - i]; } ans } fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); input! { p: ModInt, q: usize, a: [usize; q], } for a in a { let ans = solve(p, a); writeln!(out, "{}", ans).ok(); } } fn main() { run(); }