struct ModInt @@MOD = 1_000_000_007i64 def self.mod @@MOD end def self.zero ModInt.new(0) end def initialize(n) @n = n.to_i64 % @@MOD end getter n : Int64 def + : self self end def - : self ModInt.new(n != 0 ? @@MOD - @n : 0) end def +(m) ModInt.new(@n + m.to_i64 % @@MOD) end def -(m) ModInt.new(@n - m.to_i64 % @@MOD) end def *(m) ModInt.new(@n * m.to_i64 % @@MOD) end def /(m) raise DivisionByZeroError.new if m == 0 a, b, u, v = m.to_i64, @@MOD, 1i64, 0i64 while b != 0 t = a // b a -= t * b a, b = b, a u -= t * v u, v = v, u end ModInt.new(@n * u) end def //(m) self / m end def **(m) t, res = self, ModInt.new(1) while m > 0 res *= t if m.odd? t *= t m >>= 1 end res end def ==(m) @n == m.to_i64 end def !=(m) @n != m.to_i64 end def succ self + 1 end def pred self - 1 end def to_i64 : Int64 @n end delegate to_s, to: @n delegate inspect, to: @n end class Comb(T) def initialize(@Max = 300009) @fac = Array(T).new(@Max, T.zero) @finv = Array(T).new(@Max, T.zero) @inv = Array(T).new(@Max, T.zero) @fac[0] = @fac[1] = T.new(1) @finv[0] = @finv[1] = T.new(1) @inv[1] = T.new(1) (2...@Max).each do |i| @fac[i] = @fac[i - 1] * i @inv[i] = -@inv[T.mod % i] * (T.mod / i) @finv[i] = @finv[i - 1]*@inv[i] end end def p(n, r) (n < r || n < 0 || r < 0) ? 0 : @fac[n] * @finv[n - r] end def c(n, r) (n < r || n < 0 || r < 0) ? 0 : @fac[n] * @finv[r] * @finv[n - r] end def h(n, r) (n < 0 || r < 0) ? 0 : r == 0 ? 1 : c(n + r - 1, r) end def fact(n) @fac[n] end end C = Comb(ModInt).new(2000009) read_line.to_i.times do str = read_line comma = str.index(',').not_nil! n = str[2...comma].to_i m = str[comma + 1...-1].to_i case str[0] when 'C' puts C.c(n, m) when 'P' puts C.p(n, m) when 'H' puts C.h(n, m) end end