Problem:
MinimumCostPath
NxNの方眼がある。
(N=3の場合の図)
隣接するマス目に移動するにはコストが1かかる。ただし、斜めに移動することは出来ない。
また障害物があるマス目がM個あり、そのマスに入ることは許されない。
マス(1, 1)からマス(N, N)に最小のコストで移動する方法は何通りあるか。
答えは大きくなることがありうるので、1000000009(=10
9
+9)でMODを取って出力せよ。
Input
入力は以下の形式で与えられる
N M
X
1
Y
1
X
2
Y
2
……
X
M
Y
M
X
i
, Y
i
は(X
i
, Y
i
)に障害物があることを表す
Constraints
2 ≤ N ≤ 10
6
0 ≤ M ≤ 50
1 ≤ X
i
, Y
i
≤ N
i ≠ jならば (X
i
, Y
i
) ≠ (X
j
, Y
j
)
(X
i
, Y
i
) ≠ (1, 1)
(X
i
, Y
i
) ≠ (N, N)
Output
1行に経路の総数を出力せよ。ただし、マス(1, 1)からマス(N, N)に移動する経路が存在しない場合は0と出力せよ。
Sample Input 1
3 0
Output for the Sample Input 1
6
以下の図に対応する。
Sample Input 2
3 1
2 2
Output for the Sample Input 2
2
以下の図に対応する。
Sample Input 3
5 8
4 3
2 1
4 2
4 4
3 4
2 2
2 4
1 4
Output for the Sample Input 3
1
以下の図に対応する。
Sample Input 4
1000 10
104 87
637 366
393 681
215 604
707 876
943 414
95 327
93 415
663 596
661 842
Output for the Sample Input 4
340340391
Sample Input 5
2 2
1 2
2 1
Output for the Sample Input 5
0

Submitted solutions:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

template <ll mod>
struct modint {
    ll x;

    modint() : x(0) {}

    modint(ll y) {
        x = y % mod;
        if (x < 0) x += mod;
    }

    static ll get_mod() { return mod; }

    modint &operator+=(modint p) {
        x += p.x;
        if (x >= mod) x -= mod;
        return *this;
    }

    modint &operator-=(modint p) {
        x -= p.x;
        if (x < 0) x += mod;
        return *this;
    }

    modint &operator*=(modint p) {
        x *= p.x;
        x %= mod;
        return *this;
    }

    modint &operator/=(modint p) {
        x *= p.inverse();
        return *this;
    }

    modint operator-() { return modint(-x); }
    modint operator+(modint p) { return modint(*this) += p; }
    modint operator-(modint p) { return modint(*this) -= p; }
    modint operator*(modint p) { return modint(*this) *= p; }
    modint operator/(modint p) { return modint(*this) /= p; }
    bool operator==(modint p) { return x == p.x; }
    bool operator!=(modint p) { return x != p.x; }

    modint pow(ll k) {
        modint now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    modint inverse() {
        assert(x != 0);
        return pow(mod - 2);
    }

    friend ostream &operator<<(ostream &os, modint p) { return os << p.x; }

    friend istream &operator>>(istream &is, modint &p) {
        ll y;
        is >> y;
        p = modint(y);
        return is;
    }
};

using mint = modint<1000000009>;

const int MAX = 2000000;

mint fac[MAX + 1], ifac[MAX + 1];

void init_fac() {
    fac[0] = 1;
    rep2(i, 1, MAX + 1) fac[i] = fac[i - 1] * i;
    ifac[MAX] = fac[MAX].inverse();
    per2(i, 1, MAX + 1) ifac[i - 1] = ifac[i] * i;
}

mint C(int n, int k) {
    if (k < 0 || n < k) return 0;
    return fac[n] * ifac[n - k] * ifac[k];
}

mint C2(int x, int y) { return C(x + y, x); }

int main() {
    int N, M;
    cin >> N >> M;

    init_fac();

    int D = 100;

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

    auto solve = [&](vector<vector<int>> c) {
        int H = sz(c), W = sz(c[0]);
        vector<vector<int>> d(H, vector<int>(W, inf));
        vector<vector<mint>> dp(H, vector<mint>(W, 0));

        queue<pii> que;
        d[0][0] = 0;
        dp[0][0] = 1;
        que.emplace(0, 0);

        auto IN = [&](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W && c[x][y] == 0; };

        while (!empty(que)) {
            auto [x, y] = que.front();
            que.pop();
            rep(k, 4) {
                int nx = x + dx[k], ny = y + dy[k];
                if (!IN(nx, ny)) continue;
                if (chmin(d[nx][ny], d[x][y] + 1)) que.emplace(nx, ny);
                if (d[nx][ny] == d[x][y] + 1) dp[nx][ny] += dp[x][y];
            }
        }

        return make_pair(d, dp);
    };

    vector<int> x(M), y(M);
    rep(i, M) cin >> x[i] >> y[i], x[i]--, y[i]--;

    if (N <= 2 * D) {
        vector<vector<int>> c(N, vector<int>(N, 0));
        rep(i, M) c[x[i]][y[i]]++;
        auto [d, dp] = solve(c);
        cout << dp[N - 1][N - 1] << '\n';
        return 0;
    }

    vector<vector<int>> c1(D, vector<int>(D, 0)), c2 = c1;
    rep(i, M) {
        if (x[i] < D && y[i] < D) c1[x[i]][y[i]]++;
        if (x[i] >= N - D && y[i] >= N - D) c2[N - 1 - x[i]][N - 1 - y[i]]++;
    }

    vector<vector<int>> d1, d2;
    vector<vector<mint>> dp1, dp2;

    tie(d1, dp1) = solve(c1);
    tie(d2, dp2) = solve(c2);

    int m1 = inf;
    rep(i, D - 1) chmin(m1, d1[i][D - 1] - i);
    rep(j, D - 1) chmin(m1, d1[D - 1][j] - j);

    int m2 = inf;
    rep(i, D - 1) chmin(m2, d2[i][D - 1] - i);
    rep(j, D - 1) chmin(m2, d2[D - 1][j] - j);

    vector<int> ord(M);
    rep(i, M) ord[i] = i;
    sort(all(ord), [&](int i, int j) {
        if (x[i] != x[j]) return x[i] < x[j];
        return y[i] < y[j];
    });

    vector<mint> dp(M, 0);

    auto get = [&](int X, int Y) {
        mint ret = 0;
        rep(i, D) {
            if (d1[i][D - 1] - i == m1) ret += dp1[i][D - 1] * C2(X - i, Y - D);
        }
        rep(j, D) {
            if (d1[D - 1][j] - j == m1) ret += dp1[D - 1][j] * C2(X - D, Y - j);
        }
        rep(i, M) {
            ret -= dp[i] * C2(X - x[i], Y - y[i]); //
        }
        return ret;
    };

    each(e, ord) dp[e] = get(x[e], y[e]);

    mint ans = 0;
    rep(i, D) {
        if (d2[i][D - 1] - i == m2) ans += get(N - 1 - i, N - D - 1) * dp2[i][D - 1];
    }
    rep(j, D) {
        if (d2[D - 1][j] - j == m2) ans += get(N - D - 1, N - 1 - j) * dp2[D - 1][j];
    }

    cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 35088
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const long long mod = 1000000009;
const long long BORDER = 60;
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, -1, 0, 1 };
long long fact[2000009];
long long inv[2000009];
int N;
int M, X[59], Y[59];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 30; i++) {
		if ((b >> i) & 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Division(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

void Initialize() {
	fact[0] = 1;
	for (int i = 1; i <= 2000000; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 0; i <= 2000000; i++) inv[i] = Division(1, fact[i], mod);
}

long long ncr(int n, int r) {
	if (r < 0 || n < r) return 0;
	return (fact[n] * inv[r] % mod) * inv[n - r] % mod;
}

// Calculate Shortest Path with Naive DP
vector<vector<pair<int, int>>> Shortest1(int H, int W, vector<int> X, vector<int> Y, int Mode) {
	vector<vector<pair<int, int>>> Answer(H, vector<pair<int, int>>(W, make_pair(10000000, 0)));
	vector<vector<bool>> Obstacle(H, vector<bool>(W, false));
	for (int i = 0; i < X.size(); i++) Obstacle[X[i]][Y[i]] = true;
	Answer[0][0].first = 0;
	Answer[0][0].second = 1;

	// Calculate
	for (int i = 0; i < H + W + 100; i++) {
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				if (Mode == 2 && (j == H - 1 || k == W - 1)) continue;
				if (Answer[j][k].first != i) continue;
				for (int l = 0; l < 4; l++) {
					int sx = j + dx[l];
					int sy = k + dy[l];
					if (sx < 0 || sy < 0 || sx >= H || sy >= W || Obstacle[sx][sy] == true) continue;
					if (Answer[sx][sy].first > Answer[j][k].first + 1) {
						Answer[sx][sy].first = Answer[j][k].first + 1;
						Answer[sx][sy].second = 0;
					}
					if (Answer[sx][sy].first == Answer[j][k].first + 1) {
						Answer[sx][sy].second += Answer[j][k].second;
						Answer[sx][sy].second %= mod;
					}
				}
			}
		}
	}
	return Answer;
}

// Calculate Shortest Path with Fast Solution
int Shortest2(int H, int W, vector<int> X, vector<int> Y) {
	vector<pair<int, int>> List;
	for (int i = 0; i < X.size(); i++) List.push_back(make_pair(X[i], Y[i]));
	List.push_back(make_pair(0, 0));
	List.push_back(make_pair(H - 1, W - 1));
	sort(List.begin(), List.end());

	// DP Start
	vector<long long> DP(List.size(), 0);
	DP[0] = mod - 1;
	for (int i = 0; i < List.size(); i++) {
		for (int j = i + 1; j < List.size(); j++) {
			int ex = List[j].first - List[i].first;
			int ey = List[j].second - List[i].second;
			if (ex < 0 || ey < 0) continue;
			long long ways = ncr(ex + ey, ey);
			DP[j] -= DP[i] * ways;
			DP[j] = (DP[j] + mod * mod) % mod;
		}
	}
	return DP[List.size() - 1];
}

int main() {
	// Step 1. Input
	cin >> N >> M;
	for (int i = 0; i < M; i++) cin >> X[i] >> Y[i];
	Initialize();

	// Step 2. Easy Case
	if (N <= BORDER * 2) {
		vector<int> LX, LY;
		for (int i = 0; i < M; i++) LX.push_back(X[i] - 1);
		for (int i = 0; i < M; i++) LY.push_back(Y[i] - 1);
		vector<vector<pair<int, int>>> Return = Shortest1(N, N, LX, LY, 1);
		cout << Return[N - 1][N - 1].second << endl;
		return 0;
	}

	// Step 3. Hard Case: Left Corner
	vector<int> LX, LY;
	for (int i = 0; i < M; i++) {
		int ux = X[i] - 1;
		int uy = Y[i] - 1;
		if (ux >= BORDER || uy >= BORDER) continue;
		LX.push_back(ux);
		LY.push_back(uy);
	}
	vector<vector<pair<int, int>>> Way1 = Shortest1(BORDER, BORDER, LX, LY, 2);

	// Step 4. Hard Case: Right Corner
	vector<int> RX, RY;
	for (int i = 0; i < M; i++) {
		int ux = N - X[i];
		int uy = N - Y[i];
		if (ux >= BORDER || uy >= BORDER) continue;
		RX.push_back(ux);
		RY.push_back(uy);
	}
	vector<vector<pair<int, int>>> Way2 = Shortest1(BORDER, BORDER, RX, RY, 2);

	// Step 5. Merge
	long long Answer1 = 10000000;
	long long Answer2 = 0;
	for (int px = 0; px < BORDER; px++) {
		for (int py = 0; py < BORDER; py++) {
			for (int qx = 0; qx < BORDER; qx++) {
				for (int qy = 0; qy < BORDER; qy++) {
					if (px != BORDER - 1 && py != BORDER - 1) continue;
					if (qx != BORDER - 1 && qy != BORDER - 1) continue;
					if (Way1[px][py].second == 0) continue;
					if (Way2[qx][qy].second == 0) continue;

					// Calculate
					int lx = px + 1, rx = N - qx;
					int ly = py + 1, ry = N - qy;
					vector<int> PX, PY;
					for (int i = 0; i < M; i++) {
						if (lx <= X[i] && X[i] <= rx && ly <= Y[i] && Y[i] <= ry) {
							PX.push_back(X[i] - lx);
							PY.push_back(Y[i] - ly);
						}
					}
					long long Way3 = Shortest2(rx - lx + 1, ry - ly + 1, PX, PY); if (Way3 == 0) continue;
					long long Dist = Way1[px][py].first + Way2[qx][qy].first + (rx - lx) + (ry - ly);
					if (Answer1 > Dist) {
						Answer1 = Dist;
						Answer2 = 0;
					}
					if (Answer1 == Dist) {
						Answer2 += (1LL * Way1[px][py].second * Way2[qx][qy].second % mod) * Way3 % mod;
						Answer2 %= mod;
					}
				}
			}
		}
	}

	// Step 6. Output
	cout << Answer2 << endl;
	return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 34736
final_rank: 4

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000009;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
	ll res = mod_pow(x, -n, m);
	return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
	if (n & 1)res = res * x % m;
	x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 22;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}
int gcd(int a, int b) {
	if (a < b)swap(a, b);
	while (b) {
		int r = a % b; a = b; b = r;
	}
	return a;
}

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

bool exiroad(int n, int m, vector<P> p) {
	vector<int> vx, vy;
	rep(i, p.size()) {
		int x = p[i].first;
		vx.push_back(x);
		if (x - 1 >= 0)vx.push_back(x - 1);
		if (x + 1 < n)vx.push_back(x + 1);
		int y = p[i].second;
		vy.push_back(y);
		if (y - 1 >= 0)vy.push_back(y - 1);
		if (y + 1 < m)vy.push_back(y + 1);
	}
	vx.push_back(0);
	vx.push_back(n - 1);
	vy.push_back(0);
	vy.push_back(m - 1);
	sort(all(vx)); vx.erase(unique(all(vx)), vx.end());
	sort(all(vy)); vy.erase(unique(all(vy)), vy.end());
	vector<vector<bool>> wall(vx.size(), vector<bool>(vy.size()));
	vector<vector<bool>> canr(vx.size(), vector<bool>(vy.size()));
	rep(i, p.size()) {
		int x = p[i].first;
		int y = p[i].second;
		x = lower_bound(all(vx), x) - vx.begin();
		y = lower_bound(all(vy), y) - vy.begin();
		wall[x][y] = true;
	}
	canr[0][0] = true;
	rep(i, vx.size())rep(j, vy.size()) {
		if (!canr[i][j])continue;
		if (i + 1 < vx.size() && !wall[i + 1][j])canr[i + 1][j] = true;
		if (j + 1 < vy.size() && !wall[i][j + 1])canr[i][j + 1] = true;
	}
	return canr[vx.size() - 1][vy.size() - 1];
}
modint calc(int n, int m, vector<P> p) {
	sort(all(p));
	vector<vector<modint>> dp(p.size(),vector<modint>(2));
	modint ans = comb(n - 1 + m - 1, n - 1);
	rep(i, dp.size()) {
		int x = p[i].first;
		int y = p[i].second;
		dp[i][1] += comb(x + y, x);
		rep(j, i) {
			int px = p[j].first;
			int py = p[j].second;
			if (px <= x && py <= y) {
				rep(k, 2) {
					dp[i][k ^ 1] += dp[j][k] * comb(x - px + y - py, x - px);
				}
			}
		}
		ans += dp[i][0] * comb(n - 1 - x + m - 1 - y, n - 1 - x);
		ans -= dp[i][1] * comb(n - 1 - x + m - 1 - y, n - 1 - x);
	}
	return ans;
}
void solve() {
	int n, m; cin >> n >> m;
	vector<P> p(m);
	set<P> st;
	vector<P> vs;
	rep(i, m) {
		int x, y; cin >> x >> y; x--; y--;
		p[i] = { x,y };
		st.insert(p[i]);
		rep(d, 4) {
			int nx = x + dx[d];
			int ny = y + dy[d];
			if (nx < 0 || nx >= n || ny < 0 || ny >= n)continue;
			vs.push_back({ nx,ny });
		}
	}
	{
		vs.push_back({ 0,0 });
		vs.push_back({ n - 1,n - 1 });
		vector<P> nvs;
		rep(i, vs.size()) {
			if (st.count(vs[i]))continue;
			nvs.push_back(vs[i]);
		}
		sort(all(nvs));
		nvs.erase(unique(all(nvs)), nvs.end());
		swap(vs, nvs);
	}
	vector<pair<int, modint>> dp(vs.size(), { mod,0 });
	dp[0] = { 0,1 };
	priority_queue<P, vector<P>, greater<P>> q;
	q.push({ 0,0 });
	vector<P> bans;
	for (P p : vs)bans.push_back(p);
	for (P p : st)bans.push_back(p);
	while (!q.empty()) {
		P p = q.top(); q.pop();
		int id = p.second;
		if (dp[id].first < p.first)continue;
		//cout << id << "\n";
		rep(to, vs.size())if(id!=to) {
			int x1 = vs[id].first;
			int y1 = vs[id].second;
			int x2 = vs[to].first;
			int y2 = vs[to].second;
			int cost = abs(x1 - x2) + abs(y1 - y2);
			if (x1 > x2) {
				swap(x1, x2);
				swap(y1, y2);
			}
			int ly = min(y1, y2);
			int ry = max(y1, y2);
			vector<P> np;
			for (P p : bans) {
				if (p == P{ x1,y1 } || p == P{ x2,y2 })continue;
				if (x1 <= p.first && p.first <= x2 && ly <= p.second && p.second <= ry) {
					np.push_back({ p.first - x1,p.second - y1 });
				}
			}
			if (y1 > y2) {
				for (P& p : np)p.second *= -1;
			}
			if (exiroad(x2 - x1 + 1, ry - ly + 1, np)) {
				modint num = calc(x2 - x1 + 1, ry - ly + 1, np);
				int nd = dp[id].first + abs(x2 - x1) + abs(y2 - y1);
				if (nd < dp[to].first) {
					dp[to] = { nd,num * dp[id].second };
					q.push({ nd,to });
				}
				else if (nd == dp[to].first) {
					dp[to].second += num * dp[id].second;
				}
			}
		}
	}
	cout << dp.back().second << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
		solve();
	return 0;
}
accuracy: 1
time_ms: 770
memory_kb: 69116
final_rank: 9

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 2020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
//const int mod = 998244353;

const int mod = 1e9 + 9;

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll 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;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

bool in(ll y, ll x, ll h, ll w){
    return 0 <= y && y < h && 0 <= x && x < w;
}

void calc1(int n, vvl &d, vector<vector<mint>> &dp, vector<vb> &b){
    dp[0][0] = 1;
    d[0][0] = 1;
    queue<P> q;
    q.emplace(0,0);
    while(!q.empty()){
        auto p = q.front(); q.pop();
        int y = p.first, x = p.second;
        rep(i,4){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(!in(ny,nx,n,n) || d[ny][nx] < d[y][x] + 1 || b[ny][nx]) continue;
            if(chmin(d[ny][nx], d[y][x] + 1)){
                q.emplace(ny,nx);
            }
            dp[ny][nx] += dp[y][x];
        }
    }
}

struct st{
    int x,y;
    mint num;
};

int main(){
    int n,m; cin >> n >> m;
    vl x(m),y(m);
    rep(i,m) cin >> x[i] >> y[i], x[i]--, y[i]--;
    if(n <= 405){
        vvl d(n,vl(n,inf));
        vector<vector<mint>> dp(n,vector<mint>(n,0));
        vector<vb> b(n,vb(n,false));
        rep(i,m) b[x[i]][y[i]] = 1;
        calc1(n,d,dp,b);
        cout << dp[n-1][n-1] << endl;
    }else{
        comInit();
        vpl v;
        vvl d(100,vl(100,inf)),d2(100,vl(100,inf));
        vector<vector<mint>> ep(101,vector<mint>(101,0)), ep2(101,vector<mint>(101,0));
        vector<vb> b(101,vb(101,false)), b2(101,vb(101,false));
        rep(i,m){
            if(x[i] < 100 && y[i] < 100) b[x[i]][y[i]] = 1;
            else if(n-1-x[i] < 100 && n-1-y[i] < 100) b2[n-1-x[i]][n-1-y[i]] = 1;
            else v.emplace_back(x[i],y[i]);
            if((x[i] == 100 || y[i] == 100) && (x[i] <= 100 && y[i] <= 100)) b[x[i]][y[i]] = 1;
            if((n-1-x[i] == 100 || n-1-y[i] == 100) && (n-1-x[i] <= 100 && n-1-y[i] <= 100)) b2[n-1-x[i]][n-1-y[i]] = 1;
        }
        calc1(100,d,ep,b);
        calc1(100,d2,ep2,b2);
        vector<st> stlist;
        int mn = inf;
        rep(i,100){
            chmin(mn, d[i][99] + n-1-i + n-1-99);
            chmin(mn, d[99][i] + n-1-i + n-1-99);
        }
        if(mn == inf){
            cout << 0 << endl;
            return 0;
        }
        rep(i,100){
            if(d[i][99] + n-1-i + n-1-99 == mn && !b[i][100]){
                stlist.push_back(st{i,100,ep[i][99]});
            }
            if(d[99][i] + n-1-i + n-1-99 == mn && !b[100][i]){
                stlist.push_back(st{100,i,ep[99][i]});
            }
        }
        mn = inf;
        rep(i,100){
            chmin(mn, d2[i][99] - i);
            chmin(mn, d2[99][i] - i);
        }
        if(mn == inf){
            cout << 0 << endl;
            return 0;
        }
        map<P,mint> mp;
        rep(i,100){
            // if(d2[i][99] - i == mn && !b2[i][100]){
            //     v.emplace_back(n-1-i,n-101);
            //     mp[P(n-1-i,n-101)] = ep2[i][99];
            // }
            // if(d2[99][i] - i == mn && !b2[100][i]){
            //     v.emplace_back(n-101,n-1-i);
            //     mp[P(n-101,n-1-i)] = ep2[99][i];
            // }
            if(d2[i][99] - i == mn){
                v.emplace_back(n-1-i,n-100);
                mp[P(n-1-i,n-100)] = ep2[i][99];
            }
            if(d2[99][i] - i == mn){
                v.emplace_back(n-100,n-1-i);
                mp[P(n-100,n-1-i)] = ep2[99][i];
            }

        }
        sort(all(v));
        m = v.size();
        int K = stlist.size();
        vector<vector<mint>> dp(m,vector<mint>(K,0));
        mint ans = 0;
        rep(i,m){
            int x = v[i].first, y = v[i].second;
            rep(j,K){
                auto e = stlist[j];
                if(x < e.x || y < e.y) continue;
                dp[i][j] += e.num * com(x-e.x+y-e.y,x-e.x);
                rep(k,i){
                    if(v[k].second > y) continue;
                    dp[i][j] -= dp[k][j] * com(x-v[k].first+y-v[k].second, x-v[k].first);
                }
            }
            if(mp.count(P(x,y))) rep(j,K) ans += dp[i][j] * mp[P(x,y)];
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 50
memory_kb: 51264
final_rank: 2

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 2020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
//const int mod = 998244353;

const int mod = 1e9 + 9;

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll 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;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

bool in(ll y, ll x, ll h, ll w){
    return 0 <= y && y < h && 0 <= x && x < w;
}

void calc1(int n, vvl &d, vector<vector<mint>> &dp, vector<vb> &b){
    dp[0][0] = 1;
    d[0][0] = 1;
    queue<P> q;
    q.emplace(0,0);
    while(!q.empty()){
        auto p = q.front(); q.pop();
        int y = p.first, x = p.second;
        rep(i,4){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(!in(ny,nx,n,n) || d[ny][nx] < d[y][x] + 1 || b[ny][nx]) continue;
            if(chmin(d[ny][nx], d[y][x] + 1)){
                q.emplace(ny,nx);
            }
            dp[ny][nx] += dp[y][x];
        }
    }
}

struct st{
    int x,y;
    mint num;
};

int main(){
    int n,m; cin >> n >> m;
    vl x(m),y(m);
    rep(i,m) cin >> x[i] >> y[i], x[i]--, y[i]--;
    if(n <= 405){
        vvl d(n,vl(n,inf));
        vector<vector<mint>> dp(n,vector<mint>(n,0));
        vector<vb> b(n,vb(n,false));
        rep(i,m) b[x[i]][y[i]] = 1;
        calc1(n,d,dp,b);
        cout << dp[n-1][n-1] << endl;
    }else{
        comInit();
        vpl v;
        vvl d(100,vl(100,inf)),d2(100,vl(100,inf));
        vector<vector<mint>> ep(101,vector<mint>(101,0)), ep2(101,vector<mint>(101,0));
        vector<vb> b(101,vb(101,false)), b2(101,vb(101,false));
        rep(i,m){
            if(x[i] < 100 && y[i] < 100) b[x[i]][y[i]] = 1;
            else if(n-1-x[i] < 100 && n-1-y[i] < 100) b2[n-1-x[i]][n-1-y[i]] = 1;
            else v.emplace_back(x[i],y[i]);
            if((x[i] == 100 || y[i] == 100) && (x[i] <= 100 && y[i] <= 100)) b[x[i]][y[i]] = 1;
            if((n-1-x[i] == 100 || n-1-y[i] == 100) && (n-1-x[i] <= 100 && n-1-y[i] <= 100)) b2[n-1-x[i]][n-1-y[i]] = 1;
        }
        calc1(100,d,ep,b);
        calc1(100,d2,ep2,b2);
        vector<st> stlist;
        int mn = inf;
        rep(i,100){
            chmin(mn, d[i][99] + n-1-i + n-1-99);
            chmin(mn, d[99][i] + n-1-i + n-1-99);
        }
        if(mn == inf){
            cout << 0 << endl;
            return 0;
        }
        rep(i,100){
            if(d[i][99] + n-1-i + n-1-99 == mn && !b[i][100]){
                stlist.push_back(st{i,100,ep[i][99]});
            }
            if(d[99][i] + n-1-i + n-1-99 == mn && !b[100][i]){
                stlist.push_back(st{100,i,ep[99][i]});
            }
        }
        mn = inf;
        rep(i,100){
            chmin(mn, d2[i][99] - i);
            chmin(mn, d2[99][i] - i);
        }
        if(mn == inf){
            cout << 0 << endl;
            return 0;
        }
        map<P,mint> mp;
        rep(i,100){
            // if(d2[i][99] - i == mn && !b2[i][100]){
            //     v.emplace_back(n-1-i,n-101);
            //     mp[P(n-1-i,n-101)] = ep2[i][99];
            // }
            // if(d2[99][i] - i == mn && !b2[100][i]){
            //     v.emplace_back(n-101,n-1-i);
            //     mp[P(n-101,n-1-i)] = ep2[99][i];
            // }
            if(d2[i][99] - i == mn){
                v.emplace_back(n-1-i,n-100);
                mp[P(n-1-i,n-100)] = ep2[i][99];
            }
            if(d2[99][i] - i == mn){
                v.emplace_back(n-100,n-1-i);
                mp[P(n-100,n-1-i)] = ep2[99][i];
            }

        }
        m = v.size();
        int K = stlist.size();
        vector<vector<mint>> dp(m,vector<mint>(K,0));
        mint ans = 0;
        rep(i,m){
            int x = v[i].first, y = v[i].second;
            rep(j,K){
                auto e = stlist[j];
                if(x < e.x || y < e.y) continue;
                dp[i][j] += e.num * com(x-e.x+y-e.y,x-e.x);
                rep(k,i){
                    if(v[k].second > y) continue;
                    dp[i][j] -= dp[k][j] * com(x-v[k].first+y-v[k].second, x-v[k].first);
                }
            }
            if(mp.count(P(x,y))) rep(j,K) ans += dp[i][j] * mp[P(x,y)];
        }
        cout << ans << endl;
    }
}
accuracy: 0.05357142857142857
time_ms: 40
memory_kb: 50852
final_rank: 19

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 2020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
//const int mod = 998244353;

const int mod = 1e9 + 9;

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll 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;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

bool in(ll y, ll x, ll h, ll w){
    return 0 <= y && y < h && 0 <= x && x < w;
}

void calc1(int n, vvl &d, vector<vector<mint>> &dp, vector<vb> &b){
    dp[0][0] = 1;
    d[0][0] = 1;
    queue<P> q;
    q.emplace(0,0);
    while(!q.empty()){
        auto p = q.front(); q.pop();
        int y = p.first, x = p.second;
        rep(i,4){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(!in(ny,nx,n,n) || d[ny][nx] < d[y][x] + 1 || b[ny][nx]) continue;
            if(chmin(d[ny][nx], d[y][x] + 1)){
                q.emplace(ny,nx);
            }
            dp[ny][nx] += dp[y][x];
        }
    }
}

struct st{
    int x,y;
    mint num;
};

int main(){
    int n,m; cin >> n >> m;
    vl x(m),y(m);
    rep(i,m) cin >> x[i] >> y[i], x[i]--, y[i]--;
    if(n <= 105){
        vvl d(n,vl(n,inf));
        vector<vector<mint>> dp(n,vector<mint>(n,0));
        vector<vb> b(n,vb(n,false));
        rep(i,m) b[x[i]][y[i]] = 1;
        calc1(n,d,dp,b);
        cout << dp[n-1][n-1] << endl;
    }else{
        comInit();
        vpl v;
        vvl d(100,vl(100,inf));
        vector<vector<mint>> ep(101,vector<mint>(101,0));
        vector<vb> b(101,vb(101,false));
        rep(i,m){
            if(x[i] < 100 && y[i] < 100) b[x[i]][y[i]] = 1;
            else v.emplace_back(x[i],y[i]);
            if(x[i] == 100 || y[i] == 100) b[x[i]][y[i]] = 1;
        }
        v.emplace_back(n-1,n-1);
        sort(all(v));
        calc1(100,d,ep,b);
        vector<st> stlist;
        int mn = inf;
        rep(i,100){
            chmin(mn, d[i][99] + n-1-i + n-1-99);
            chmin(mn, d[99][i] + n-1-i + n-1-99);
        }
        if(mn == inf){
            cout << 0 << endl;
            return 0;
        }
        rep(i,100){
            if(d[i][99] + n-1-i + n-1-99 == mn && !b[i][100]){
                //if(i==99) stlist.push_back(st{i,99,ep[i][99]});
                stlist.push_back(st{i,100,ep[i][99]});
            }
            if(d[99][i] + n-1-i + n-1-99 == mn && !b[100][i]){
                //if(i<99)
                    stlist.push_back(st{100,i,ep[99][i]});
            }
        }
        m = v.size();
        int K = stlist.size();
        vector<vector<mint>> dp(m,vector<mint>(K,0));
        rep(i,m){
            int x = v[i].first, y = v[i].second;
            rep(j,K){
                auto e = stlist[j];
                if(x < e.x || y < e.y) continue;
                dp[i][j] += e.num * com(x-e.x+y-e.y,x-e.x);
                rep(k,i){
                    if(v[k].second > y) continue;
                    dp[i][j] -= dp[k][j] * com(x-v[k].first+y-v[k].second, x-v[k].first);
                }
            }
        }
        mint ans = 0;
        for(auto e : dp[m-1]) ans += e;
        cout << ans << endl;
    }
}
accuracy: 0.5714285714285714
time_ms: 30
memory_kb: 50524
final_rank: 12

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 2020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
//const int mod = 998244353;

const int mod = 1e9 + 9;

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll 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;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

bool in(ll y, ll x, ll h, ll w){
    return 0 <= y && y < h && 0 <= x && x < w;
}

int main(){
    int n,m; cin >> n >> m;
    vl x(m),y(m);
    rep(i,m) cin >> x[i] >> y[i], x[i]--, y[i]--;
    if(n <= 100){
        vvl d(n,vl(n,inf));
        vector<vector<mint>> dp(n,vector<mint>(n,0));
        vector<vb> b(n,vb(n,false));
        rep(i,m) b[x[i]][y[i]] = 1;
        dp[0][0] = 1;
        d[0][0] = 1;
        queue<P> q;
        q.emplace(0,0);
        while(!q.empty()){
            auto p = q.front(); q.pop();
            int y = p.first, x = p.second;
            rep(i,4){
                int ny = y + dy[i];
                int nx = x + dx[i];
                if(!in(ny,nx,n,n) || d[ny][nx] < d[y][x] + 1 || b[ny][nx]) continue;
                if(chmin(d[ny][nx], d[y][x] + 1)){
                    q.emplace(ny,nx);
                }
                dp[ny][nx] += dp[y][x];
            }
        }
        cout << dp[n-1][n-1] << endl;
    }else{
        comInit();
        vpl v(m);
        rep(i,m) v[i] = {x[i],y[i]};
        sort(all(v));
        v.emplace_back(n-1,n-1);
        m++;
        vector<mint> dp(m,0);
        rep(i,m){
            int x = v[i].first, y = v[i].second;
            dp[i] = com(x+y,x);
            rep(j,i){
                if(v[j].second > y) continue;
                dp[i] -= dp[j] * com(x-v[j].first+y-v[j].second, x-v[j].first);
            }
        }
        cout << dp[m-1] << endl;
    }
}
accuracy: 0.10714285714285714
time_ms: 30
memory_kb: 50392
final_rank: 16

code_snippet: /*	author:  Kite_kuma
	created: 2021.09.22 14:29:26 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007 + 2;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

#pragma region binomial_coefficient

// normal binomial coefficient
vector<vector<long long>> binom_memo(100, vector<long long>(100, -1));
long long binom(int n, int k) {
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(binom_memo[n][k] != -1) return binom_memo[n][k];
	long long res;
	if(n - k < k)
		res = binom(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = binom(n - 1, k - 1) + binom(n - 1, k);
	binom_memo[n][k] = res;
	return res;
}

vector<long long> fac, finv, inv;
struct mod_factorial {
	const int MAX;	// time: 300ms, when MAX = 3*10^7, time: 1900ms
	mod_factorial(const int MAX_ = 5100000) : MAX(MAX_) {
		fac.resize(MAX), finv.resize(MAX), inv.resize(MAX);
		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;
		}
	}
} mod_factorial_;

long long com_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long permutation_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}

// patterns of n of undistinguished balls in k of distinguished boxes
long long multi_choose_mod(long long n, long long k) { return com_mod(n + k - 1, n); }

#pragma endregion

mint solve(int n) {
	int m;
	cin >> m;
	vector<int> x(m), y(m);
	rep(i, m) {
		cin >> x[i] >> y[i];
		x[i]--, y[i]--;
	}

	if(n < 5000) {
		VV<mint> grid(n, V<mint>(n));
		VV<int> cost(n, vi(n, inf));
		grid[0][0] = 1;
		cost[0][0] = 0;

		vvi obj(n, vi(n));
		rep(i, m) obj[x[i]][y[i]] = 1;

		queue<pair<int, int>> q;
		q.emplace(0, 0);

		while(!q.empty()) {
			int x, y;
			tie(x, y) = q.front();
			q.pop();
			if(x == n - 1 && x == y) return grid[x][y];
			int c = cost[x][y];
			for(int dir = 0; dir < 4; dir++) {
				int u = x + dx[dir];
				int v = y + dy[dir];
				if(u < 0 || u >= n || v < 0 || v >= n) continue;
				if(obj[u][v]) continue;
				if(chmin(cost[u][v], c + 1)) {
					grid[u][v] = grid[x][y];
					q.emplace(u, v);
				} else if(cost[u][v] == c + 1) {
					grid[u][v] += grid[x][y];
				}
			}
		}
		return 0;
	}

	vector<pair<int, int>> xy(m);
	rep(i, m) xy[i] = make_pair(x[i], y[i]);
	xy.emplace_back(0, 0);
	xy.emplace_back(n - 1, n - 1);
	sort(all(xy));

	int sz = xy.size();
	V<mint> res(sz);
	res[0] = -1;

	auto path = [&](int x, int y) -> mint {
		if(x < 0 || y < 0) return mint(0);
		return com_mod(x + y, x);
	};

	rep(i, sz - 1) {
		rep(j, i + 1, sz) { res[j] -= res[i] * path(xy[j].first - xy[i].first, xy[j].second - xy[i].second); }
	}

	return res.back();
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n) {
		cout << solve(n) << dl;
	}

	return 0;
}
accuracy: 0.6785714285714286
time_ms: 170
memory_kb: 166528
final_rank: 10

code_snippet: /*	author:  Kite_kuma
	created: 2021.09.22 14:29:26 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007 + 2;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

#pragma region binomial_coefficient

// normal binomial coefficient
vector<vector<long long>> binom_memo(100, vector<long long>(100, -1));
long long binom(int n, int k) {
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(binom_memo[n][k] != -1) return binom_memo[n][k];
	long long res;
	if(n - k < k)
		res = binom(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = binom(n - 1, k - 1) + binom(n - 1, k);
	binom_memo[n][k] = res;
	return res;
}

vector<long long> fac, finv, inv;
struct mod_factorial {
	const int MAX;	// time: 300ms, when MAX = 3*10^7, time: 1900ms
	mod_factorial(const int MAX_ = 5100000) : MAX(MAX_) {
		fac.resize(MAX), finv.resize(MAX), inv.resize(MAX);
		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;
		}
	}
} mod_factorial_;

long long com_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long permutation_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}

// patterns of n of undistinguished balls in k of distinguished boxes
long long multi_choose_mod(long long n, long long k) { return com_mod(n + k - 1, n); }

#pragma endregion

mint solve(int n) {
	int m;
	cin >> m;
	vector<int> x(m), y(m);
	rep(i, m) {
		cin >> x[i] >> y[i];
		x[i]--, y[i]--;
	}

	if(n < 5000) {
		VV<mint> grid(n, V<mint>(n));
		VV<int> cost(n, vi(n, inf));
		grid[0][0] = 1;
		cost[0][0] = 0;

		vvi obj(n, vi(n));
		rep(i, m) obj[x[i]][y[i]] = 1;

		queue<pair<int, int>> q;
		q.emplace(0, 0);

		while(!q.empty()) {
			int x, y;
			tie(x, y) = q.front();
			q.pop();
			if(x == n - 1 && x == y) return grid[x][y];
			int c = cost[x][y];
			for(int dir = 0; dir < 4; dir++) {
				int u = x + dx[dir];
				int v = y + dy[dir];
				if(u < 0 || u >= n || v < 0 || v >= n) continue;
				if(obj[u][v]) continue;
				if(chmin(cost[u][v], c + 1)) {
					grid[u][v] = grid[x][y];
					q.emplace(u, v);
				} else if(cost[u][v] == c + 1) {
					grid[u][v] += grid[x][y];
				}
			}
		}
		return 0;
	}

	vector<pair<int, int>> xy(m);
	rep(i, m) xy[i] = make_pair(x[i], y[i]);
	xy.emplace_back(0, 0);
	xy.emplace_back(n - 1, n - 1);
	sort(all(xy));

	int sz = xy.size();
	V<mint> res(sz);
	res[0] = -1;

	auto path = [&](int x, int y) -> mint {
		if(x < 0 && y < 0) return mint(0);
		return com_mod(x + y, x);
	};

	rep(i, sz - 1) {
		rep(j, i + 1, sz) { res[j] -= res[i] * path(xy[j].first - xy[i].first, xy[j].second - xy[i].second); }
	}

	return res.back();
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n) {
		cout << solve(n) << dl;
	}

	return 0;
}
accuracy: 0.6785714285714286
time_ms: 180
memory_kb: 166596
final_rank: 11

code_snippet: /*	author:  Kite_kuma
	created: 2021.09.22 14:29:26 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007 + 2;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

#pragma region binomial_coefficient

// normal binomial coefficient
vector<vector<long long>> binom_memo(100, vector<long long>(100, -1));
long long binom(int n, int k) {
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(binom_memo[n][k] != -1) return binom_memo[n][k];
	long long res;
	if(n - k < k)
		res = binom(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = binom(n - 1, k - 1) + binom(n - 1, k);
	binom_memo[n][k] = res;
	return res;
}

vector<long long> fac, finv, inv;
struct mod_factorial {
	const int MAX;	// time: 300ms, when MAX = 3*10^7, time: 1900ms
	mod_factorial(const int MAX_ = 5100000) : MAX(MAX_) {
		fac.resize(MAX), finv.resize(MAX), inv.resize(MAX);
		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;
		}
	}
} mod_factorial_;

long long com_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long permutation_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}

// patterns of n of undistinguished balls in k of distinguished boxes
long long multi_choose_mod(long long n, long long k) { return com_mod(n + k - 1, n); }

#pragma endregion

mint solve(int n) {
	int m;
	cin >> m;
	vector<int> x(m), y(m);
	rep(i, m) {
		cin >> x[i] >> y[i];
		x[i]--, y[i]--;
	}

	if(n < 300) {
		VV<mint> grid(n, V<mint>(n));
		VV<int> cost(n, vi(n, inf));
		grid[0][0] = 1;
		cost[0][0] = 0;

		vvi obj(n, vi(n));
		rep(i, m) obj[x[i]][y[i]] = 1;

		queue<pair<int, int>> q;
		q.emplace(0, 0);

		while(!q.empty()) {
			int x, y;
			tie(x, y) = q.front();
			q.pop();
			if(x == n - 1 && x == y) return grid[x][y];
			int c = cost[x][y];
			for(int dir = 0; dir < 4; dir++) {
				int u = x + dx[dir];
				int v = y + dy[dir];
				if(u < 0 || u >= n || v < 0 || v >= n) continue;
				if(obj[u][v]) continue;
				if(chmin(cost[u][v], c + 1)) {
					grid[u][v] = grid[x][y];
					q.emplace(u, v);
				} else if(cost[u][v] == c + 1) {
					grid[u][v] += grid[x][y];
				}
			}
		}
		return 0;
	}

	vector<pair<int, int>> xy(m);
	rep(i, m) xy[i] = make_pair(x[i], y[i]);
	xy.emplace_back(0, 0);
	xy.emplace_back(n - 1, n - 1);
	sort(all(xy));

	int sz = xy.size();
	V<mint> res(sz);
	res[0] = -1;

	auto path = [&](int x, int y) -> mint {
		if(x < 0 && y < 0) return mint(0);
		return com_mod(x + y, x);
	};

	rep(i, sz - 1) {
		rep(j, i + 1, sz) { res[j] -= res[i] * path(xy[j].first - xy[i].first, xy[j].second - xy[i].second); }
	}

	return res.back();
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n) {
		cout << solve(n) << dl;
	}

	return 0;
}
accuracy: 0.10714285714285714
time_ms: 90
memory_kb: 122624
final_rank: 18

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define drep(i,n) for(ll i = n-1;i >= 0;i--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define endl '\n'
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)\
    vector<type> name(size);\
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)\
    vector<vector<type>> name(h, vector<type>(w));\
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)\
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)\
    int __VA_ARGS__;\
    IN(__VA_ARGS__)
#define LL(...)\
    ll __VA_ARGS__;\
    IN(__VA_ARGS__)
#define STR(...)\
    string __VA_ARGS__;\
    IN(__VA_ARGS__)
#define CHR(...)\
    char __VA_ARGS__;\
    IN(__VA_ARGS__)
#define DBL(...)\
    double __VA_ARGS__;\
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
template <typename T> vi iota(vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(all(res), 0);
    sort(all(res), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}
#define i128 __int128_t
#define ull unsigned long long int
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" :", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#define LOCAL
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#pragma endregion
//#include<atcoder/all>
//using namespace atcoder;
const int mod = 1000000009;
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
vector<ll> fact,ifact;
void init(int n){
    fact.resize(n);
    ifact.resize(n);
    fact[0] = 1;
    for(int i = 1;i < n;i++)fact[i] = (fact[i-1]*i)%mod;
    rep(i,n)ifact[i] = modpow(fact[i],mod-2,mod);
}
void add(ll &a,ll b){
    a = (a+b)%mod;
    a = (a%mod + mod)%mod;
}
void mul(ll &a,ll b){
    a = (a*b)%mod;
}
ll C(ll x,ll y){
    if(x < 0)return 0;
    if(y < 0)return 0;
    if(x < y)return 0;
    ll ret = fact[x];
    mul(ret,ifact[y]);
    mul(ret,ifact[x-y]);
    return ret;
}
int main(){
    INT(n,m);
    init(110000);
    vector<pii> p;
    rep(i,m){
        INT(x,y);
        p.pb({x,y});
    }
    ll ret = 0;
    sort(all(p));
    rep(mask,1<<m){
        int my = -1;
        int px = 1,py = 1;
        bool ok = true;
        ll tmp = 1;
        rep(i,m){
            int x = p[i].first,y = p[i].second;
            if(mask>>i&1){
                if(my > y)ok = false;
                else{
                    chmax(my,y);
                    mul(tmp,C(x-px+y-py,x-px));
                    px = x;
                    py = y;
                }
            }
        }
        if(!ok)continue;
        
        mul(tmp,C(n-px+n-py,n-px));
        // trace(mask,tmp);
        if(popcount(mask)%2 == 0)add(ret,tmp);
        else add(ret,-tmp);
    }
    cout << ret << endl;
    return 0;
}
accuracy: 0.03571428571428571
time_ms: 10
memory_kb: 4540
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 9;

ll modset(ll x) { return ((x % MOD) + MOD) % MOD; }
ll modpow(ll x, ll n) {
    x %= MOD;
    ll ret = 1;
    while (n) {
        if (n & 1ll)
            ret = modset(ret * x);
        x = modset(x * x);
        n >>= 1;
    }
    return ret;
}
ll modinv(ll x) { return modpow(x, MOD - 2); }

int main() {
    const int LEN = 3e6;
    vector<ll> fac(LEN), ifac(LEN);
    fac[0] = 1;
    for (ll i = 1; i < LEN; i++)
        fac[i] = modset(fac[i - 1] * i);
    ifac[LEN - 1] = modinv(fac[LEN - 1]);
    for (ll i = LEN - 1; i >= 1; i--)
        ifac[i - 1] = modset(ifac[i] * i);
    auto C = [&](int n, int k) {
        if (n < k || k < 0)
            return 0ll;
        return modset(fac[n] * modset(ifac[k] * ifac[n - k]));
    };

    int N, M;
    cin >> N >> M;
    if (N > 60) {
        vector<pair<int, int>> obst, obst_rev;
        for (int i = 0; i < M; i++) {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            obst.push_back({x, y});
            obst_rev.push_back({N - y - 1, N - x - 1});
        }
        int H = 60;

        auto calc_to_diag = [](int H, vector<pair<int, int>> obst) {
            vector can(H, vector(H, true));
            for (auto& [x, y] : obst) {
                if (x < H && y < H)
                    can[x][y] = false;
            }
            vector seen(H, vector(H, false));
            vector now(H, vector(H, 0ll));
            now[0][0] = 1;
            seen[0][0] = true;
            int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
            auto check = [&](int x, int y) {
                if (x < 0 || x >= H || y < 0 || y >= H)
                    return false;
                return (bool)can[x][y];
            };
            for (int a = 0; a < H * H; a++) {
                vector nxt(H, vector(H, 0ll));
                auto nxt_seen = seen;
                for (int x = 0; x < H; x++) {
                    for (int y = 0; y < H; y++) {
                        if (seen[x][y]) {
                            for (int i = 0; i < 4; i++) {
                                int nxt_x = x + dx[i], nxt_y = y + dy[i];
                                if (check(nxt_x, nxt_y)) {
                                    nxt_seen[nxt_x][nxt_y] = true;
                                    auto& tmp = nxt[nxt_x][nxt_y];
                                    tmp = modset(tmp + now[x][y]);
                                }
                            }
                        }
                    }
                }
                bool flag = false;
                for (int i = 0; i < H; i++) {
                    flag |= nxt_seen[i][H - i - 1];
                }
                if (flag) {
                    vector<ll> ret(H);
                    for (int i = 0; i < H; i++) {
                        ret[i] = nxt[i][H - i - 1];
                    }
                    return ret;
                }
                now = nxt;
                seen = nxt_seen;
            }
            cout << 0 << endl;
            exit(0);
        };

        auto V = calc_to_diag(H, obst);
        auto V_rev = calc_to_diag(H, obst_rev);
        vector<pair<int, int>> place;
        for (int i = 0; i < H; i++) {
            place.push_back({i, H - i - 1});
        }
        sort(obst.begin(), obst.end(), [](pair<int, int> a, pair<int, int> b) { return a.first + a.second < b.first + b.second; });
        for (auto& [x, y] : obst) {
            if (x + y >= H && x + y < N * 2 - H - 1) {
                place.push_back({x, y});
            }
        }
        for (int i = 0; i < H; i++) {
            place.push_back({N - H + i, N - i - 1});
        }
        V.resize(place.size());
        for (int i = 0; i < V.size(); i++) {
            for (int j = i + 1; j < V.size(); j++) {
                auto a = place[i], b = place[j];
                ll dx = b.first - a.first, dy = b.second - a.second;
                if (dx >= 0 && dy >= 0) {
                    V[j] = modset(V[j] - V[i] * C(dx + dy, dx));
                }
            }
        }
        ll ans = 0;
        for (int i = 0; i < H; i++) {
            ans = modset(ans + V_rev[i] * V[V.size() - H + i]);
        }
        cout << modset(-ans) << endl;
    } else {
        vector can(N, vector(N, true));
        for (int i = 0; i < M; i++) {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            can[x][y] = 0;
        }
        vector seen(N, vector(N, false));
        vector now(N, vector(N, 0ll));
        now[0][0] = 1;
        seen[0][0] = true;
        int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
        auto check = [&](int x, int y) {
            if (x < 0 || x >= N || y < 0 || y >= N)
                return false;
            return (bool)can[x][y];
        };
        for (int a = 0; a < N * N; a++) {
            vector nxt(N, vector(N, 0ll));
            auto nxt_seen = seen;
            for (int x = 0; x < N; x++) {
                for (int y = 0; y < N; y++) {
                    if (seen[x][y]) {
                        for (int i = 0; i < 4; i++) {
                            int nxt_x = x + dx[i], nxt_y = y + dy[i];
                            if (check(nxt_x, nxt_y)) {
                                nxt_seen[nxt_x][nxt_y] = true;
                                auto& tmp = nxt[nxt_x][nxt_y];
                                tmp = modset(tmp + now[x][y]);
                            }
                        }
                    }
                }
            }
            if (nxt_seen[N - 1][N - 1]) {
                cout << nxt[N - 1][N - 1] << endl;
                return 0;
            }
            now = nxt;
            seen = nxt_seen;
        }
        cout << 0 << endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 50028
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 9;

ll modset(ll x) { return ((x % MOD) + MOD) % MOD; }
ll modpow(ll x, ll n) {
    x %= MOD;
    ll ret = 1;
    while (n) {
        if (n & 1ll)
            ret = modset(ret * x);
        x = modset(x * x);
        n >>= 1;
    }
    return ret;
}
ll modinv(ll x) { return modpow(x, MOD - 2); }

int main() {
    const int LEN = 3e6;
    vector<ll> fac(LEN), ifac(LEN);
    fac[0] = 1;
    for (ll i = 1; i < LEN; i++)
        fac[i] = modset(fac[i - 1] * i);
    ifac[LEN - 1] = modinv(fac[LEN - 1]);
    for (ll i = LEN - 1; i >= 1; i--)
        ifac[i - 1] = modset(ifac[i] * i);
    auto C = [&](int n, int k) {
        if (n < k || k < 0)
            return 0ll;
        return modset(fac[n] * modset(ifac[k] * ifac[n - k]));
    };

    int N, M;
    cin >> N >> M;


    if (N > 50) {
        vector<pair<int, int>> V;
        V.push_back({1, 1});
        for (int i = 0; i < M; i++) {
            int x, y;
            cin >> x >> y;
            V.push_back({x, y});
        }
        V.push_back({N, N});
        sort(V.begin(), V.end(), [](pair<int, int> a, pair<int, int> b) { return a.first + a.second < b.first + b.second; });
        M += 2;
        vector<ll> data(M);
        data[0] = MOD - 1;
        for (int i = 0; i < M; i++) {
            for (int j = i + 1; j < M; j++) {
                auto a = V[i], b = V[j];
                ll dx = b.first - a.first, dy = b.second - a.second;
                if (dx >= 0 && dy >= 0) {
                    data[j] = modset(data[j] - data[i] * C(dx + dy, dx));
                }
            }
        }
        cout << data.back() << endl;
    } else {
        vector can(N, vector(N, true));
        for (int i = 0; i < M; i++) {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            can[x][y] = 0;
        }
        vector seen(N, vector(N, false));
        vector now(N, vector(N, 0ll));
        now[0][0] = 1;
        seen[0][0] = true;
        int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
        auto check = [&](int x, int y) {
            if (x < 0 || x >= N || y < 0 || y >= N)
                return false;
            return (bool)can[x][y];
        };
        for (int a = 0; a < N * N; a++) {
            vector nxt(N, vector(N, 0ll));
            auto nxt_seen = seen;
            for (int x = 0; x < N; x++) {
                for (int y = 0; y < N; y++) {
                    if (seen[x][y]) {
                        for (int i = 0; i < 4; i++) {
                            int nxt_x = x + dx[i], nxt_y = y + dy[i];
                            if (check(nxt_x, nxt_y)) {
                                nxt_seen[nxt_x][nxt_y] = true;
                                auto& tmp = nxt[nxt_x][nxt_y];
                                tmp = modset(tmp + now[x][y]);
                            }
                        }
                    }
                }
            }
            if (nxt_seen[N - 1][N - 1]) {
                cout << nxt[N - 1][N - 1] << endl;
                return 0;
            }
            now = nxt;
            seen = nxt_seen;
        }
        cout << 0 << endl;
    }
}
accuracy: 0.10714285714285714
time_ms: 30
memory_kb: 49936
final_rank: 15

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 111;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];

  init();

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

  auto bfs=[&](auto &in){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));

    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);
      }
    }
  };

  if(n<SZ-10){
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };

    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;

    bfs(in);
    cout<<wy[n][n]<<endl;
    return 0;
  }

  const Int LEN=SZ-20;
  auto in=[&](Int y,Int x){
    return 0<y&&y<=LEN&&0<x&&x<=LEN;
  };

  // from (n, n)
  {
    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++){
      Int y=n-xs[i]+1;
      Int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;
    }

    bfs(in);

    for(Int i=0;i<SZ;i++)
      for(Int j=0;j<SZ;j++)
        zy[i][j]=wy[i][j];

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
  // from (1, 1)
  {
    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++)
      if(in(ys[i],xs[i]))
        wl[ys[i]][xs[i]]=1;

    bfs(in);

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }


  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]<LEN||xs[i]+ys[i]>2*n-LEN+2) continue;
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  }

  sz=vp.size();
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[ys[i]][xs[i]];

  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }

  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 97376
final_rank: 6

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 111;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];

  init();
  
  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  
  auto bfs=[&](auto &in){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }
  };
  
  if(n<SZ-10){    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
  
    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
    
    bfs(in);
    cout<<wy[n][n]<<endl;
    return 0;
  }
  
  const Int LEN=SZ-20;
  auto in=[&](Int y,Int x){
    return 0<y&&y<=LEN&&0<x&&x<=LEN;
  };
  
  // from (n, n)
  {
    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++){
      Int y=n-xs[i]+1;
      Int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;      
    }
    
    bfs(in);
    
    for(Int i=0;i<SZ;i++)
      for(Int j=0;j<SZ;j++)
        zy[i][j]=wy[i][j];
    
    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
  // from (1, 1)
  {    
    memset(wl, 0,sizeof(wl));
    for(Int i=0;i<m;i++)
      if(in(ys[i],xs[i]))
        wl[ys[i]][xs[i]]=1;

    bfs(in);
    
    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }    
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }

    
  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]<LEN||xs[i]+ys[i]>2*n-LEN+2) continue;
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  }
  
  sz=vp.size();
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[ys[i]][xs[i]];  
  
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 97336
final_rank: 5

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 111;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];

  init();

  const Int DEBUG=0;
  if(DEBUG){
    Int tmp=0;
    tmp=add(tmp,C((n-1)+(n-1)-11,n-1));
    tmp=add(tmp,C((n-1)+(n-1)-12,n-1-3));    
    tmp=mul(tmp,2);
    cout<<tmp<<endl;
  }
  
  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  
  if(n<SZ-10){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
  
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }
    
    cout<<wy[n][n]<<endl;
    return 0;
  }

  
  const Int LEN=SZ-20;
  auto in=[&](Int y,Int x){
    return 0<y&&y<=LEN&&0<x&&x<=LEN;
  };
  // from (1, 1)
  {    
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++)
      if(in(ys[i],xs[i]))
        wl[ys[i]][xs[i]]=1;    
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    if(DEBUG) cout<<res<<endl;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<res<<endl;
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }

  // from goal
  {
    auto in=[&](Int y,Int x){
      return 0<y&&y<=LEN&&0<x&&x<=LEN;
    };
    
    memset(zy, 0,sizeof(zy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++){
      Int y=n-xs[i]+1;
      Int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;      
    }
    
    using P = pair<Int, Int>;
    queue<P> que;
    zy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=zy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=0;
        }
        zy[ny][nx]=add(zy[ny][nx],zy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    if(DEBUG) cout<<res<<endl;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<ds[y][x]<<" "<<zy[y][x]<<endl;
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
    
  //init();
  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]<LEN||xs[i]+ys[i]>2*n-LEN+2) continue;
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  }
  
  sz=vp.size();
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[ys[i]][xs[i]];  
  
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }
  
  cout<<ans<<endl;
  //return 0;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 97380
final_rank: 7

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 1411;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];

  init();

  const Int DEBUG=0;
  if(DEBUG){
    Int tmp=0;
    tmp=add(tmp,C((n-1)+(n-1)-11,n-1));
    tmp=add(tmp,C((n-1)+(n-1)-12,n-1-3));    
    tmp=mul(tmp,2);
    cout<<tmp<<endl;
  }
  
  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  
  if(n<SZ-10){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
  
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }
    
    cout<<wy[n][n]<<endl;
    return 0;
  }



  
  const Int LEN=1010;
  auto in=[&](Int y,Int x){
    return 0<y&&y<=LEN&&0<x&&x<=LEN;
  };
  // from (1, 1)
  {    
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++)
      if(in(ys[i],xs[i]))
        wl[ys[i]][xs[i]]=1;    
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    if(DEBUG) cout<<res<<endl;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<res<<endl;
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }

  // from goal
  {
    auto in=[&](Int y,Int x){
      return 0<y&&y<=LEN&&0<x&&x<=LEN;
    };
    
    memset(zy, 0,sizeof(zy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++){
      Int y=n-xs[i]+1;
      Int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;      
    }
    
    using P = pair<Int, Int>;
    queue<P> que;
    zy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=zy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=0;
        }
        zy[ny][nx]=add(zy[ny][nx],zy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    if(DEBUG) cout<<res<<endl;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<ds[y][x]<<" "<<zy[y][x]<<endl;
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
    
  //init();
  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]<LEN||xs[i]+ys[i]>2*n-LEN+2) continue;
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  }
  
  sz=vp.size();
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[ys[i]][xs[i]];  
  
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }
  
  cout<<ans<<endl;
  //return 0;
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 159264
final_rank: 8

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 1411;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];
    
  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  
  if(n<SZ-10){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
  
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }
    
    cout<<wy[n][n]<<endl;
    return 0;
  }





  
  const Int LEN=1010;
  auto in=[&](Int y,Int x){
    return 0<y&&y<=LEN&&0<x&&x<=LEN;
  };
  // from (1, 1)
  {    
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++)
      if(in(ys[i],xs[i]))
        wl[ys[i]][xs[i]]=1;    
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<res<<endl;
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }

  // from goal
  {
    auto in=[&](Int y,Int x){
      return 0<y&&y<=LEN&&0<x&&x<=LEN;
    };
    
    memset(zy, 0,sizeof(zy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++){
      int y=n-xs[i]+1;
      int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;      
    }
    
    using P = pair<Int, Int>;
    queue<P> que;
    zy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=zy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=0;
        }
        zy[ny][nx]=add(zy[ny][nx],zy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<ds[y][x]<<" "<<zy[y][x]<<endl;
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
    
  init();
  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++)
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[xs[i]][ys[i]];  
  
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.48214285714285715
time_ms: 160
memory_kb: 159224
final_rank: 14

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

const Int SZ = 1111;
Int wy[SZ][SZ];
Int zy[SZ][SZ];
Int ds[SZ][SZ];
Int wl[SZ][SZ];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];
    
  Int dy[]={0,0,1,-1};
  Int dx[]={1,-1,0,0};
  
  if(n<SZ-10){
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
  
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }
    
    cout<<wy[n][n]<<endl;
    return 0;
  }











  
  const Int LEN=505;
  // from (1, 1)
  {
    auto in=[&](Int y,Int x){
      return 0<y&&y<=LEN&&0<x&&x<=LEN;
    };
    
    memset(wy, 0,sizeof(wy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++)
      if(ys[i]<=LEN&&xs[i]<=LEN)
        wl[ys[i]][xs[i]]=1;    
    
    using P = pair<Int, Int>;
    queue<P> que;
    wy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=wy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          wy[ny][nx]=0;
        }
        wy[ny][nx]=add(wy[ny][nx],wy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<res<<endl;
        ys.emplace_back(y);
        xs.emplace_back(x);
      }
    }
  }

  // from goal
  {
    auto in=[&](Int y,Int x){
      return 0<y&&y<=LEN&&0<x&&x<=LEN;
    };
    
    memset(zy, 0,sizeof(zy));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++){
      int y=n-xs[i]+1;
      int x=n-ys[i]+1;
      if(in(y,x)) wl[y][x]=1;      
    }
    
    using P = pair<Int, Int>;
    queue<P> que;
    zy[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=zy[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          zy[ny][nx]=0;
        }
        zy[ny][nx]=add(zy[ny][nx],zy[y][x]);        
      }
    }

    Int res=-1;
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]<0) continue;
      if(res<0) res=ds[y][x];
      chmin(res,ds[y][x]);
    }
    if(res<0){
      cout<<0<<endl;
      return 0;
    }
    for(Int y=1;y<LEN;y++){
      Int x=LEN-y;
      if(ds[y][x]==res){
        //cout<<y<<" "<<x<<":"<<ds[y][x]<<" "<<zy[y][x]<<endl;
        ys.emplace_back(n-x+1);
        xs.emplace_back(n-y+1);
      }
    }
  }
    
  init();
  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++)
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  for(Int i=0;i<sz;i++)
    if(xs[i]+ys[i]==LEN)
      dp2[i]=wy[xs[i]][ys[i]];  
  
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }

  Int ans=0;
  for(Int i=0;i<sz;i++){
    if(xs[i]+ys[i]==2*n-LEN+2){
      ans=add(ans,mul(dp1[i],zy[n-xs[i]+1][n-ys[i]+1]));
      ans=sub(ans,mul(dp2[i],zy[n-xs[i]+1][n-ys[i]+1]));
    }
  }
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.48214285714285715
time_ms: 80
memory_kb: 135628
final_rank: 13

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

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

const Int MOD = 1e9+9;

Int add(Int a,Int b){
  return (a+b)%MOD;
}
Int sub(Int a,Int b){
  return (a+MOD-b)%MOD;
}
Int mul(Int a,Int b){
  return (1LL*a*b)%MOD;
}
Int pow(Int a,Int b){
  Int r=1;
  while(b){
    if(b&1) r=mul(r,a);
    a=mul(a,a);
    b>>=1;
  }
  return r;
}
Int inv(Int a){
  return pow(a,MOD-2);
}

const Int MAX = 4e6+100;
Int fact[MAX],finv[MAX],invs[MAX];
void init(){
  fact[0]=1;
  for(Int i=1;i<MAX;i++)
    fact[i]=mul(fact[i-1],i);    

  finv[MAX-1]=inv(fact[MAX-1]);
  for(Int i=MAX-1;i>0;i--)
    finv[i-1]=mul(finv[i],i);  

  invs[0]=0;
  for(Int i=1;i<MAX;i++)
    invs[i]=mul(finv[i],fact[i-1]);
}

Int C(Int n,Int k){
  if(k<0||k>n) return 0;
  return mul(fact[n],mul(finv[n-k],finv[k]));
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  vector<Int> xs(m),ys(m);
  for(Int i=0;i<m;i++) cin>>xs[i]>>ys[i];

  
  if(n<100){
    Int ws[111][111];
    Int ds[111][111];
    Int wl[111][111];
    
    memset(ws, 0,sizeof(ws));
    memset(ds,-1,sizeof(ds));
    memset(wl, 0,sizeof(wl));
    
    for(Int i=0;i<m;i++)
      wl[ys[i]][xs[i]]=1;
   
    
    auto in=[&](Int y,Int x){
      return 0<y&&y<=n&&0<x&&x<=n;
    };
    
    Int dy[]={0,0,1,-1};
    Int dx[]={1,-1,0,0};
    
    using P = pair<Int, Int>;
    queue<P> que;
    ws[1][1]=1;
    ds[1][1]=0;
    que.emplace(1,1);

    while(!que.empty()){
      Int y,x;
      tie(y,x)=que.front();que.pop();
      //cout<<y<<" "<<x<<":"<<ds[y][x]<<" "<<ws[y][x]<<endl;
      for(Int k=0;k<4;k++){
        Int ny=y+dy[k],nx=x+dx[k];
        if(!in(ny,nx)||wl[ny][nx]) continue;
        if(ds[ny][nx]<0){
          ds[ny][nx]=ds[y][x]+1;
          ws[ny][nx]=ws[y][x];
          que.emplace(ny,nx);
          continue;
        }
        if(ds[ny][nx]<ds[y][x]+1) continue;
        if(ds[ny][nx]>ds[y][x]+1){          
          ds[ny][nx]=ds[y][x]+1;
          ws[ny][nx]=0;
        }
        ws[ny][nx]=add(ws[ny][nx],ws[y][x]);        
      }
    }

    //cout<<ds[n][n]<<endl;
    cout<<ws[n][n]<<endl;
    return 0;
  }
  init();
  xs.emplace_back(1);
  ys.emplace_back(1);
  xs.emplace_back(n);
  ys.emplace_back(n);

  Int sz=xs.size();
  using P = pair<Int, Int>;
  vector<P> vp;
  for(Int i=0;i<sz;i++)
    vp.emplace_back(xs[i]+ys[i],ys[i]);
  sort(vp.begin(),vp.end());
  for(Int i=0;i<sz;i++){
    xs[i]=vp[i].first-vp[i].second;
    ys[i]=vp[i].second;
  }

  vector<Int> dp1(sz,0);
  vector<Int> dp2(sz,0);
  dp2[0]=1;
  for(Int i=0;i<sz;i++){
    for(Int j=i+1;j<sz;j++){
      Int dx=xs[j]-xs[i];
      Int dy=ys[j]-ys[i];
      if(dx<0||dy<0) continue;
      dp1[j]=add(dp1[j],mul(dp2[i],C(dx+dy,dx)));
      dp2[j]=add(dp2[j],mul(dp1[i],C(dx+dy,dx)));
    }
  }
  
  cout<<sub(dp1[sz-1],dp2[sz-1])<<endl;
  return 0;
}
accuracy: 0.10714285714285714
time_ms: 50
memory_kb: 96992
final_rank: 17