# 牛客网暑期ACM多校训练营（第一场）A.Monotonic Matrix(非降路径和Lindström–Gessel–Viennot定理)

Count the number of n x m matrices A satisfying the following condition modulo (109+7).
* Ai, j ∈ {0, 1, 2} for all 1 ≤ i ≤ n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai + 1, j for all 1 ≤ i < n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai, j + 1 for all 1 ≤ i ≤ n, 1 ≤ j < m.

The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers n and m.

For each test case, print an integer which denotes the result.

1 2
2 2
1000 1000

6
20
540949876

* 1 ≤ n, m ≤ 103
* The number of test cases does not exceed 105.

## 思路

$\left(n,0\right)->\left(0,m\right)$

$\left(n,0\right)->\left(0,m\right)$
$\left(n-1,-1\right)->\left(-1,m-1\right)$

$M=|\begin{array}{cccc}e\left({a}_{1},{b}_{1}\right)& e\left({a}_{1},{b}_{2}\right)& \cdots & e\left({a}_{1},{b}_{n}\right)\\ e\left({a}_{2},{b}_{1}\right)& e\left({a}_{2},{b}_{2}\right)& \cdots & e\left({a}_{2},{b}_{n}\right)\\ ⋮& ⋮& \ddots & ⋮\\ e\left({a}_{n},{b}_{1}\right)& e\left({a}_{n},{b}_{2}\right)& \cdots & e\left({a}_{n},{b}_{n}\right)\end{array}|$

${a}_{1}=\left(n,0\right),{b}_{1}=\left(0,m\right)$
${a}_{2}=\left(n-1,-1\right),{b}_{2}=\left(-1,m-1\right)$

$M=|\begin{array}{cc}e\left({a}_{1},{b}_{1}\right)& e\left({a}_{1},{b}_{2}\right)\\ e\left({a}_{2},{b}_{1}\right)& e\left({a}_{2},{b}_{2}\right)\end{array}|=|\begin{array}{cc}{C}_{n+m}^{n}& {C}_{n+m}^{n+1}\\ {C}_{n+m}^{m+1}& {C}_{n+m}^{n}\end{array}|={{C}_{n+m}^{n}}^{2}-{C}_{n+m}^{n+1}\ast {C}_{n+m}^{m+1}$

$e\left({a}_{i},{b}_{j}\right)$$e(a_i,b_j)$可以用非降路径的方法求出

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
const int N=2e3+5;
const long long mod=1e9+7;
long long C[N][N];
void get_C()
{
C[0][0] = 1;
for(int i=1;i<N;i++)
{
C[i][0] = 1;
for(int j=1;j<=i;j++)
{
C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
}
}
}
int main()
{
get_C();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
long long ans=(C[n+m][n]%mod*C[n+m][n]%mod-C[n+m][n+1]%mod*C[n+m][m+1]%mod)%mod;
printf("%lld\n",(ans+mod)%mod);
}
return 0;
}