import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; long ncr(int n, int r, int mod=10^^9+7){ if (n-r < r) r = n-r; if (r == 0) return 1; if (r == 1) return n; int[] numerator = new int[](r); int[] denominator = new int[](r); foreach (k; iota(r)) { numerator[k] = n - r + k + 1; denominator[k] = k + 1; } foreach (p; iota(2, r+1)) { int pivot = denominator[p-1]; if (pivot > 1) { int offset = (n-r)%p; foreach (k; iota(p-1, r, p)) { numerator[k-offset] /= pivot; denominator[k] /= pivot; } } } long result = 1; foreach (k; iota(r)) if (numerator[k] > 1) result = (result * numerator[k]) % mod; return result; } void main() { long mod = 10^^9+7; long[] factorial = new long[](10^^6+1); factorial[0] = 1; factorial[1] = 1; foreach (i; iota(2, 10^^6+1)) factorial[i] = (i * factorial[i-1]) % mod; int T = readln().chomp.to!int; foreach (i; iota(T)) { auto input = readln().chomp.replace("(", " ").replace(",", " ").replace(")", "").split; int n = input[1].to!int; int k = input[2].to!int; if (input[0] == "C") writeln(ncr(n, k)); else if (input[0] == "P") writeln(ncr(n, k)*factorial[k]%mod); else writeln(ncr(n+k-1, k)); } }