#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } fn squmul(a: [[i64; 2]; 2], b: [[i64; 2]; 2], mo: i64) -> [[i64; 2]; 2] { let n = a.len(); let mut ret = [[0; 2]; 2]; for i in 0..n { for j in 0..n { for k in 0..n { ret[i][k] += a[i][j] * b[j][k]; ret[i][k] %= mo; } } } ret } fn squpow(a: [[i64; 2]; 2], mut e: i64, mo: i64) -> [[i64; 2]; 2] { let n = a.len(); let mut sum = [[0; 2]; 2]; for i in 0..n { sum[i][i] = 1 % mo; } let mut cur = a; while e > 0 { if e % 2 == 1 { sum = squmul(sum, cur, mo); } cur = squmul(cur, cur, mo); e /= 2; } sum } fn powsum(a: [[i64; 2]; 2], n: i64, b: i64) -> [[i64; 2]; 2] { if n == 0 { return [[0; 2]; 2]; } if n % 2 == 0 { let mut tmp = squpow(a, n / 2, b); tmp[0][0] = (tmp[0][0] + 1) % b; tmp[1][1] = (tmp[1][1] + 1) % b; let sub = powsum(a, n / 2, b); return squmul(tmp, sub, b); } let sub = powsum(a, n - 1, b); let mut tmp = squmul(a, sub, b); tmp[0][0] = (tmp[0][0] + 1) % b; tmp[1][1] = (tmp[1][1] + 1) % b; tmp } fn det(a: [[i64; 2]; 2], b: i64) -> i64 { let s = a[0][0] * a[1][1] - a[0][1] * a[1][0]; (s % b + b) % b } fn main() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} #[allow(unused)] macro_rules! putvec { ($v:expr) => { for i in 0..$v.len() { puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "}); } } } input! { t: usize, nba: [(i64, i64, [[i64; 2]; 2]); t], } for (n, b, a) in nba { let mut aa = [[0; 2]; 2]; for i in 0..2 { for j in 0..2 { aa[i][j] = (a[i][j] % b + b) % b; } } let s = powsum(aa, n, b); let mut det = det(s, b) * det(aa, b) % b; if a[0][0] * a[1][1] < a[1][0] * a[0][1] && n % 2 == 1 { det = (b - det) % b; } puts!("{}\n", det); } }