Calculate multinomial coefficient
I want to calculate multinomial coefficient mod 1e9 + 7. It equals: n! / (k0! * k1! * k2 * ... * km!) In my case m = 3, k0 + k1 + k2 = n, so it would be: n! / (k0! * k1! * k2!) My code for this: .... long long k2 = n - k1 - k0; long long dans = fact[n] % MOD; long long tmp = fact[i] % MOD; tmp = (tmp * fact[j]) % MOD; tmp = (tpm * fact[k]) % MOD; res = (fact[n] / tmp) % MOD; // mb mistake is here... cout << res; fact[i] - factorial of i mod 1e9+7 It does not work on big tests I hope I'm not linkfarming here, but here is a process of work, to solve your problem : Naive implementations will