def memo m = [] lambda {|arg1,arg2| (m.size..arg1).each{|i|m << yield(i,arg2)} (arg1<0)?yield(arg1,arg2):m[arg1] } end Inv = memo{|m,mod| case when m>1 then ((mod-mod/m)*Inv[mod%m,mod])%mod else 1 end } Fact = memo{|m,mod| case when m>0 then (m*Fact[m-1,mod])%mod else 1 end } Factr = memo{|m,mod| case when m>0 then (Inv[m,mod]*Factr[m-1,mod])%mod when m==0 then 1 else 0 end } def combination_len(n,r,mod);Fact[n,mod]*Factr[r,mod]%mod*Factr[n-r,mod]%mod;end def repeated_combination_len(n,r,mod);combination_len(n+r-1,r,mod);end def permutation_len(n,r,mod);Fact[n,mod]*Factr[n-r,mod]%mod;end MOD = 1000000007 m=Hash[?C,:combination_len,?P,:permutation_len,?H,:repeated_combination_len] gets.to_i.times{ a,b,c = gets.split(/\(|\)|,/) p send(m[a],b.to_i,c.to_i,1000000007) }