using System; using System.IO; using System.Collections.Generic; namespace No117_1 { public class Program { void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- var T = ss.Next(Int); var comb = new Combination(2000000); for (var i = 0; i < T; i++) { var S = ss.Next(String); var Q = S[0]; var sp = Array.ConvertAll(S.Substring(1).Trim('(', ')').Split(','), int.Parse); var N = sp[0]; var K = sp[1]; if (Q == 'C') sw.WriteLine(comb.NCK(N, K)); if (Q == 'P') sw.WriteLine(comb.NPK(N, K)); if (Q == 'H') sw.WriteLine(comb.NHK(N, K)); } //--------------------------------- } static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false}; new Program().Solve(ss, sw); sw.Flush(); } static readonly Func String = s => s; static readonly Func Int = int.Parse; static readonly Func Long = long.Parse; static readonly Func Double = double.Parse; } public struct ModInt { public static int Mod = 1000000007; readonly long value; public int Value { get { return (int)value; } } public ModInt Inverse { get { return GetInverse(); } } public ModInt(long value) { value %= Mod; this.value = value < 0 ? value + Mod : value; } ModInt GetInverse() { if (value == 0) throw new InvalidOperationException("value must NOT equal 0"); return Pow(Mod - 2); } public ModInt Square() { return this * this; } public ModInt Pow(long exp) { if (exp == 0) return 1; if (exp % 2 == 0) return Pow(exp / 2).Square(); return this * Pow(exp - 1); } public static ModInt Parse(string str) { return long.Parse(str); } public static implicit operator ModInt(long value) { return new ModInt(value); } public static ModInt operator +(ModInt left, ModInt right) { return left.value + right.value; } public static ModInt operator -(ModInt left, ModInt right) { return left.value - right.value; } public static ModInt operator *(ModInt left, ModInt right) { return left.value * right.value; } public static ModInt operator /(ModInt left, ModInt right) { return left * right.Inverse; } public override bool Equals(object obj) { if (!(obj is ModInt)) return false; return value == ((ModInt)obj).value; } public override int GetHashCode() { return value.GetHashCode(); } public override string ToString() { return value.ToString(); } } public class Combination { readonly ModInt[] facts; public Combination(int max) { facts = new ModInt[max + 1]; facts[0] = 1; for (var i = 1; i <= max; i++) facts[i] = facts[i - 1] * i; } public ModInt Fact(int n) { return facts[n]; } public ModInt NCK(int n, int k) { if (n < k) return 0; return facts[n] / facts[k] / facts[n - k]; } public ModInt NPK(int n, int k) { if (n < k) return 0; return facts[n] / facts[n - k]; } public ModInt NHK(int n, int k) { if (k == 0) return 1; if (n == 0) return 0; return facts[n + k - 1] / facts[k] / facts[n - 1]; } } public class StreamScanner { static readonly char[] Sep = {' '}; readonly Queue buffer = new Queue(); readonly TextReader textReader; public StreamScanner(TextReader textReader) { this.textReader = textReader; } public T Next(Func parser) { if (buffer.Count != 0) return parser(buffer.Dequeue()); var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries); foreach (var nextString in nextStrings) buffer.Enqueue(nextString); return Next(parser); } } }