Problem:
Problem B : Grid
r
×
c
の2次元グリッド上の２つの座標 (
a
1
,
a
2
) と (
b
1
,
b
2
) が与えられる。
あるマス (e,f) から (e+1,f)、(e-1,f)、(e,f+1)、(e,f-1) のどれかのマスに移動するコストを１とする。
また、(e,c-1) と (e,0)、(r-1,f) と (0,f) の間もコスト１で移動することができる。
この時に１つ目の座標から２つ目の座標へ最短コストで移動できる経路の数を求めよ。
Input
入力は以下のフォーマットで与えられる。
r
c
a
1
a
2
b
1
b
2
入力は以下の制約を満たす
1 ≤
r
,
c
≤ 1,000
0 ≤
a
1
,
b
1
< r
0 ≤
a
2
,
b
2
< c
Output
答えの値を100,000,007で割った余りを出力せよ。
Sample Input 1
4 4 0 0 3 3
Sample Output 1
2
Sample Input 2
4 4 0 0 1 1
Sample Output 2
2
Sample Input 3
2 3 0 0 1 2
Sample Output 3
4
Sample Input 4
500 500 0 0 200 200
Sample Output 4
34807775

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i =0; i<(n); i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 100000007;
const int inf ( 1<<30);

int main(){
	int r,c; cin>>r>>c;
	vector<vector<ll>> table(r,vector<ll>(c));
	vector<vector<ll>> visit(r,vector<ll>(c,inf));

	int a1,a2; cin>>a1>>a2;
	queue<P> que;
	que.push({a1,a2});

	int vx[4] = {1,-1,0,0};
	int vy[4] = {0,0,1,-1};
	visit[a1][a2] = 0;
	table[a1][a2] = 1; 
	while(que.size()){
		int x = que.front().first;
		int y = que.front().second;
		que.pop();

		rep(v,4){
			int nx = (r+x+vx[v])%r;
			int ny = (c+y+vy[v])%c;

			if(visit[x][y]+1 < visit[nx][ny]){
				table[nx][ny] = table[x][y];
				table[nx][ny]%= mod;
				que.push({nx,ny});
				visit[nx][ny] = visit[x][y]+1;
			}else if(visit[x][y]+1 == visit[nx][ny]){
				table[nx][ny]+=table[x][y];
				table[nx][ny]%=mod;
			}
		}
	}
	int b1,b2; cin>>b1>>b2;
	cout<<table[b1][b2]<<endl;


}
accuracy: 1
time_ms: 30
memory_kb: 19072
final_rank: 8

code_snippet: #include<bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1e8+7;

ll mod(ll x){
  return (((x % MOD)+MOD)%MOD);
}

ll modadd(ll a,ll b){
  return (mod(a + b));
}

ll modmul(ll a,ll b){
  return mod(a * b);
}

ll modpow(ll a,ll b){
  ll res = 1;
  while(b){
    if(b & 1)res = modmul(res, a);
    a = modmul(a,a);
    b >>= 1;
  }
  return res;
}

ll modinv(ll a){
  return modpow(a,MOD-2);
}

ll moddiv(ll a,ll b){
  return modmul(a,modinv(b));
}

ll factorial(int n){
  static vector<ll> dp(2,1);
  while(dp.size() <= n){
    ll add = modmul(dp.back(),dp.size());
    dp.push_back(add);
  }
  return dp[n];
}

int dx[]={1,0,-1,0};
int dy[]={0,-1,0,1};

int f(int x,int y){
  x %= y;
  x += y;
  x %= y;
  return x;
}

int main(){
  int r;
  int c;
  int x1;
  int x2;
  int y1;
  int y2;
  cin >> r >> c >> x1 >> y1 >> x2 >> y2;
  using P = pair<int,int>;
  auto edges = [&](P p) -> vector<P>{
    int x = p.first;
    int y = p.second;
    vector<P> res;
    for(int i=0;i<4;++i){
      int nx = f(x + dx[i],r);
      int ny = f(y + dy[i],c);
      res.emplace_back(nx,ny);
    }
    return res;
  };

  vector<vector<int>> dists(1001,vector<int>(1001,1e9));
  queue<P> que;
  auto add = [&](P p,int v) -> void{
    if(dists[p.first][p.second] > v){
      dists[p.first][p.second] = v;
      que.push(p);
    }
  };
  add(P(x1,y1),0);
  while(dists[x2][y2] > 1e5){
    P p = que.front();
    que.pop();
    auto v = edges(p);
    for(auto e:v){
      int nt = dists[p.first][p.second] + 1;
      add(e,nt);
    }
  }
  while(que.size())que.pop();
  vector<vector<int>> dps(1001,vector<int>(1001,-1));
  que.emplace(x2,y2);
  dps[x2][y2] = 1;


  while(que.size()){
    P p = que.front();
    que.pop();
    for(auto e:edges(p)){
      if(dists[e.first][e.second] + 1 != dists[p.first][p.second])continue;
      if(dps[e.first][e.second] == -1){
        dps[e.first][e.second] = dps[p.first][p.second];
        que.push(e);
      }else{
        ll x = modadd(dps[e.first][e.second], dps[p.first][p.second]);
        dps[e.first][e.second] = x;
      }
    }
  }
  cout << dps[x1][y1] << endl;
}
accuracy: 1
time_ms: 150
memory_kb: 11360
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const i64 mod = 100000007;

int main() {
	int r, c, a1, a2, b1, b2;
	cin >> r >> c >> a1 >> a2 >> b1 >> b2;
	vector dp(r, vector(c, 0LL));
	const int inf = 100000000;
	vector mindis(r, vector(c, inf));
	dp[a1][a2] = 1LL;
	mindis[a1][a2] = 0;
	queue<pair<int, int>> que;
	que.push({ a1, a2 });


	array<int, 4> dy = { 1, 0, -1, 0 }, dx = { 0, 1, 0, -1 };

	while (que.size()) {
		auto [y, x] = que.front();
		que.pop();
		for (int d = 0 ; d < 4 ; d++) {
			int ny = y + dy[d];
			ny = (ny + r) % r;
			int nx = x + dx[d];
			nx = (nx + c) % c;
			if (mindis[ny][nx] > mindis[y][x] + 1) {
				mindis[ny][nx] = mindis[y][x] + 1;
				dp[ny][nx] = dp[y][x];
				que.push({ ny, nx });
			}
			else if (mindis[ny][nx] == mindis[y][x] + 1) {
				dp[ny][nx] += dp[y][x];
				dp[ny][nx] %= mod;
			}
		}
		/*
		for (auto d : dp) {
			for (auto x : d) cout << x << ' ';
			cout << endl;
		}
		cout << endl;
		*/
	}

/*
	for (auto d : dp) {
		for (auto x : d) cout << x << ' ';
		cout << endl;
	}
	*/

	cout << dp[b1][b2] << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 14948
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
const int MOD = 100000007;


long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    // 前処理
    COMinit();

    long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;

    cin >> r >> c >> a1 >> a2 >> b1 >> b2;

    absb1 = r-abs(a1-b1);
    absb2 = c-abs(a2-b2);
    x = min(abs(a1-b1), absb1);
    y = min(abs(a2-b2), absb2);
    long long int ans = COM(x+y, x);
    if(r == 2*abs(a1-b1)) ans*=2;
    if(c == 2*abs(a2-b2)) ans*=2;
    cout << ans % MOD << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 26556
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
const int MOD = 100000007;


long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    // 前処理
    COMinit();

    long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;

    cin >> r >> c >> a1 >> a2 >> b1 >> b2;

    absb1 = r-abs(a1-b1);
    absb2 = c-abs(a2-b2);
    x = min(abs(a1-b1), absb1);
    y = min(abs(a2-b2), absb2);
    long long int ans = COM(x+y, x);
    if(abs(a1-b1) == absb1) ans*=2;
    if(abs(a2-b2) == absb2) ans*=2;
    cout << ans << endl;
}
accuracy: 0.987012987012987
time_ms: 10
memory_kb: 26556
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
const int MOD = 100000007;


long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        //そのまま計算すると負の値になるのでMODを足す
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    // 前処理
    COMinit();

    long long int r, c, a1, a2, b1, b2, absb1, absb2, x, y;

    cin >> r >> c >> a1 >> a2 >> b1 >> b2;

    absb1 = abs(b1-r+1)+1;
    absb2 = abs(b2-c+1)+1;

    x = min(abs(a1-b1), abs(a1-absb1));
    y = min(abs(a2-b2), abs(a2-absb2));
    long long int ans = COM(x+y, x);
    if(abs(a1-b1) == abs(a1-absb1)) ans*=2;
    if(abs(a2-b2) == abs(a2-absb2)) ans*=2;
    cout << ans << endl;
}
accuracy: 0.12987012987012986
time_ms: 10
memory_kb: 26532
final_rank: 18

code_snippet: #include <bits/stdc++.h>

//#define INF 100000000
//#define MOD (int) (1e9+7)
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

const int MAX = 1000000;
const int MOD = 100000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    long long int r, c, a1, a2, b1, b2;
    cin >> r >> c >> a1 >> a2 >> b1 >> b2;

    long long int m = min(abs(b1 - a1), r - abs(b1 - a1));
    long long int n = min(abs(b2 - a2), c - abs(b2 - a2));

    // 前処理
    COMinit();

    long long int ans = COM(m+n, m);

    if (r == m*2) {
        ans = 2 * ans % MOD;
    }
    if (c == n*2) {
        ans = 2 * ans % MOD;
    }

    cout << ans << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 26536
final_rank: 6

code_snippet: #include<bits/stdc++.h>

//#define INF 100000000
//#define MOD (int) (1e9+7)
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

const int MAX = 1000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    long long int r, c, a1, a2, b1, b2;
    cin >> r >> c >> a1 >> a2 >> b1 >> b2;

    long long int m = min(abs(b1 - a1), r - abs(b1 - a1));
    long long int n = min(abs(b2 - a2), c - abs(b2 - a2));

    // 前処理
    COMinit();

    long long int ans = COM(m+n, m);

    if (r == m*2) {
        ans = 2 * ans % MOD;
    }
    if (c == n*2) {
        ans = 2 * ans % MOD;
    }

    cout << ans << endl;

}
accuracy: 0.11688311688311688
time_ms: 10
memory_kb: 26536
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 100000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}

#define MAX 3000

int ans = INF;
vp v;
int ncr[MAX][MAX];

void f(int sx, int sy, int gx, int gy){
    int dist = abs(sx-gx) + abs(sy-gy);
    if(dist > ans)return;
    if(dist < ans){
        ans = dist;
        v.clear();
    }
    v.push_back(pii(abs(sx-gx),abs(sy-gy)));
}

signed main(void) {
    int r,c;
    cin >> r >> c;
    int sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    f(r+sx,c+sy,gx,gy);
    f(r+sx,c+sy,gx,c+gy);
    f(r+sx,c+sy,gx,c+c+gy);
    f(r+sx,c+sy,r+gx,gy);
    f(r+sx,c+sy,r+gx,c+gy);
    f(r+sx,c+sy,r+gx,c+c+gy);
    f(r+sx,c+sy,r+r+gx,gy);
    f(r+sx,c+sy,r+r+gx,c+gy);
    f(r+sx,c+sy,r+r+gx,c+c+gy);
    ncr[0][0] = 1;
    rep(i,MAX)rep(j,MAX){
        if(i) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j]) % MOD;
        if(i && j) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j - 1]) % MOD;
    }
    int ans = 0;
    rep(i,v.size()){
        int x = v[i].first;
        int y = v[i].second;
        (ans += ncr[x+y][x]) %= MOD;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 73412
final_rank: 13

code_snippet: #include <bits/stdc++.h>
// #define rep(i,n) for(int i=0;i<n;i++)
// #define all(x) (x).begin(),(x).end()
using namespace std;
// const int INF=1145141919,MOD=1e9+7;
// const long long LINF=8931145141919364364,LMOD=998244353;
// inline long long mod(long long n,long long m){return(n%m+m)%m;}
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

struct Combination{
    const int MAX=2010;
    const int MOD;
    vector<vector<int>> COM;
    vector<vector<int>> PER;
    Combination(const int MOD):
        MOD(MOD)
        {
            COM=vector<vector<int>>(MAX,vector<int>(MAX));
            PER=vector<vector<int>>(MAX,vector<int>(MAX));
            COM[0][0]=PER[0][0]=1;
            for(int i=1;i<MAX;i++){
                COM[i][0]=PER[i][0]=1;
                for(int j=1;j<MAX;j++){
                    COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;
                    PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;
                }
            }
        }
    int C(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return COM[n][r];
    }
    int P(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return PER[n][r];
    }
    int H(int n,int r){
        assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);
        return COM[n+r-1][r];
    }
};

int main(){
    const int MOD=100000007;
    Combination com(MOD);
    int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;
    int h1=abs(a1-b1),h2=r-abs(a1-b1);
    int h=min(h1,h2);
    int w1=abs(a2-b2),w2=c-abs(a2-b2);
    int w=min(w1,w2);
    int ans=com.C(h+w,h);
    if(h1==h2) ans=ans*2%MOD;
    if(w1==w2) ans=ans*2%MOD;
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 34400
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
inline long long mod(long long n,long long m){return(n%m+m)%m;}
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

#define int long long

struct Combination{
    const int MAX=2010;
    const int MOD;
    vector<vector<int>> COM;
    vector<vector<int>> PER;
    Combination(const int MOD):
        MOD(MOD)
        {
            COM=vector<vector<int>>(MAX,vector<int>(MAX));
            PER=vector<vector<int>>(MAX,vector<int>(MAX));
            COM[0][0]=PER[0][0]=1;
            for(int i=1;i<MAX;i++){
                COM[i][0]=PER[i][0]=1;
                for(int j=1;j<MAX;j++){
                    COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;
                    PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;
                }
            }
        }
    int C(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return COM[n][r];
    }
    int P(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return PER[n][r];
    }
    int H(int n,int r){
        assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);
        return COM[n+r-1][r];
    }
};

signed main(){
    const int mod=100000007;
    Combination com(mod);
    int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;
    int h1=abs(a1-b1),h2=r-abs(a1-b1);
    int h=min(h1,h2);
    int w1=abs(a2-b2),w2=c-abs(a2-b2);
    int w=min(w1,w2);
    int ans=com.C(h+w,h);
    if(h1==h2) ans=ans*2%mod;
    if(w1==w2) ans=ans*2%mod;
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 66080
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
inline long long mod(long long n,long long m){return(n%m+m)%m;}
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

struct Combination{
    const int MAX=2010;
    const int MOD;
    vector<vector<int>> COM;
    vector<vector<int>> PER;
    Combination(const int MOD):
        MOD(MOD)
        {
            COM=vector<vector<int>>(MAX,vector<int>(MAX));
            PER=vector<vector<int>>(MAX,vector<int>(MAX));
            COM[0][0]=PER[0][0]=1;
            for(int i=1;i<MAX;i++){
                COM[i][0]=PER[i][0]=1;
                for(int j=1;j<MAX;j++){
                    COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;
                    PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;
                }
            }
        }
    int C(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return COM[n][r];
    }
    int P(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return PER[n][r];
    }
    int H(int n,int r){
        assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);
        return COM[n+r-1][r];
    }
};

signed main(){
    Combination com(100000007);
    int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;
    int h1=abs(a1-b1),h2=r-abs(a1-b1);
    int h=min(h1,h2);
    int w1=abs(a2-b2),w2=c-abs(a2-b2);
    int w=min(w1,w2);
    int ans=com.C(h+w,h);
    if(h1==h2) ans=ans*2%MOD;
    if(w1==w2) ans=ans*2%MOD;
    cout<<ans<<endl;
    return 0;
}
accuracy: 0.987012987012987
time_ms: 30
memory_kb: 34400
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
inline long long mod(long long n,long long m){return(n%m+m)%m;}
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

struct Combination{
    const int MAX=2010;
    const int MOD;
    vector<vector<int>> COM;
    vector<vector<int>> PER;
    Combination(const int MOD):
        MOD(MOD)
        {
            COM=vector<vector<int>>(MAX,vector<int>(MAX));
            PER=vector<vector<int>>(MAX,vector<int>(MAX));
            COM[0][0]=PER[0][0]=1;
            for(int i=1;i<MAX;i++){
                COM[i][0]=PER[i][0]=1;
                for(int j=1;j<MAX;j++){
                    COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD;
                    PER[i][j]=PER[i][j-1]*(i-j+1)%MOD;
                }
            }
        }
    int C(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return COM[n][r];
    }
    int P(int n,int r){
        assert(0<=n&&n<MAX&&0<=r&&r<MAX);
        return PER[n][r];
    }
    int H(int n,int r){
        assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX);
        return COM[n+r-1][r];
    }
};

int main(){
    Combination com(MOD);
    long long r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2;
    long long h1=abs(a1-b1),h2=r-abs(a1-b1);
    long long h=min(h1,h2);
    long long w1=abs(a2-b2),w2=c-abs(a2-b2);
    long long w=min(w1,w2);
    long long ans=com.C(h+w,h);
    if(h1==h2) ans=ans*2%MOD;
    if(w1==w2) ans=ans*2%MOD;
    cout<<ans<<endl;
    return 0;
}
accuracy: 0.11688311688311688
time_ms: 40
memory_kb: 34400
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct Combinatorics {
  using int64 = long long;
  int64 mod;
  int64 fact[202020];
  int64 invfact[202020];
  Combinatorics(int64 mod):mod(mod) {
    fact[0] = invfact[0] = 1;
    for(int i = 1; i < 202020; ++i) {
      fact[i] = fact[i-1]*i%mod;
      invfact[i] = minv(fact[i]);
    }
  }
  int64 mpow(int64 x, int64 n) const {
    int64 res = 1;
    while(n > 0) {
      if(n&1) res = res*x%mod;
      x = x*x%mod;
      n >>= 1;
    }
    return res;
  }
  int64 minv(int64 x) const {
    return mpow(x, mod-2);
  }
  int64 mfact(int64 x) const {
    return fact[x];
  }
  int64 C(int64 n, int64 r) const {
    if(r < 0 || n < r) return 0;
    return fact[n]*invfact[r]%mod*invfact[n-r]%mod;
  }
  int64 P(int64 n, int64 r) const {
    if(r < 0 || n < r) return 0;
    return fact[n]*invfact[n-r]%mod;
  }
}C(mod);


signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int r, c, ay, ax, by, bx;
  cin >> r >> c >> ay >> ax >> by >> bx;

  int By[3][3], Bx[3][3];
  reps(i, -1, 2) reps(j, -1, 2) {
    By[i+1][j+1] = by+i*r;
    Bx[i+1][j+1] = bx+j*c;
  }

  int mod = 1e8+7;
  Combinatorics comb(mod);

  int mn = inf;
  rep(i, 3) rep(j, 3) {
    chmin(mn, llabs(ay-By[i][j])+llabs(ax-Bx[i][j]));
  }

  int ans = 0;
  rep(i, 3) rep(j, 3) {
    int d = llabs(ay-By[i][j])+llabs(ax-Bx[i][j]);
    if(mn == d) {
      ans += comb.C(mn, llabs(ay-By[i][j]));
      ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 9536
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct Combinatorics {
  using int64 = long long;
  int64 mod;
  int64 fact[202020];
  int64 invfact[202020];
  Combinatorics(int64 mod):mod(mod) {
    fact[0] = invfact[0] = 1;
    for(int i = 1; i < 202020; ++i) {
      fact[i] = fact[i-1]*i%mod;
      invfact[i] = minv(fact[i]);
    }
  }
  int64 mpow(int64 x, int64 n) const {
    int64 res = 1;
    while(n > 0) {
      if(n&1) res = res*x%mod;
      x = x*x%mod;
      n >>= 1;
    }
    return res;
  }
  int64 minv(int64 x) const {
    return mpow(x, mod-2);
  }
  int64 mfact(int64 x) const {
    return fact[x];
  }
  int64 C(int64 n, int64 r) const {
    if(r < 0 || n < r) return 0;
    return fact[n]*invfact[r]%mod*invfact[n-r]%mod;
  }
  int64 P(int64 n, int64 r) const {
    if(r < 0 || n < r) return 0;
    return fact[n]*invfact[n-r]%mod;
  }
}C(mod);


signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int r, c, ay, ax, by, bx;
  cin >> r >> c >> ay >> ax >> by >> bx;

  int By[3][3], Bx[3][3];
  reps(i, -1, 2) reps(j, -1, 2) {
    By[i+1][j+1] = by+i*r;
    Bx[i+1][j+1] = bx+j*c;
  }

  Combinatorics comb(1e8+7);

  int mn = inf;
  rep(i, 3) rep(j, 3) {
    chmin(mn, llabs(ay-By[i][j])+llabs(ax-Bx[i][j]));
  }

  int ans = 0;
  rep(i, 3) rep(j, 3) {
    int d = llabs(ay-By[i][j])+llabs(ax-Bx[i][j]);
    if(mn == d) {
      ans += comb.C(mn, llabs(ay-By[i][j]));
      ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}
accuracy: 0.987012987012987
time_ms: 160
memory_kb: 9536
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;

typedef long long ll;
const ll mod = 1e8+7;

ll mod_pow(ll x,ll n){
  ll res=1;
  while(n){
    if(n%2) res=res*x%mod;
    x=x*x%mod;
    n/=2;
  }
  return res;
}

ll divi(ll a,ll b){
  return a * mod_pow(b,mod-2) % mod;
}

ll factorial(int i){
  static vector<ll> k(1e6);
  if(!k[0]){k[0]=1;for(int i=1;i<(int)k.size();i++)k[i]=i*k[i-1]%mod;}
  return k[i];
}

ll nCr(ll n,ll r){
  ll a = mod_pow( factorial(r) * factorial(n-r) % mod ,mod-2);
  return factorial(n) * a % mod;
}

typedef pair<int,int> P;

int h, w, sy, sx, ty, tx;

P cal(int y, int x){
  
  int Y = abs(y - sy);
  int X = abs(x - sx);
  
  return P( Y + X, nCr( Y + X, X ));
}

void solve(){
  
  map<int,int> ans;

  for(int i=-1;i<=1;i++){
    
    for(int j=-1;j<=1;j++){
      
      P tmp = cal( ty + i * h, tx + j * w );

      ans[tmp.first] += tmp.second;
      
      ans[tmp.first] %= mod;
    }
    
  }
  
  auto p = ans.begin();
  
  cout<<(*p).second<<endl;
}

signed main(){
  
  cin>>h>>w;
  
  cin>>sy>>sx>>ty>>tx;
  
  solve();  
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 10760
final_rank: 1

code_snippet: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 100000007
using namespace std;
typedef pair<int,int>P;

int d[1000][1000],ans[1000][1000];
int dx[]{1,-1,0,0},dy[]{0,0,1,-1};
int main(){
	int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;
	queue<P>que;
	memset(d,-1,sizeof(d));
	ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));
	while(!que.empty()){
		P p=que.front();que.pop();
		rep(i,4){
			int nx=p.first+dx[i]+r,ny=p.second+dy[i]+c;
			while(nx>=r)nx-=r;while(ny>=c)ny-=c;
			if(d[nx][ny]==-1){
				d[nx][ny]=d[p.first][p.second]+1;
				que.push(P(nx,ny));
			}
			if(d[nx][ny]==d[p.first][p.second]+1)
				(ans[nx][ny]+=ans[p.first][p.second])%=MOD;
		}
	}
	printf("%d\n",ans[x2][y2]);
}
accuracy: 1
time_ms: 20
memory_kb: 11016
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 100000007
using namespace std;
typedef pair<int,int>P;

int d[1000][1000],ans[1000][1000];
int dx[]{1,-1,0,0},dy[]{0,0,1,-1};
int main(){
	int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;
	queue<P>que;
	memset(d,-1,sizeof(d));
	ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));
	while(!que.empty()){
		P p=que.front();que.pop();
		rep(i,4){
			int nx=(p.first+dx[i]+r)%r,ny=(p.second+dy[i]+c)%c;
			if(d[nx][ny]==-1){
				d[nx][ny]=d[p.first][p.second]+1;
				que.push(P(nx,ny));
			}
			if(d[nx][ny]==d[p.first][p.second]+1)
				(ans[nx][ny]+=ans[p.first][p.second])%=MOD;
		}
	}
	printf("%d\n",ans[x2][y2]);
}
accuracy: 1
time_ms: 30
memory_kb: 11112
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 100000007
using namespace std;
typedef pair<int,int>P;

int d[1000][1000],ans[1000][1000];
int dx[]{1,-1,0,0},dy[]{0,0,1,-1};
int main(){
	int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2;
	queue<P>que;
	memset(d,-1,sizeof(d));
	ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1));
	while(!que.empty()){
		P p=que.front();que.pop();
		rep(i,4){
			int nx=p.first+dx[i],ny=p.second+dy[i];
			int a=0;
			if(nx<0||nx>=r){
				nx=(nx+r)%r;a++;
			}
			if(ny<0||ny>=c){
				ny=(ny+c)%c;a++;
			}
			if(a==2)continue;
			if(d[nx][ny]==-1){
				d[nx][ny]=d[p.first][p.second]+1;
				que.push(P(nx,ny));
			}
			if(d[nx][ny]==d[p.first][p.second]+1)
				(ans[nx][ny]+=ans[p.first][p.second])%=MOD;
		}
	}
	printf("%d\n",ans[x2][y2]);
}
accuracy: 1
time_ms: 20
memory_kb: 11088
final_rank: 3

code_snippet: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<unordered_map>
#include<string.h>
#define int long long
#define mod 100000007
using namespace std;

int memo[2001][2001];
int saiki(int a, int b) {
	if (memo[a][b] != -1)return memo[a][b];
	int c = 0;
	if (a == 0 || b == 0)c = 1;
	else {
		c += saiki(a - 1, b);
		c += saiki(a, b - 1);
	}
	return memo[a][b] = c%mod;
}
signed main() {
	memset(memo, -1, sizeof(memo));
	int a, b, c, d, e, f;
	cin >> a >> b >> c >> d >> e >> f;
	int g = abs(e - c), h = abs(f - d);
	int i = a - max(e,c) + min(e,c), j = b - max(f,d) + min(f,d);
	int k = min(g, i), l = min(h, j);
	if (c == e) {
		if (h == j)puts("2");
		else puts("1");
		return 0;
	}
	if (d == f) {
		if (g == i)puts("2");
		else puts("1");
		return 0;
	}
	int ans = saiki(k, l);
	if (g == i) {
		if (h == j) {
			ans *= 4;
		}
		else {
			ans *= 2;
		}
	}
	else {
		if (h == j) {
			ans *= 2;
		}
	}
	cout << ans%mod << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 34404
final_rank: 9