use std::io::{BufWriter, stdout, Write};macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { #[allow(unused_mut)] let mut 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)*) => { #[allow(non_snake_case)] #[allow(unused_mut)] let mut $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") }; } #[allow(unused_macros)] macro_rules! debug { ($($a:expr),*) => { eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*); } } pub fn gcd_ext(a: u64, b: u64) -> (u64, i64, i64) { let (mut x, mut x_old) = (0i64, 1i64); let (mut y, mut y_old) = (1i64, 0i64); let (mut r, mut r_old) = (b as i64, a as i64); let mut q: i64; let mut t: i64; while r != 0 { q = r_old / r; t = r; r = r_old - q * r; r_old = t; t = x; x = x_old - q * x; x_old = t; t = y; y = y_old - q * y; y_old = t; } (r_old as u64, x_old, y_old) } pub fn modular_multiplicative_inverse(a: u64, m: u64) -> u64 { let (_, mut x, _) = gcd_ext(a, m); x %= m as i64; if x < 0 { x += m as i64; } x as u64 } pub fn factorial_table_mod(max: u32, _mod: u64) -> Vec { let mut table = vec![0; (max + 1) as usize]; table[0] = 1; for i in 0..(max as usize) { table[i + 1] = table[i] * ((i + 1) as u64) % _mod; } table } pub fn inv_factorial_table_mod(max: u32, max_fac: u64, _mod: u64) -> Vec { let mut table = vec![0; (max + 1) as usize]; table[max as usize] = modular_multiplicative_inverse(max_fac, _mod); for i in (0..(max as usize)).rev() { table[i] = table[i + 1] * ((i + 1) as u64) % _mod; } table } fn main() { input! { t: usize, queries: [String; t], } let _mod = 10u64.pow(9) + 7; let n_max = 10u32.pow(6) * 2; let fac = factorial_table_mod(n_max, _mod); let fac_inv = inv_factorial_table_mod(n_max, fac[n_max as usize], _mod); let out = stdout(); let mut out = BufWriter::new(out.lock()); for s in queries { let v: Vec<_> = s.split(|c| c == '(' || c == ')' || c == ',').collect(); let n: usize = v[1].parse().unwrap(); let k: usize = v[2].parse().unwrap(); if v[0] == "C" && n >= k { let a = fac[n]; let b = fac_inv[k]; let c = fac_inv[n - k]; let bc = (b * c) % _mod; writeln!(out, "{}", (a * bc) % _mod).unwrap(); } else if v[0] == "P" && n >= k { let a = fac[n]; let b = fac_inv[n - k]; writeln!(out, "{}", (a * b) % _mod).unwrap(); } else if v[0] == "H" && n > 0 { let a = fac[n + k - 1]; let b = fac_inv[k]; let c = fac_inv[n - 1]; let bc = (b * c) % _mod; writeln!(out, "{}", (a * bc) % _mod).unwrap(); } else if v[0] == "H" && n == 0 && k == 0 { writeln!(out, "1").unwrap(); } else { writeln!(out, "0").unwrap(); } } }