Some ideas:
It's easy to see that lcm(nx, ny) = n*lcm(x, y)
So actually the problem reduced to calculating lcm for C coefficients.
Also it is easy to see that lcm(x/a, x/b) = x/gcd(a, b), where gcd is a greatest common divisor.
If you remember that n choose k = n!/k!(n-k)! these two steps reduces the problem to calculation of
gcd(0!n!, 1!(n-1)!, 2!(n-2)!, ..., k!(n-k)!)
and then dividing n*n! by this value.
gcd can be easily computed by Euclidean algorithm. There's actually even easier approach:
gcd(i!(n-i)!, (i+1)!(n-i-1)!) = i!(n-i-1)!gcd(n-i, i+1)
(for the last gcd you still have to use Euclidean algorithm. but now it is easier).
You can actually do all of the computations in a ring modulo 1000000007. It means that you can take a remainder of 1000000007 after each multiplication/addition, it wouldn't affect the answer.
In the end you have two values:
x = n*n! mod 1000000007
y = gcd(0!n!, 1!(n-1)!, 2!(n-2)!, ..., k!(n-k)!) mod 1000000007
Instead of dividing these numbers, you can multiply x by z, such that
z*y = 1 modulo 1000000007
You can read more about why this works in this article and how to find such z here.
- You have to use 64-bit integers, because even product of two 1000000007-mod numbers doesn't fit into 32 bits. Alternatively, you can write your own mod-multiplication algorithm, which doesn't overflow 32-bit values (it is easy to do, if you know how to write multiplication algorithm and take my advice on calculation 1000000007 remainder after each step).