X and Y are considered separately, between (N,M) can choose to walk I step. You need to select the row C (n-2,i) and I-Step corresponding column C (m-2,i) for the I step. Multiply them. Suppose $m\leq n$
$$\sum_{i=1}^{m-2} C_{n-2}^i\cdot c_{m-2}^i=\sum_{i=1}^{m-2} C_{n-2}^i\cdot c_{m-2}^{m-2-i}=c_{n+m-4}^{m-2}$$
Then the inverse of the factorial of the thread is preprocessed, the main sentence:
$ $f [i]= (m-m/i) \cdot f[m\%i]\%m$$
Here f is the inverse of I, why can I ask for it?
$ $M =k\cdot i+r \equiv 0 \pmod m$$
On both sides, multiply $i^{-1}\cdot r^{-1}$.
$$\begin{eqnarray} K\cdot R^{-1}+i^{-1} &\equiv& 0 &\pmod m\\
I^{-1} &\equiv&-k\cdot r^{-1} &\pmod m\\
I^{-1} &\equiv& M-\left\lfloor\frac{m}{i}\right\rfloor\cdot \left (m\bmod i\right) ^{-1} &\pmod M \end{ eqnarray}$$
Code
#include <cstdio> #define M 1000000007#define N 200001#define ll long Longll fac[n]={1,1},inv[n]={1,1},f[n]={1,1}; int N,m;ll C (ll a,ll b) { return fac[a]*inv[b]%m*inv[a-b]%m;} int main () {for (int i=2;i<n;i++) { fac[i]=fac[i-1]*i%m; f[i]= (m-m/i) *f[m%i]%m; inv[i]=inv[i-1]*f[i]%m; } while (~SCANF ("%d%d", &n,&m)) printf ("%lld\n", C (M+n-4,m-2));}
"HDU 5698" instantaneous movement (combination number, inverse)