#![allow(unused_imports)] use {std::cmp::*, std::collections::*, std::io::Write, std::ops::*}; #[allow(unused_macros)] macro_rules !dbg {($($e :expr ) ,*) =>{#[cfg (debug_assertions ) ] $({let (e ,mut err ) =(stringify !($e ) ,std ::io ::stderr () ) ;writeln !(err ,"{} = {:?}" ,e ,$e ) .unwrap () } ) *} ;} #[allow(unused_macros)] macro_rules !min {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::min ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::min ($a ,min !($($rest ) ,+) ) } } ;} #[allow(unused_macros)] macro_rules !chmin {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_min =min !($($cmps ) ,+) ;if $base >cmp_min {$base =cmp_min ;true } else {false } } } ;} #[allow(unused_macros)] macro_rules !max {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::max ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::max ($a ,max !($($rest ) ,+) ) } } ;} #[allow(unused_macros)] macro_rules !chmax {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_max =max !($($cmps ) ,+) ;if $base { it: SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next(&mut self) -> T { match self.it.next().unwrap().parse::() { Ok(v) => v, _ => panic!("Scanner error"), } } pub fn next_chars(&mut self) -> Vec { self.next::().chars().collect() } pub fn next_vec(&mut self, len: usize) -> Vec { (0..len).map(|_| self.next()).collect() } } pub fn read_string() -> String { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s } } fn main() { let sc = scanner::read_string(); let mut sc = scanner::Scanner::new(&sc); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let s: usize = sc.next(); for _ in 0..s { let n: usize = sc.next(); let m = Mint(sc.next()); let x: usize = sc.next(); if x == 0 { writeln!(out, "{}", ((Mint(1) + m).pow(n) + (Mint(1) - m).pow(n)) / 2).unwrap(); } else { writeln!(out, "{}", ((Mint(1) + m).pow(n) - (Mint(1) - m).pow(n)) / 2).unwrap(); } } } const MOD: i64 = 1000000007; #[derive(Copy, Clone)] pub struct Mint(i64); impl Mint { fn new(x: i64) -> Self { Mint(x.rem_euclid(MOD)) } fn pow(self, n: usize) -> Self { match n { 0 => Mint::new(1), _ => { let mut a = self.pow(n >> 1); a *= a; if n & 1 == 1 { a *= self; } a } } } fn inv(self) -> Self { self.pow((MOD - 2) as usize) } } impl std::ops::Neg for Mint { type Output = Mint; fn neg(self) -> Self::Output { Self::new(-self.0) } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; self.0 %= MOD; } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, rhs: i64) { *self += Mint::new(rhs); } } impl std::ops::Add for Mint where Mint: std::ops::AddAssign, { type Output = Self; fn add(self, other: T) -> Self { let mut res = self; res += other; res } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; if self.0 < 0 { self.0 += MOD; } } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, rhs: i64) { *self -= Mint::new(rhs); } } impl std::ops::Sub for Mint where Mint: std::ops::SubAssign, { type Output = Self; fn sub(self, other: T) -> Self { let mut res = self; res -= other; res } } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0; self.0 %= MOD; } } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, rhs: i64) { *self *= Mint::new(rhs); } } impl std::ops::Mul for Mint where Mint: std::ops::MulAssign, { type Output = Self; fn mul(self, other: T) -> Self { let mut res = self; res *= other; res } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, rhs: Self) { *self *= rhs.inv(); } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, rhs: i64) { *self /= Mint::new(rhs); } } impl std::ops::Div for Mint where Mint: std::ops::DivAssign, { type Output = Self; fn div(self, other: T) -> Self { let mut res = self; res /= other; res } } impl std::fmt::Display for Mint { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::ops::Deref for Mint { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 } } impl std::ops::DerefMut for Mint { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } struct Fact { fact: Vec, ifact: Vec, } impl Fact { fn new(n: usize) -> Self { let mut fact = vec![Mint::new(1); n + 1]; let mut ifact = vec![Mint::new(1); n + 1]; for i in 1..n + 1 { fact[i] = fact[i - 1] * i as i64; } ifact[n] = fact[n as usize].inv(); for i in (1..n + 1).rev() { ifact[i - 1] = ifact[i] * i as i64; } Fact { fact: fact, ifact: ifact, } } fn c(&self, n: usize, r: usize) -> Mint { if r > n { Mint::new(0) } else { self.fact[n] * self.ifact[r] * self.ifact[n - r] } } fn p(&self, n: usize, r: usize) -> Mint { if r > n { Mint::new(0) } else { self.fact[n] * self.ifact[n - r] } } }