• + 1 comment

    As others have been saying, the answer numerically should be (m+n-2) choose (m-1) (mod 10^9 + 7). Of course, the problem is the time limit, which you will see if you try this method. Here are the two insights that I needed to solve this:

    1. since the answer is mod p, make your own factorial function that takes mod p for every successive multiplication
    2. Use FLT and fast exponentiation mod p to divide by (m-1)! and (n-1)! instead of calculating them and then dividing