f_{i,j} = \sum_{k = 0} ^ {j + 1} f_{i-1,k}
$$
f_{i,j} = f_{i,j-1} + f_{i-1,j+1}
$$
### 反射容斥优化
大眼观察上面的式子,有没有很像“从某个点到某个点规定走法的路径条数”之类的计数问题?顺着这个思路我们也把这个式子赋一个类似的含义:从源点出发每步只能向右或向左上走的路径条数。试着画个图出来:

走斜线不好转移,试着把它拉伸一下:

问题也就转化成了“求从 $(0,0)$ 出发,每次只能向上或向右走,不能经过直线 $y=x+1$ 和 直线 $y=x-m-2$ 走到 $(n+m+1,n)$ 的方案数”。
对于这种“不能经过某条直线从某点到某点的方案数”的问题,有一个经典的 trick 叫反射容斥,这里假设我们求的是从 $(0,0)$ 到 $(n,m)$ 的方案数。
现在先考虑一下简单的情况:只有一条直线 $y = x + b$ 的限制(为了方便令 $n+b > m$),那么每一条不合法的路线都能对应一条从 $(0,0)$ 到 $(n,m)$ 关于 $y = x + b$ 的对称点 $(m - b, n + b)$ 的路线(从第一次碰撞开始对称),如下图:

接下来是两条直线的情况,不难想到容斥:总方案 - 碰到一条直线的方案 + 碰到两条直线的方案。
但是这是不对的,因为反射后的直线可能还会碰到另一条直线,然后在两条直线之间疯狂反弹。
不妨设反射序列 $ABABAB...$ 表示先碰到直线 $a$ 再碰到直线 $B$,……。
那么最后的答案就是:总方案 - $A....$ - $B....$,发现后面是一个递归的形式可以直接算,并且对称的时候每次至少走 $1$ 的距离,所以复杂度是 $O(n)$ 的。
画图不易,留个赞再走呗 QAQ。
code:
```cpp
#include <bits/stdc++.h>
#define AC true
#define int long long
#define dub double
#define mar(x) for(int i = head[x]; i; i = e[i].nxt)
#define car(a) memset(a, 0, sizeof(a))
#define cap(a, b) memcpy(a, b, sizeof(b))
const int inf = 1e9 + 7;
const int MAXN = 6e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
void cmx(int &x, int y){x < y && (x = y);}
void cmi(int &x, int y){x > y && (x = y);}
inline int read( ){
int x = 0 ; short w = 0 ; char ch = 0;
while( !isdigit(ch) ) { w|=ch=='-';ch=getchar();}
while( isdigit(ch) ) {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return w ? -x : x;
}
int ji[MAXN], inv[MAXN];
int ans, n, m;
void cof(int &x, int &y, int b){swap(x, y); x -= b; y += b;}
int fsp(int x, int k = mod - 2){
int res = 1;
while(k){
if(k & 1) res = res * x % mod;
x = x * x % mod;
k >>= 1;
}
return res;
}
void init(int n){
ji[0] = inv[0] = 1;
for(int i = 1; i <= n; i++) ji[i] = ji[i-1] * i % mod;
inv[n] = fsp(ji[n]);
for(int i = n - 1; i >= 1; i--) inv[i] = inv[i+1] * (i + 1) % mod;
return;
}
int C(int n, int m){return ji[n] * inv[m] % mod * inv[n-m] % mod;}
signed main( ){
n = read( ); m = read( );
init(5e6 + 500);
ans = C((n << 1) + m + 1, n);
int x = n + m + 1, y = n;
while(x >= 0 and y >= 0){//-ABABABABABA
cof(x, y, 1); ans -= C(x + y, x) - mod; ans %= mod;
cof(x, y, -(m + 2)); ans += C(x + y, x); ans %= mod;
}
x = n + m + 1, y = n;
while(x >= 0 and y >= 0){//-BABABABABAB
cof(x, y, -(m + 2)); ans -= C(x + y, x) - mod; ans %= mod;
cof(x, y, 1); ans += C(x + y, x); ans %= mod;
}
printf("%lld\n",ans);
return (0-0);
}
```