#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read(){
   int s=0,w=1;char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
   return s*w;
}
const int N=1e5+5;
const int mod=1e9+7;
LL jc[N],inv[N],f[2005];
struct ppx{
    int x,y;
}a[2005];
inline bool cmp(const ppx &a,const ppx &b){return a.x==b.x?a.y<b.y:a.x<b.x;}
inline LL ksm(LL a,int b){
    LL cnt=1;
    while(b){
	if(b&1)cnt=(1ll*cnt*a)%mod;
	a=(1ll*a*a)%mod;
	b>>=1;
    }
    return cnt;
}
inline LL C(int n,int m){return jc[n]*inv[m]%mod*inv[n-m]%mod;}
int main(){
    jc[0]=1;inv[0]=1;
    for(int i=1;i<=100000;++i){
	jc[i]=jc[i-1]*i%mod;
	inv[i]=ksm(jc[i],mod-2);
    }
    int h=read(),w=read(),n=read();
    for(int i=1;i<=n;++i){
	a[i].x=read();a[i].y=read();
    }
    sort(a+1,a+n+1,cmp);
    a[n+1].x=h;a[n+1].y=w;
    for(int i=1;i<=n+1;++i){
	f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
	for(int j=1;j<i;++j)
	    f[i]=(f[i]-1ll*f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod;
    }
    printf("%lld\n",(f[n+1]+mod)%mod);
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

long long n,m,k;
long long mod=1e9+7;
long long c[2000005];
long long ny[2000005];
long long dp[2000005];
struct node{
	long long x,y;
	bool operator < (const node & bb) const
	{
		if(x==bb.x) return y<bb.y;
		return x<bb.x;
	}
}a[2000005];

long long quickpow(long long x,long long k,long long p)
{
	long long tot=1;
	while(k)
	{
		if(k%2==1) tot=(tot*x)%p;
		x=(x*x)%p;
		k/=2;
	}
	return tot;
}

void work()
{
	c[0]=1;
	for(long long i=1;i<=200000;i++)
	c[i]=(c[i-1]*i)%mod;
	ny[200000]=quickpow(c[200000],mod-2,mod);
	for(long long i=200000-1;i>=0;i--)
	ny[i]=(ny[i+1]*(i+1))%mod;
//	for(long long i=1;i<=20000;i++)
//	ny[i]=(ny[i]*c[i-1])%mod;
}

long long C(int x,int y)
{
	if(y>x) return 0;
	if(x==y) return 1;
//	cout<<c[x]<<" "<<y<<" "<<ny[y]<<" "<<x<<" "<<ny[x-y]<<endl;
	return ((c[x]*ny[y])%mod*ny[x-y])%mod;
}

int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	for(long long i=1;i<=k;i++)
	scanf("%lld%lld",&a[i].x,&a[i].y);
	a[k+1].x=n;a[k+1].y=m;
	sort(a+1,a+k+2);
	work();
//	for(int i=1;i<=15;i++)
//	cout<<ny[i]<<" ";
	for(int i=1;i<=k+1;i++)
	{
		dp[i]=C(a[i].x+a[i].y-2,a[i].x-1);
//		cout<<dp[i]<<" ";
		for(int j=1;j<i;j++)
		{
			if(a[i].x<a[j].x||a[i].y<a[j].y) continue;
			dp[i]=(dp[i]-(dp[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod)%mod;
		}
		while(dp[i]<0) dp[i]+=mod;
//		cout<<dp[i]<<" ";
	}
	cout<<dp[k+1];
	return 0;
}


#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ll long long
#define db double
#define For(i,a,b) for(register int i=a;i<=b;++i)
#define FOR(i,a,b) for(register int i=a;i>=b;--i)
//#define bl(i,x) for(int i=fir[x];i;i=nex[i])
//#define bl(i,x) for(int i=0;i<son[x].size();++i)

const ll N=2e5+50,JLY=1e9+7;
ll h,w,n,p[N],inv[N],f[N];
struct Node{ ll x,y; }a[N];

inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return x*f;
}

ll ksm(ll a,ll b){
	ll res=1;
	for(;b;b>>=1){if(b&1)res=(res*a)%JLY;a=(a*a)%JLY;}
	return res%JLY;
}

inline bool cmp(Node a,Node b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}

ll C(ll m,ll n){if(m>n)return 0;return (p[n]*inv[n-m]%JLY*inv[m])%JLY;}

int main()
{
	h=read(),w=read(),n=read();
	For(i,1,n)a[i].x=read(),a[i].y=read();
	a[n+1].x=h,a[n+1].y=w;
	sort(a+1,a+n+2,cmp);
	For(i,0,N-10)p[i]=(i==0)?1:((p[i-1]*i)%JLY),inv[i]=ksm(p[i],JLY-2);
	For(i,1,n+1)f[i]=C(a[i].x-1,a[i].x+a[i].y-2);
	For(i,1,n+1)For(j,1,i-1)
	{
		if(a[i].x<a[j].x||a[i].y<a[j].y)continue;
		f[i]-=f[j]*C(a[i].x-a[j].x,a[i].x+a[i].y-a[j].x-a[j].y);
		f[i]=(f[i]+JLY)%JLY;
	}
	printf("%lld\n",(f[n+1]+JLY)%JLY);
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;

struct cord
{
	int x;
	int y;
	friend bool operator < (cord a, cord b)
	{
		if(a.x != b.x)
			return a.x < b.x;
		return a.y < b.y;
	}
}a[2005];
const int N = 2e5 + 5, mod = 1e9 + 7;
int fac[N], inv[N], f[2005];

int qpow(int a, int b)
{
	int ans = 1;
	while(b)
	{
		if(b & 1)
			ans = ans * 1ll * a % mod;
		a = a * 1ll * a % mod;
		b >>= 1;
	}
	return ans;
}
void reset(int x)
{
	int i;
	fac[1] = 1;
	for(i = 2; i <= x; i++)
		fac[i] = fac[i - 1] * 1ll * i % mod;
	inv[x] = qpow(fac[x], mod - 2);
	for(i = x; i >= 2; i--)
		inv[i - 1] = inv[i] * 1ll * i % mod;
	return ;
}
int C(int a, int b)
{
	if(b < a)
		return 0;
	if(!a)
		return 1;
	return fac[b] * 1ll * inv[a] % mod * inv[b - a] % mod;
}

int main()
{
	int i, j, h, w, n;
	cin >> h >> w >> n;
	reset(h + w);
	for(i = 1; i <= n; i++)
		cin >> a[i].x >> a[i].y;
	n++;
	a[n].x = h;
	a[n].y = w;
	sort(a + 1, a + n + 1);
	for(i = 1; i <= n; i++)
	{
		f[i] = C(a[i].x - 1, a[i].x + a[i].y - 2);
		for(j = 1; j <= i - 1; j++)
			f[i] = ((f[i] * 1ll - f[j] * 1ll * C(a[i].x - a[j].x, a[i].x + a[i].y - a[j].x - a[j].y) % mod) % mod + mod) % mod;
	}
	cout << f[n] << endl;
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define N 100010
#define ll long long
using namespace std;
int n,m,k;
struct node{int x,y;}o[3010];
bool cmp(node a,node b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
ll mul[N<<1],inv[N<<1],dp[N],ans;
ll C(int n,int m){return mul[n]*inv[m]%mod*inv[n-m]%mod;}
int a[1010][1010],f[1010][1010];
void part()
{
	for(int i = 1;i<=k;i++)
    {
    	int x,y;
    	scanf("%d%d",&x,&y);
    	a[x][y] = 1;
    }f[1][1]  = 1;
    for(int i = 1;i<=n;i++)
    	for(int j = 1;j<=m;j++)
    		if(!a[i][j]&&i*j!=1)f[i][j] =(f[i-1][j]+f[i][j-1])%mod;
	cout<<f[n][m],exit(0);
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    if(n<=1000&&m<=1000)part();
    mul[0] = mul[1] = inv[0] = inv[1] = 1;
    for(int i = 2;i<=n+m;i++)mul[i] = mul[i-1]*i%mod;
    for(int i = 2;i<=n+m;i++)inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(int i = 2;i<=n+m;i++)inv[i] = inv[i]*inv[i-1]%mod;
    for(int i = 1;i<=k;i++)scanf("%d%d",&o[i].x,&o[i].y);
    sort(o+1,o+1+k,cmp);
    ans = C(n+m-2,n-1);
    for(int i = k;i>=1;i--)
    {
        dp[i] = C(n-o[i].x+m-o[i].y,n-o[i].x);
        for(int j = 1;j<=k;j++)
        {
            if(i==j||o[j].x<o[i].x||o[j].y<o[i].y)continue;
            dp[i] = (dp[i]-C(o[j].x-o[i].x+o[j].y-o[i].y,o[j].x-o[i].x)*dp[j]%mod)%mod;
            dp[i] = (dp[i]+mod)%mod;
        }
    }
    for(int i = 1;i<=k;i++)
    {
        ans = (ans-dp[i]*C(o[i].x+o[i].y-2,o[i].x-1)%mod)%mod;
    }printf("%I64d",(ans+mod)%mod);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pii pair<int,int>
#define N 2010
#define M 200010
#define ll long long
#define x first
#define y second
int h,w,n;
pii a[N];
ll jc[M],inv[M],f[N];
ll C(int n,int m){//C(n,m)
	return jc[n]*inv[m]%mod*inv[n-m]%mod;
}
ll poww(ll a,int b){
	ll ret=1;
	for(;b;b>>=1){
		if(b&1)ret=ret*a%mod;
		a=a*a%mod;
	}
	return ret;
}
int main(){
	cin>>h>>w>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].y;
	}
	sort(a+1,a+n+1);
//	a[1].x=a[1].y=1;
	a[n+1].x=h;
	a[n+1].y=w;
	jc[0]=inv[0]=1;//要从0开始，因为0!=1
	for(int i=1;i<h+w;i++){
		jc[i]=jc[i-1]*i%mod;
		inv[i]=poww(jc[i],mod-2);
	}
	for(int i=1;i<=n+1;i++){
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
		for(int j=1;j<i;j++){
			if(a[i].x>=a[j].x&&a[i].y>=a[j].y)f[i]=(f[i]-f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod;
		}
//		cout<<f[i]<<endl;
	}
	cout<<(f[n+1]+mod)%mod<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2010;
const int mod=1e9+7;

int h,w,n;
int f[maxn];
struct node{
	int x,y;
} a[maxn];
int jc[200010];
int jcc[200010];

inline int C(int nn,int mm){
	//printf("nn==%d,mm==%d\n",nn,mm);
	return jc[nn]*jcc[mm]%mod*jcc[nn-mm]%mod;
}

inline int qpow(int a,int b){
	int c=1;
	while(b){
		if(b&1) c=c * a % mod;
		a=a * a % mod;
		b>>=1;
	}
	return c;
}

inline bool cmp(node x,node y){
	if(x.x<y.x) return 1;
	if(x.x==y.x&&x.y<y.y) return 1;
	return 0;
}
signed main(){
	jc[0]=1;jcc[0]=1;
	for(int i=1;i<=200000;i++){
		jc[i]=jc[i-1]*i%mod;
		jcc[i]=qpow(jc[i],mod-2)%mod;

	}
	cin>>h>>w>>n;

	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].y;
	}

	a[n+1].x=h;a[n+1].y=w;
	sort(a+1,a+1+n,cmp);

	for(int i=1;i<=n+1;i++){
		f[i]=C(a[i].x-1+a[i].y-1,a[i].x-1)%mod;
		//printf("f[%lld]==%lld\n",i,f[i]);
		for(int j=1;j<i;j++){
			if(a[j].x>a[i].x||a[j].y>a[i].y) continue;
			f[i]=(f[i]%mod-f[j]%mod*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mod)%mod;
		}
		//printf("f[%lld]==%lld\n",i,f[i]);
	}
	cout<<(f[n+1]+mod)%mod<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
pair<int,int> a[2005];
int h,w,n,f[2005],mod=1e9+7;
long long q[200005],q1[200005];
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int C(int x,int y)
{
    return q[x]*q1[y]%mod*q1[x-y]%mod;
}
long long power(int a,int b)
{
    long long x=1;
    while(b)
    {
        if(b&1) x=x*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }
    return x;
}
int main()
{
    q[0]=1,q1[0]=1;
    for(int i=1;i<=200002;i++)
    {
        q[i]=q[i-1]*i%mod;
        q1[i]=power(q[i],mod-2);
    }
    h=read(),w=read(),n=read();
    for(int i=1;i<=n;i++)
    a[i].xx=read(),a[i].yy=read();
    sort(a+1,a+1+n);
    a[n+1].xx=h,a[n+1].yy=w;
    for(int i=1;i<=n+1;i++)
    {
        f[i]=C(a[i].xx+a[i].yy-2,a[i].xx-1);
        for(int j=1;j<i;j++)
        {
            if(a[j].xx>a[i].xx||a[j].yy>a[i].yy) continue;
            f[i]=(f[i]-1ll*f[j]*C(a[i].xx+a[i].yy-a[j].xx-a[j].yy,a[i].xx-a[j].xx))%mod;
        }
    }
    cout<<(f[n+1]+mod)%mod<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#define xi (black[i].first)
#define xj (black[j].first)
#define yi (black[i].second)
#define yj (black[j].second)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
ll n,m,k,fact[200005],inv[200005],f[2005];
pair<ll,ll>black[2005];
inline ll c(const ll a,const ll b){
    return fact[a]%MOD*inv[b]%MOD*inv[a-b]%MOD;
}
inline ll pow(register ll a,register ll b){
    register ll ans=1;
    while(b){
        if(b&1) ans=ans*a%MOD;
        a=a*a%MOD;
        b/=2;
    }
    return ans%MOD;
}
int main(){
	cin>>n>>m>>k;
	for(register ll i=1;i<=k;i++) cin>>xi>>yi;
	k++;
	black[k].first=n,black[k].second=m;
	sort(black+1,black+k+1);
	fact[0]=1;
	for(register ll i=1;i<=200000;i++) fact[i]=fact[i-1]*i%MOD;
	inv[200000]=pow(fact[200000],MOD-2);
	for(register ll i=199999;i>=0;i--) inv[i]=inv[i+1]*(i+1)%MOD;
	for(register ll i=1;i<=k;i++){
		f[i]=c(xi+yi-2,xi-1);
		for(register ll j=1;j<i;j++) f[i]=((f[i]-f[j]*c(xi+yi-xj-yj,xi-xj)%MOD)%MOD+MOD)%MOD;
	}
	cout<<(f[k]+MOD)%MOD;
}


#include<cstdio>
#include<algorithm>
#define int long long
int p=1e9+7;
using namespace std;
struct node{
	int x,y;
}nod[1000000];
bool cmp(node x,node y)
{
	if(x.x==y.x)return x.y<y.y;
	return x.x<y.x;
}
int ksm(int x,int k)
{
	int sum=1;
	while(k)
	{
		if(k&1)sum=(sum*x)%p;
		x=(x*x)%p;
		k>>=1;
	}
	return sum;
}
int jx[1000100],h,w,n,dp[1000000],inv[1000100];
int C(int x,int y)
{
	return (jx[y]*inv[y-x]%p)*inv[x]%p;
}
signed main()
{
	jx[1]=1;jx[0]=1;inv[0]=1;//求jx求逆元 初始化
	for(int i=1;i<=100100;++i)
	{
		jx[i]=(jx[i-1]*i)%p;
		inv[i]=ksm(jx[i],p-2)%p;
	}
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld%lld",&nod[i].x,&nod[i].y);
	}
	nod[n+1].x=h;
	nod[n+1].y=w;
	sort(nod+1,nod+n+2,cmp);
	for(int i=1;i<=n+1;++i)
	{
		dp[i]=C(nod[i].x-1,nod[i].x+nod[i].y-2);
		for(int j=1;j<=i-1;++j)
		{
			if(i==j)continue;
			if(nod[i].x>=nod[j].x&&nod[i].y>=nod[j].y)
			{
				dp[i]-=dp[j]*C(nod[i].x-nod[j].x,nod[i].x+nod[i].y-nod[j].x-nod[j].y)%p;//到 x,y 的所有情况减去所有有黑点的情况
				dp[i]=(dp[i]+p)%p;
			}
		}
	}
	printf("%lld",(dp[n+1]+p)%p);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define int long long
int p=1e9+7;
using namespace std;
struct node{
	int x,y;
}nod[1000000];
bool cmp(node x,node y)
{
	if(x.x==y.x)return x.y<y.y;
	return x.x<y.x;
}
int ksm(int x,int k)
{
	int sum=1;
	while(k)
	{
		if(k&1)sum=sum*x%p;
		x=x*x%p;
		k>>=1;
	}
	return sum;
}
int jx[1000100],h,w,n,dp[1000000],inv[1000100];
int C(int x,int y)
{
	return jx[y]*inv[y-x]%p*inv[x]%p;
}
signed main()
{
	jx[1]=1;jx[0]=1;inv[0]=1;//求jx求逆元 初始化
	for(int i=1;i<=100100;++i)
	{
		jx[i]=(jx[i-1]*i)%p;
		inv[i]=ksm(jx[i],p-2)%p;
	}
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld%lld",&nod[i].x,&nod[i].y);
	}
	nod[n+1].x=h;
	nod[n+1].y=w;
	sort(nod+1,nod+n+1,cmp);
	for(int i=1;i<=n+1;++i)
	{
		dp[i]=C(nod[i].x-1,nod[i].x+nod[i].y-2);
		for(int j=1;j<=n+1;++j)
		{
			if(nod[i].x<nod[j].x||nod[i].y<nod[j].y)continue;
			dp[i]-=dp[j]*C(nod[i].x-nod[j].x,nod[i].x+nod[i].y-nod[j].x-nod[j].y)%p;//到 x,y 的所有情况减去所有有黑点的情况
			dp[i]=(dp[i]+p)%p;
		}
	}
	printf("%lld",(dp[n+1]+p)%p);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
struct hlydl{
	int x;
	int y;
}t[2005];
bool cmp(hlydl x,hlydl y)
{
	if(x.x!=y.x)
	return x.x<y.x;
	return x.y<y.y;
}
int f[2005];
int jc[200005];
int invjc[200005];
int mod=1e9+7;
int ksm(int a,int b)
{
	int ans=1,base=a;
	while(b)
	{
		if(b&1)
		ans=ans*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return ans;
}
int C(int x,int y)
{
	return jc[x]*invjc[y]%mod*invjc[x-y]%mod;
}
signed main()
{
	jc[0]=1;
	invjc[0]=1;
	for(int i=1;i<=200002;i++)
	jc[i]=jc[i-1]*i%mod;
	invjc[200002]=ksm(jc[200002],mod-2);
	for(int i=200001;i>=1;i--)
	invjc[i]=invjc[i+1]*(i+1)%mod;
	int h,w,n;
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;i++)
	scanf("%lld%lld",&t[i].x,&t[i].y);
	sort(t+1,t+n+1,cmp);
	t[n+1].x=h,t[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(t[i].x+t[i].y-2,t[i].x-1);
		for(int j=1;j<i;j++)
		{
			if(t[i].x<t[j].x||t[i].y<t[j].y)
			continue;
			f[i]=(f[i]-f[j]*C(t[i].x-t[j].x+t[i].y-t[j].y,t[i].x-t[j].x))%mod;
		}
	}
	cout<<f[n+1];
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define int long long
const int maxn = 5050;
pair<int, int> a[maxn];
int h, w, n, f[maxn], mod = 1e9 + 7;
int jc[2000020], jcinv[2000020];
int C(int n, int m)
{
	return jc[n] * jcinv[m] % mod * jcinv[n - m] % mod;
}
int qpow(int x, int y, int mod)
{
	if (!y) return 1;
	if (y == 1) return x;
	if (y % 2)
	{
		int tmp = qpow(x, y / 2, mod);
		return tmp * tmp % mod * x % mod;
	}
	else
	{
		int tmp = qpow(x, y / 2, mod);
		return tmp * tmp % mod;
	}
}
signed main()
{
	jc[0] = 1, jcinv[0] = 1;
	for (int i = 1; i <= 200000; i++)
		jc[i] = jc[i - 1] * i % mod, jcinv[i] = qpow(jc[i], mod - 2, mod);
	scanf("%lld%lld%lld", &h, &w, &n);
	for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i].x, &a[i].y);
	a[n + 1].x = h, a[n + 1].y = w;
	sort(a + 1, a + n + 2);
	for (int i = 1; i <= n + 1; i++)
	{
		f[i] = C(a[i].x + a[i].y - 2, a[i].x - 1);
		for (int j = 1; j < i; j++)
		{
			if (a[j].x > a[i].x || a[j].y > a[i].y) continue;
			f[i] = (f[i] - f[j] * C(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x- a[j].x) + mod) % mod;
		}

	}
	printf("%lld\n", f[n + 1]);
	return 0;

}


#include<bits/stdc++.h>
#define unable AC
#define dying KLL
#define re register int
#define rep(q,a,b) for(re q(a);q<=b;++q)
#define dep(q,a,b) for(re q(a);q>=b;--q)
#define mem(a,b) memset(a,b,sizeof a )
#define in(a) scanf("%d",&a)
using namespace std;
const long long INF=1e9+7;
int h,w,n;
long long fi[200005],ni[200005],dp[2005];
void init() {
    fi[0]=fi[1]=1,ni[0]=ni[1]=1;
    rep(q,2,w+h)fi[q]=fi[q-1]*q%INF;
    rep(q,2,w+h)ni[q]=(INF-INF/q)*ni[INF%q]%INF;
    rep(q,2,w+h)ni[q]=ni[q]*ni[q-1]%INF;
}
long long C(int a,int b) {
    return fi[a]*ni[a-b]%INF*ni[b]%INF;
}
pair<int,int> qw[2005];
int main() {
    int a,b;
    scanf("%d%d%d",&h,&w,&n);
    init();
    rep(q,1,n)scanf("%d%d",&a,&b),qw[q]=make_pair(a,b);
    qw[++n]=make_pair(h,w);
    sort(qw+1,qw+n+1);
    rep(q,1,n) {
        dp[q]=C(qw[q].first+qw[q].second-2,qw[q].first-1);
        rep(w1,1,q-1) {
            int x=qw[q].first-qw[w1].first;
            int y=qw[q].second-qw[w1].second;
            if(x>=0&&y>=0)dp[q]=(dp[q]-dp[w1]*C(x+y,x))%INF;
        }
    }
    cout<<(dp[n]+INF)%INF;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
const int N=200005;
const int mod=1e9+7;
ll jc[N],inv[N],f[N];
struct ppx{
	int x,y;
}a[2005];
inline bool cmp(const ppx &x,const ppx &y){return x.x==y.x?x.y<y.y:x.x<y.x;}
inline ll ksm(int a,int b){
	ll cnt=1;
	while(b){
		if(b&1)cnt=(1ll*cnt*a)%mod;
		a=(1ll*a*a)%mod;
		b>>=1;
	}
	return cnt;
}
inline ll C(int n,int m){return 1ll*jc[n]*inv[m]%mod*inv[n-m]%mod;}
int main(){
	jc[0]=1;inv[0]=1;
	for(int i=1;i<=200000;++i){
		jc[i]=(jc[i-1]*i)%mod;
		inv[i]=ksm(jc[i],mod-2);
	}
	int h=read(),w=read(),n=read();
	for(int i=1;i<=n;++i)a[i].x=read(),a[i].y=read();
	sort(a+1,a+n+1,cmp);a[n+1].x=h;a[n+1].y=w;
	for(int i=1;i<=n+1;++i){
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
		for(int j=1;j<i;++j)
			f[i]=(f[i]-1ll*f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod;
	}
	printf("%lld\n",(f[n+1]+mod)%mod);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define il inline
#define ri register
#define ll long long
#define Size 200010
#define yyb 1000000007
using namespace std;
struct point{
	int x,y;
	il bool operator<(const point&a)const{
		return y==a.y?x<a.x:y<a.y;
	}
}p[2005];
ll dp[2005],jc[Size],jv[Size];
il void prepare(int);
il ll pow(ll,ll),c(int,int);
int main(){
	int h,w,n;
	scanf("%d%d%d",&h,&w,&n),prepare(h+w);
	for(int i(1);i<=n;++i)scanf("%d%d",&p[i].x,&p[i].y);
	sort(p+1,p+n+1),dp[1]=c(p[1].y+p[1].x-2,p[1].y-1);
	if(p[n].x==h&&p[n].y==w)return puts("0"),0;
	p[++n].x=h,p[n].y=w;
	for(int i(2),j;i<=n;++i){
		dp[i]=c(p[i].y+p[i].x-2,p[i].x-1);
		for(j=1;j<i;++j)
			(dp[i]-=dp[j]*c(p[i].y+p[i].x-p[j].y-p[j].x,p[i].y-p[j].y)%yyb)%=yyb;
	}printf("%lld",(dp[n]+yyb)%yyb);
	return 0;
}
il ll c(int n,int r){
	if(n<r||n<0||r<0)return 0;
	return jc[n]*jv[r]%yyb*jv[n-r]%yyb;
}
il ll pow(ll x,ll y){
	ll ans(1);while(y){
		if(y&1)(ans*=x)%=yyb;
		y>>=1,(x*=x)%=yyb;
	}return ans;
}
il void prepare(int n){
	int i;jc[0]=jv[0]=1;
	for(i=1;i<=n;++i)jc[i]=jc[i-1]*i%yyb;jv[n]=pow(jc[n],yyb-2);
	for(i=n-1;i;--i)jv[i]=jv[i+1]*(i+1)%yyb;
}


#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;
const int MAXN = 200000;
ll fac[MAXN + 10], finv[MAXN + 10], dp[2010];

inline ll qPow(ll a, ll b){
	ll ans = 1;
	while (b){
		if (b & 1)
			ans = ans * a % mod;
		a = a * a % mod, b >>= 1;
	}
	return ans;
}

void Init(){
	finv[0] = fac[0] = 1;
	for (register ll i = 1; i <= MAXN; ++i)
		fac[i] = fac[i - 1] * i % mod;
	finv[MAXN] = qPow(fac[MAXN], mod - 2);
	for (register ll i = MAXN - 1; i > 0; --i)
		finv[i] = finv[i + 1] * (i + 1) % mod;
}

inline ll C(ll n, ll m){
	return fac[n] * finv[m] % mod * finv[n - m] % mod;
}

struct Point{
	int r, c;

	bool operator < (const Point& oth) const {
		return r == oth.r ? r < oth.r : c < oth.c;
	}
}pt[2010];

inline ll Abs(ll a){return a < 0 ? -a : a;}

inline ll dist(Point a, Point b){
	return Abs(a.r - b.r) + Abs(a.c - b.c);
}

int main(){
	Init();
	int N, M, tot;
	scanf("%d %d %d", &N, &M, &tot);

	for (int i = 1; i <= tot; ++i){
		scanf("%d %d", &pt[i].r, &pt[i].c);
		if ((pt[i].r == 1 && pt[i].c == 1) ||
			(pt[i].r == N && pt[i].c == M) ){
			printf("0");
			return 0;
		}
	}
	pt[tot + 1] = {N, M}, ++tot;
	sort(pt + 1, pt + tot + 1);

	for (int i = 1; i <= tot; ++i){
		dp[i] = C(pt[i].r + pt[i].c - 2, pt[i].c - 1);

		for (int j = 1; j < i; ++j)
			if (pt[j].r <= pt[i].r && pt[j].c <= pt[i].c)
				dp[i] = (mod + dp[i] -
						(dp[j] * C( dist(pt[i], pt[j]), Abs(pt[i].c - pt[j].c) ) % mod) ) % mod;

		dp[i] = (dp[i] + mod) % mod;
	}
	printf("%lld", dp[tot]);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#define p 1000000007
using namespace std;
int h,w,n;
long long f[2260],jc[200006],jcinv[200006];
struct node{
	int x,y;
}a[2260];
long long power(long long a,long long b){
	long long ans=1;
	while(b)
	{
		if(b&1)	ans=(ans*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return ans%p;
}
int C(int n,int m){
    return jc[n]*jcinv[m]%p*jcinv[n-m]%p;
}
int Lucas(int x,int y){
    if(!y) return 1;
    if(x<y) return 0;
    if(x<p&&y<p)  return C(x,y);
    return (Lucas(x%p,y%p)*Lucas(x/p,y/p))%p;
}
int comp(node w,node x){
	if(w.x==x.x){
		return w.y<x.y;
	}
	return w.x<x.x;
}
int main(){
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	sort(a+1,a+n+1,comp);
	jc[0]=jcinv[0]=1;
	for(int i=1;i<=200000;i++){
        jc[i]=(jc[i-1]*i)%p;
        jcinv[i]=power(jc[i],p-2);
    }
    a[n+1].x=h;
    a[n+1].y=w;
    f[1]=Lucas(a[1].x+a[1].y-2,a[1].x-1)%p;
    //cout<<f[1]<<endl;
    for(int i=2;i<=n+1;i++){
    f[i]=Lucas(a[i].x+a[i].y-2,a[i].x-1)%p;
	long long u=0;
		for(int j=1;j<=i-1;j++){
    		f[i]=f[i]-(Lucas(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x)*f[j]);
    		f[i]=(f[i]+p)%p;
		}
	}
	cout<<f[n+1];
	return 0;
}


#include<bits/stdc++.h>
#define N 2005
#define PRE 200005
#define M 1000000007
#define ll long long
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
using namespace std;
struct P
{
	ll x,y;
}a[N];
ll fac[200010],h,w,n,f[N],inv[M];
int cmp(P A,P B)
{
	if(A.x<B.x) return 1;
	else
	{
		if(A.x==B.x)
		{
			if(A.y<B.y) return 1;
			else return 0;
		}
		else return 0;
	}
}
ll F(ll aa,ll bb,ll p)
{
    ll sum=1;
    while(bb)
    {
        if(bb&1) sum=((sum%p)*(aa%p))%p;
        aa=((aa%p)*(aa%p))%p;
        bb=bb>>1;
    }
    return sum%p;
}
ll C(ll A,ll B,ll p)
{
    if(B>A) return 0;
    return fac[A]*inv[B]%p*inv[A-B]%p;
}
int main()
{
    cin>>h>>w>>n;
    for(ll i=1;i<=n;i++)
    {
    	scanf("%d%d",&a[i].x,&a[i].y);
	}
	fac[0]=1;
	for(ll i=1;i<=PRE;i++)
	{
		fac[i]=((fac[i-1]%M)*(i%M))%M;
		inv[i]=F(fac[i],M-2,M);
	}
	sort(a+1,a+n+1,cmp);
	a[n+1].x=h;a[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1,M)%M;
		for(int j=1;j<=i-1;j++)
		{
		    ll d=C(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x,M);
			f[i]=f[i]-(((f[j]%M)*(d%M))%M);
			f[i]=(f[i]+M)%M;
		}
	}
	cout<<f[n+1]%M;
	return 0;
}


#include<bits/stdc++.h>
#define N 2005
#define PRE 200005
#define M 1000000007
#define ll long long
using namespace std;
struct P
{
	ll x,y;
}a[N];
ll fac[200010],h,w,n,f[200010],inv[200010];
int cmp(P A,P B)
{
	if(A.x<B.x) return 1;
	else
	{
		if(A.x==B.x)
		{
			if(A.y<B.y) return 1;
			else return 0;
		}
		else return 0;
	}
}
ll F(ll aa,ll bb,ll p)
{
    ll sum=1;
    while(bb)
    {
        if(bb&1) sum=((sum%p)*(aa%p))%p;
        aa=((aa%p)*(aa%p))%p;
        bb=bb>>1;
    }
    return sum%p;
}    //n    m
ll C(ll A,ll B,ll p)
{
    if(B>A) return 0;
    return fac[A]*inv[B]%p*inv[A-B]%p;
}
int main()
{
    cin>>h>>w>>n;
    for(ll i=1;i<=n;i++)
    {
    	scanf("%lld%lld",&a[i].x,&a[i].y);
	}
	fac[0]=1;inv[0]=1;
	for(ll i=1;i<=PRE;i++)
	{
		fac[i]=((fac[i-1]%M)*(i%M))%M;
		inv[i]=F(fac[i],M-2,M);
	}
	sort(a+1,a+n+1,cmp);
	a[n+1].x=h;a[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1,M)%M;
		for(int j=1;j<=i-1;j++)
		{
		    ll d=C(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x,M);
			f[i]=f[i]-(((f[j]%M)*(d%M))%M);
			f[i]=(f[i]+M)%M;
		}
	}
	cout<<f[n+1]%M;
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
#define M 200005
#define N 2005
#define LL long long
#define P 1000000007
using namespace std;
int h,w,n,mark[N][N];
LL fac[M],in[M],dp[N],rev[M];
struct node{
    int x,y;
    bool operator<(const node &A)const{
        return x!=A.x?x<A.x:y<A.y;
    }
}A[N];
void Init(){
    fac[0]=1,rev[1]=1,rev[0]=1;
    for(int i=1;i<M;i++)fac[i]=(fac[i-1]*i)%P;
    for(int i=2;i<M;i++)rev[i]=(P-P/i)*rev[P%i]%P;
    for(int i=2;i<M;i++)rev[i]=rev[i]*rev[i-1]%P;
}
struct P100{
    LL calc(LL a,LL b){
        return fac[a]*rev[b]%P*rev[a-b]%P;
    }
    void solve(){
        for(int i=1;i<=n;i++)scanf("%d%d",&A[i].x,&A[i].y);
        A[n+1].x=h,A[n+1].y=w;
        sort(A+1,A+n+2);
        for(int i=1;i<=n+1;i++){
            dp[i]=calc(A[i].x+A[i].y-2,A[i].x-1);
            for(int j=1;j<i;j++){
                int x=A[i].x-A[j].x,y=A[i].y-A[j].y;
                if(x>=0&&y>=0)dp[i]=(dp[i]-dp[j]*calc(x+y,x))%P;
            }
        }
        printf("%lld\n",(dp[n+1]+P)%P);
    }
}p100;
int main(){
    Init();
    scanf("%d%d%d",&h,&w,&n);
	p100.solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define x first
#define y second

using namespace std;

const int mod =1e9 + 7;
int h, w, n, f[2010];
pair <int, int> a[2010];
ll jc[200010], jcny[200010];
int C(int n, int m) {
	return jc[n] * jcny[m] % mod * jcny[n - m] % mod;
}
ll quickpow(ll a, ll b, ll c) {
	ll ans = 1;
	for (; b; b >>= 1) {
		if (b & 1) ans = (ans * a) % c;
		a = a * a % c;
	}
	return ans;
}

int main() {
	jc[0] = 1, jcny[0] = 1;
	for (register int i = 1; i <= 200000; i ++) {
		jc[i] = jc[i - 1] * i % mod;
		jcny[i] = quickpow(jc[i], mod - 2, mod);
	}
	cin >> h >> w >> n;
	for (register int i = 1; i <= n; i ++)
		scanf("%d %d", &a[i].x, &a[i].y);
	sort(a + 1, a + n + 1);
	a[n + 1].x = h, a[n + 1].y = w;
	for (register int i = 1; i <= n + 1; i ++) {
		f[i] = C(a[i].x + a[i].y - 2, a[i].x - 1);
		for (register int j = 1; j < i; j ++) {
			if (a[j].x > a[i].x || a[j].y > a[i].y) continue;
			f[i] = (f[i] - (ll)f[j] * C(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x) + mod) % mod;
		}
	}
	cout << (f[n + 1] + mod) % mod << endl;
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define N 200020
using namespace std;
typedef long long ll;
struct ljzz
{ll r,c;};
ljzz a[N];
ll h,w,n,dp[N],p[N],inv[N];
bool tmp(ljzz x,ljzz y)
{return x.r<y.r||(x.r==y.r&&x.c<y.c);}
ll cmbntn(ll x,ll y)
{return (x<y)?0:(p[x]%mod*inv[x-y]%mod*inv[y]%mod)%mod;}
ll counting(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
		ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans%mod;
}
int main()
{
	scanf("%lld%lld%lld",&h,&w,&n);
	for(ll i=1;i<=n;i++)
	scanf("%lld%lld",&a[i].r,&a[i].c);
	a[++n].r=h,a[n].c=w;
	sort(a+1,a+1+n,tmp);
	p[0]=inv[0]=1;
	for(ll i=1;i<N;i++)
	{
		p[i]=(p[i-1]*i)%mod;
		inv[i]=counting(p[i],mod-2);
	}
	for(ll i=1;i<=n;i++)
	dp[i]=cmbntn(a[i].r-1+a[i].c-1,a[i].r-1);
	for(ll i=1;i<=n;i++)
	for(ll j=1;j<i;j++)
	{
		if(a[i].r<a[j].r||a[i].c<a[j].c)continue;
		dp[i]=(dp[i]-dp[j]*cmbntn(a[i].r-a[j].r+a[i].c-a[j].c,a[i].r-a[j].r)+mod)%mod;
	}
	printf("%lld",dp[n]);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define R(i,s,t) for(int i=s;i<=t;i++)
using namespace std;
typedef pair<int,int> pii;
const int Mod=1e9+7;
pii rec[2005];
int h,w,n,p[200005],f[2005];
int qm(int b,int e) {
	if(!e) return 1;
	int tmp=qm(b,e>>1);
	tmp=tmp*tmp%Mod;
	if(e&1) tmp=tmp*b%Mod;
	return tmp;
}
int C(int n,int m) {
	return p[n]*qm(p[m],Mod-2)%Mod*qm(p[n-m],Mod-2)%Mod;
}
signed main() {
#ifdef LOCAL
	//freopen("count.in","r",stdin);
#endif
	p[0]=1;
	R(i,1,200000) p[i]=p[i-1]*i%Mod;
	cin>>h>>w>>n;
	R(i,1,n) cin>>rec[i].first>>rec[i].second;
	sort(rec+1,rec+1+n);
	int tot=C(h+w-2,h-1);
	R(i,1,n) {
		int x=rec[i].first,y=rec[i].second;
		f[i]=C(x+y-2,x-1);
		R(j,1,i-1) if(rec[j].second<=y) {
			int cx=rec[j].first,cy=rec[j].second;
			f[i]=(f[i]-f[j]*C(x-cx+y-cy,x-cx))%Mod;
		}
		tot=(tot-f[i]*C(h-x+w-y,h-x))%Mod;
	}
	cout<<(tot+Mod)%Mod<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int h,w,n;
long long f[4000],jc[300000],inv[300000],mo;
struct node
{
	int x,y;
}a[5000];
inline bool cmp(node x,node y)
{
	if (x.x!=y.x) return x.x<y.x;
	else return x.y<y.y;
}
inline long long power(long long u,long long b)
{
	long long s=1;
	while (b)
	{
		if (b&1) s=s*u%mo;
		u=u*u%mo;
		b>>=1;
	}
	return s;
}
inline long long c(int u,int v)
{
	return jc[u]*inv[v]%mo*inv[u-v]%mo;
}
int main()
{
	mo=1000000007;
	scanf("%d%d%d",&h,&w,&n);
	jc[0]=0;
	jc[1]=1;
	int sd=200001;
	for (int i=2;i<=sd+1;i++)jc[i]=(jc[i-1]*i)%mo;
	inv[sd+1]=power(jc[sd+1],1LL*(mo-2));
	for (int i=sd;i>=0;i--)inv[i]=inv[i+1]*1LL*(i+1)%mo;
	for (int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp);
	n++;
	a[n].x=h;a[n].y=w;
	for (int i=1;i<=n;i++)
	{
		long long sum=0;
		f[i]=c(a[i].x+a[i].y-2,a[i].x-1)%mo;
		for (int j=1;j<i;j++)
		{
			if (a[j].x>a[i].x||a[j].y>a[i].y) continue;
			sum=(sum+f[j]*c(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mo)%mo;
		}
		f[i]-=sum;
	}
	printf("%lld",f[n]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
const int MAXN= 2003;
const int MAXM = 1e5 + 3;
const ll mod = 1e9 + 7;
ll f[MAXN];
int h , w ;
struct node{
    ll x , y;
    friend bool operator < ( node a , node b ){
        if( a.x == b.x )
            return a.y < b.y;
        return a.x < b.x;
    }
}a[MAXN];
int n;
ll nic[MAXM] , jc[MAXM];
void pre(){
    nic[0] = nic[1] = jc[0] = jc[1] = 1;
    for( int i = 2 ; i <= MAXM - 3 ; i ++ ){
        jc[i] = jc[i-1] * i % mod;
        nic[i] = nic[mod%i] * ( mod - mod / i ) % mod;
    }
    for( int i = 1 ; i <= MAXM - 3 ; i ++ ){
        nic[i] = nic[i-1] * nic[i] % mod;
    }
}
ll C( ll n , ll m ){
    if( n < m ) return 0;
    return jc[n] * nic[m] % mod * nic[n-m] % mod;
}
ll abs_( ll x ) {
    if( x < 0 )
        return -x;
    return x;
}
int main(){
    scanf( "%d%d%d" , &h , &w , &n );
    pre();
    for( int i = 1 ; i <= n ; i ++ ){
        scanf( "%lld%lld" , &a[i].x , &a[i].y );
    }
    a[++n].x = h , a[n].y  = w;
    sort( a + 1 , a + n + 1) ;
    memset( f, 0 , sizeof( f ) );
    f[1] = C( a[1].x + a[1].y - 2 , a[1].x - 1 ) % mod;
    for( int i = 2 ; i <= n ; i ++ ){
        f[i] = C( a[i].x + a[i].y - 2 , a[i].x - 1 ) % mod;
        for( int j = 1 ; j < i ; j ++ ){
            if( a[j].x > a[i].x || a[j].y > a[j].y ) continue;
            f[i] = ( ( ( f[i] - ( f[j] * C( a[i].x + a[i].y - a[j].x - a[j].y , a[i].x - a[j].x ) % mod ) )%mod+ mod)% mod ) % mod;
        }
    }
    printf( "%lld\n" , f[n] );
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int M=2005,N=1e5+5,mod=1e9+7;
int h,w,n;
struct point {
	int x,y;
	bool friend operator <(point a,point b) {
		return a.x<b.x||a.x==b.x&&a.y<b.y;
	}
} p[M];
typedef long long LL;
LL f[N]= {1,1},t[N]= {1,1},dp[M];
LL fast_pow(int x,int p) {
	LL res=1,tmp=x;
	while(p) {
		if(p&1) (res*=tmp)%=mod;
		(tmp*=tmp)%=mod;
		p>>=1;
	}
	return res;
}
LL C(int x,int y) {
	if(x>y) return 0;
	return f[y]*t[y-x]%mod*t[x]%mod;
}
int main() {
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1; i<=n; i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	++n,p[n].x=h,p[n].y=w;
	sort(p+1,p+1+n);
	for(int i=2; i<=N-5; i++) {
		f[i]=1ll*f[i-1]*i%mod;
		t[i]=fast_pow(f[i],mod-2);
//		cout<<f[i]<<" "<<t[i]<<'\n';
	}
	for(int i=1; i<=n; i++) {
		dp[i]=C(p[i].x-1,p[i].x+p[i].y-2);
		for(int j=1; j<i; j++) {
//			if(p[i].x<p[j].x||p[i].y<p[j].y) continue;
			dp[i]-=1ll*dp[j]*C(p[i].x-p[j].x,p[i].x+p[i].y-p[j].x-p[j].y)%mod;
			dp[i]=(dp[i]+mod)%mod;
		}
	}
	printf("%lld\n",dp[n]);
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
#define db double
using namespace std;
inline int read(){
    int SUM=0,ff=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
      if(ch=='-')
        ff=-1;
      ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
      SUM=(SUM<<3)+(SUM<<1)+(ch^48);
      ch=getchar();
    }
    return SUM*ff;
}
const int N=200010;
const ll mod=1000000007;
struct node{
    int x,y;
    inline friend bool operator < (node a1,node a2){
	    if(a1.x!=a2.x) return a1.x<a2.x;
	    return a1.y<a2.y;
	}
}a[N];
int n,h,w;
ll fac[N],inv[N];
inline void init(){
    inv[0]=1;
    inv[1]=1;
    fac[0]=1;
    fac[1]=1;
    for(int i=2;i<=N-10;i++)
       inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    for(int i=2;i<=N-10;i++){
	   inv[i]=inv[i]*inv[i-1]%mod;
	   fac[i]=fac[i-1]*i%mod;
	}
}
inline ll C(int x,int y){
    return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
ll dp[N];
int main(){
    //freopen("CF559C Gerald and Giant Chess.in","r",stdin);
    //freopen("CF559C Gerald and Giant Chess.out","w",stdout);
    h=read(),w=read(),n=read();
    for(int i=1;i<=n;i++)
       a[i].x=read(),a[i].y=read();
    n++;
    a[n].x=h;
    a[n].y=w;
	sort(a+1,a+n+1);
    init();
    for(int i=1;i<=n;i++){
	   dp[i]=C(a[i].x+a[i].y-2,a[i].x-1);
	   for(int j=1;j<i;j++){
	      if(a[j].x<=a[i].x&&a[j].y<=a[i].y){
		    dp[i]-=dp[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mod;
		  }
	   }
	   dp[i]=(dp[i]%mod+mod)%mod;
	}
	printf("%lld\n",dp[n]);
    return 0;
}


#include<bits/stdc++.h>
#define N 2005
#define PRE 1000005
#define M 1000000007
#define ll long long
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
using namespace std;
struct P
{
	ll x,y;
}a[N];
ll fac[N],inv[N],h,w,n,f[N];
int cmp(P A,P B)
{
	if(A.x<B.x) return 1;
	else
	{
		if(A.x==B.x)
		{
			if(A.y<B.y) return 1;
			else return 0;
		}
		else return 0;
	}
}
ll F(ll aa,ll bb,ll p)
{
    ll sum=1;
    while(bb)
    {
        if(bb&1) sum=((sum%p)*(aa%p))%p;
        aa=((aa%p)*(aa%p))%p;
        bb=bb>>1;
    }
    return sum%p;
}
ll C(ll A,ll B,ll p)
{
    if(B>A) return 0;

    ll sa=1,sb=1;
    /*
    for(ll i=A-B+1;i<=A;i++) sa=((sa%p)*(i%p))%p;
    for(ll i=2;i<=B;i++) sb=((sb%p)*(i%p))%p;
    */
    sa=fac[A]%M;
    sb=((fac[B]%M)*(fac[A-B]%M))%M;
    return (sa*F(sb,p-2,p))%p;
}
int main()
{
    cin>>h>>w>>n;
    for(ll i=1;i<=n;i++)
    {
    	scanf("%d%d",&a[i].x,&a[i].y);
	}
	fac[0]=1;
	for(ll i=1;i<=N;i++)
	{
		fac[i]=((fac[i-1]%M)*(i%M))%M;
	}
	sort(a+1,a+n+1,cmp);
	a[n+1].x=h;a[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1,M)%M;
		for(int j=1;j<=i-1;j++)
		{
		    ll d=C(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x,M);
			f[i]=f[i]-(((f[j]%M)*(d%M))%M);
			f[i]=(f[i]+M)%M;
		}
	}
	cout<<f[n+1]%M;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

long long n,m,k;
long long mod=1e9+7;
long long c[200005];
long long ny[200005];
long long dp[200005];
struct node{
	long long x,y;
	bool operator < (const node & bb) const
	{
		if(x==bb.x) return y<bb.y;
		return x<bb.x;
	}
}a[2000005];

long long quickpow(long long x,long long k,long long p)
{
	long long tot=1;
	while(k)
	{
		if(k%2==1) tot=(tot*x)%p;
		x=(x*x)%p;
		k/=2;
	}
	return tot;
}

void work()
{
	c[0]=1;
	for(long long i=1;i<=20000;i++)
	c[i]=(c[i-1]*i)%mod;
	ny[20000]=quickpow(c[20000],mod-2,mod);
	for(long long i=20000-1;i>=0;i--)
	ny[i]=(ny[i+1]*(i+1))%mod;
//	for(long long i=1;i<=20000;i++)
//	ny[i]=(ny[i]*c[i-1])%mod;
}

long long C(int x,int y)
{
	if(y>x) return 0;
	if(x==y) return 1;
//	cout<<c[x]<<" "<<y<<" "<<ny[y]<<" "<<x<<" "<<ny[x-y]<<endl;
	return ((c[x]*ny[y])%mod*ny[x-y])%mod;
}

int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	for(long long i=1;i<=k;i++)
	scanf("%lld%lld",&a[i].x,&a[i].y);
	a[k+1].x=n;a[k+1].y=m;
	sort(a+1,a+k+2);
//	for(int i=1;i<=k+1;i++)
//	cout<<a[i].x<<" "<<a[i].y<<endl;
	work();
//	for(int i=1;i<=15;i++)
//	cout<<ny[i]<<" ";
	for(int i=1;i<=k+1;i++)
	{
		dp[i]=C(a[i].x+a[i].y-2,a[i].x-1);
//		cout<<dp[i]<<" ";
		for(int j=1;j<i;j++)
		{
			if(a[i].x<a[j].x||a[i].y<a[j].y) continue;
			dp[i]=(dp[i]-((dp[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod))%mod;
		}
		while(dp[i]<0) dp[i]+=mod;
//		cout<<dp[i]<<" ";
	}
	cout<<dp[k+1];
	return 0;
}


//
//  main.cpp
//  集训3
//
//  Created by 陈嘉佑 on 2020/10/21.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cctype>
#include <ctime>
#include <cstdlib>

#define x first
#define y second

const long long Moden=1000000007;
const int MaxN=2000000;
long long inv[2000005],fac[2000005],f[MaxN+5];
std::pair<int,int> mapp[MaxN+5];

long long q_pow(long long x,int v)
{
//    if (v==0) return 1;
//    if (v==1) return x;
//    int mid=(v/2);
//    long long hhh=q_pow(x,mid);
//    if (v%2==1) return (hhh*hhh)%Moden*x%Moden;
//    else return (hhh*hhh)%Moden;
    long long c=1;
    for (;v;v>>=1) {
        if (v&1) c=c*x%Moden;
        x=x*x%Moden;
    }
    return c;
}

void fu_fac()  //阶乘
{
    fac[0]=1;
    for (int i=1;i<=2000001;i++) {
        fac[i]=fac[i-1]*i%Moden;
//        std::cout<<fac[i]<<" ";
    }
}
void fu_inv()  //逆元
{
    inv[0]=1;
    for (int i=1;i<=2000001;i++) {
        inv[i]=q_pow(fac[i],Moden-2);
    }
}

long long c(int n,int m)
{
//    std::cout<<fac[n]<<" "<<inv[m]<<" "<<inv[n-m]<<std::endl;
    return fac[n]*inv[m]%Moden*inv[n-m]%Moden;
}

int main()
{
    fu_fac();
    fu_inv();

    int h,w,n;
    std::cin>>h>>w>>n;
    for (int i=1;i<=n;i++) {
        std::cin>>mapp[i].x>>mapp[i].y;
    }
    sort (mapp+1,mapp+n+1);
    mapp[n+1].x=h;
    mapp[n+1].y=w;
    for (int i=1;i<=n+1;i++) {
        f[i]=c(mapp[i].x+mapp[i].y-2,mapp[i].x-1);
        for (int j=1;j<i;j++) {
            if (mapp[j].x>mapp[i].x || mapp[j].y>mapp[i].y) continue;
            f[i]=f[i]-f[j]*c(mapp[i].x+mapp[i].y-mapp[j].x-mapp[j].y,mapp[i].x-mapp[j].x)%Moden;
        }
    }
    std::cout<<(f[n+1]+Moden)%Moden<<std::endl;

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using std::sort;
template<typename T>
inline bool read(T &x){
	x=0;char c;T f=1;
	do{
		c=getchar();
		if(c=='-')f=-1;
		else if(c==EOF)return false;
	}while(c>'9'||c<'0');
	do{
		x=x*10+c-'0';
		c=getchar();
	}while(c>='0'&&c<='9');
	return x*=f,true;
}
template<typename T>
inline bool updmax(T &x,T y){
	if(y>x)return x=y,true;
	return false;
}
template<typename T>
inline bool updmin(T &x,T y){
	if(y<x)return x=y,true;
	return false;
}

const long long mod=1000000007;
const int M=100010,N=2010;
long long f[M<<1],invf[M<<1];
long long g[N];
struct Point{
	int x,y;
	bool operator < (const Point b)const {
		if(x==b.x)return y<b.y;
		return x<b.x;
	}
}P[N];
inline long long Pow(long long x,long long y){
	long long res=1;
	while(y){
		if(y&1){
			res=res*x%mod;
		}
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int H,W;
int n;
inline long long C(int x,int y){
	return f[x]*invf[y]%mod*invf[x-y]%mod;
}
void init(){
	f[0]=1;
	invf[0]=1;
	for(register int i=1;i<=200000;++i){
		f[i]=1ll*f[i-1]*i%mod;
		invf[i]=Pow(f[i],mod-2);
	}
}
int main(){
	read(H);
	read(W);
	read(n);
	init();
	for(register int i=1;i<=n;++i){
		read(P[i].x);
		read(P[i].y);
	}
//	P[++n]=(Point){1,1};
	P[++n]=(Point){H,W};
	sort(P+1,P+1+n);
	for(register int i=1;i<=n;++i){
		g[i]=C(P[i].x+P[i].y-2,P[i].x-1);
		for(register int j=1;j<i;++j){
			if(P[j].x<=P[i].x&&P[j].y<=P[i].y){
				g[i]=(g[i]-C(P[i].x-P[j].x+P[i].y-P[j].y,P[i].x-P[j].x)*g[j]%mod+mod)%mod;
			}
		}
	}
	printf("%lld\n",g[n]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
const int mod=1e9+7;
int n,m,k,dp[maxn],fac[maxn],inv[maxn];

struct node{
	int x,y;
}a[maxn];

inline bool cmp(const node &a,const node &b){
	return (a.x!=b.x)?a.x<b.x:a.y<b.y;
}

inline int C(int n,int m){
    if(n<m) return 0;
    return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    fac[0]=fac[1]=inv[0]=inv[1]=1;
    for(int i=2;i<maxn;i++) fac[i]=1ll*fac[i-1]*i%mod;
    for(int i=2;i<maxn;i++) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(int i=2;i<maxn;i++) inv[i]=1ll*inv[i]*inv[i-1]%mod;
    for(int i=1;i<=k;i++) scanf("%d%d",&a[i].x,&a[i].y);
    a[++k].x=n;a[k].y=m;
    sort(a+1,a+k+1,cmp);
    for(int i=1;i<=k;i++){
    	dp[i]=C(a[i].x+a[i].y-2,a[i].x-1);
    	for(int j=1;j<i;j++)
    		if(a[j].x<=a[i].x&&a[j].y<=a[i].y) dp[i]=(dp[i]-1ll*dp[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mod+mod)%mod;
	}
	printf("%d\n",dp[k]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p=1e9+7;
struct node{
    int x,y;
}s[2005];
ll f[2005],c[200005];
ll qpow(ll x,ll k){
    ll ans=1;
    while(k){
        if(k&1){
            ans=ans*x%p;
        }
        k>>=1;
        x=x*x%p;
    }
    return ans;
}
ll C(int n,int k){
    return c[n]*qpow(c[k],p-2)%p*qpow(c[n-k],p-2)%p;
}
bool operator < (node a,node b){
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int h,w,n;
    scanf("%d%d%d",&h,&w,&n);
    c[0]=1;
    for(int i=1;i<=200000;i++){
        c[i]=(c[i-1]*i)%p;
    }
    ll res=0;

    for(int i=1;i<=n;i++){
        scanf("%d%d",&s[i].x,&s[i].y);
    }
    s[n+1].x=h,s[n+1].y=w;
    n++;

    sort(s+1,s+n+1);
    for(int i=1;i<=n;i++){
        f[i]=C(s[i].x+s[i].y-2,s[i].x-1);
        for(int j=1;j<=i-1;j++){
            if(s[j].y<=s[i].y){
                f[i]=(f[i]-f[j]*C(s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x)%p)%p;
            }
        }
    }

    if(f[n]<0)f[n]+=p;
    printf("%lld\n",f[n]);
    return 0;
}


#include<cstdio>
#include<algorithm>
const long long p=1e9+7;
const int maxn=2e5+11;
long long inv[maxn],mul[maxn],n,m,k,f[maxn];
struct G{
	long long x;
	long long y;
}dat[maxn];
//inline void make(){
//	inv[0]=inv[1]=mul[0]=1;
//	for(int i=2;i<=maxn-5;i++) inv[i]=(p-p/i)%p*inv[p%i]%p;
////	for(int i=1;i<=k<<1;i++)printf("%d\n",inv[i]);
//	for(int i=1;i<=maxn-5;i++){
//		inv[i]=(inv[i]*inv[i-1])%p;
//		mul[i]=(mul[i-1]*i)%p;
//	}
////	for(int i=1;i<=k<<1;i++) printf("%d %d\n",mul[i],inv[i]);
//}
inline long long fp(long long a,long long b,long long mod){
	long long tmp=1;
	while(b){
		if(b&1)tmp=(tmp*a)%mod;
		a=(a*a)%mod; b>>=1;
		//if(a==2ll)printf("tmp=%lld\n",tmp);
	}
	return tmp;
}
inline void make(){
	inv[0]=inv[1]=mul[0]=1;
	//printf("fp=%lld\n",fp(1ll,p-2,p));
	for(int i=1;i<=maxn-5;i++){
		mul[i]=(mul[i-1]*i)%p;
		inv[i]=fp(mul[i],p-2,p);
		//if(i<=k<<4)printf("%lld %lld %lld\n",mul[i],p-2,p);
	}
	//for(int i=1;i<=k<<4;i++) printf("%lld %lld\n",mul[i],inv[i]);
}
inline long long C(long long i,long long j){
	return mul[i]%p*inv[j]%p*inv[i-j]%p;
}
bool cmp(G a,G b){
	if(a.y==b.y)return a.x<b.x;
	return a.y<b.y;
}
inline long long path(G b,G a){
//	printf("%lld %lld %lld %lld\n",a.x,a.y,b.x,b.y);
	long long i=a.x-b.x+1,j=a.y-b.y+1;
//	printf("c=%lld %lld %lld\n",C(i+j-2,i-1),i+j-2,i-1);
//	if(i+j-2<0||i-1<0||j-1<0)return 0;
	if(i+j-2<0||i-1<0)return 0;
	return C(i+j-2,i-1);
}
int main(){
	scanf("%lld%lld%lld",&n,&m,&k);
	if(k==0){
		printf("%lld\n",C(n+m-2,n-1));
		return 0;
	}
	make();
//	printf("%d%d%d",n,m,k);
	for(long long i=1;i<=k;i++)
	{
		scanf("%lld%lld",&dat[i].x,&dat[i].y);
//		dat[i].x=1;
//		blc[i].x=1;
//		printf("%lld:%lld %lld\n",i,dat[i].x,dat[i].y);
	}
	dat[++k].x=n; dat[k].y=m;
	std::stable_sort(dat+1,dat+k+1,cmp);
//	for(long long i=1;i<=k;i++)printf("%lld:%lld %lld\n",i,dat[i].x,dat[i].y);
	for(long long i=1;i<=k;i++){
		f[i]=path((G){1,1},dat[i]);
		for(long long j=1;j<i;j++)
			f[i]-=(f[j]*path(dat[j],dat[i]))%p;
//		printf("%lld\n",f[i]);
	}
	printf("%lld\n",(f[k]+p)%p);
	return 0;
}
/*
6 6 6
6 1
3 2
5 3
2 4
1 6
5 6
*/


#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
struct hlydl{
	int x;
	int y;
}t[2005];
bool cmp(hlydl x,hlydl y)
{
	if(x.x!=y.x)
	return x.x<y.x;
	return x.y<y.y;
}
int f[2005];
int jc[200005];
int invjc[200005];
int mod=1e9+7;
int ksm(int a,int b)
{
	int ans=1,base=a;
	while(b)
	{
		if(b&1)
		ans=ans*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return ans;
}
int C(int x,int y)
{
	return jc[x]*invjc[y]%mod*invjc[x-y]%mod;
}
signed main()
{
	jc[0]=1;
	invjc[0]=1;
	for(int i=1;i<=200002;i++)
	jc[i]=jc[i-1]*i%mod;
	invjc[200002]=ksm(jc[200002],mod-2);
	for(int i=200001;i>=1;i--)
	invjc[i]=invjc[i+1]*(i+1)%mod;
	int h,w,n;
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;i++)
	scanf("%lld%lld",&t[i].x,&t[i].y);
	sort(t+1,t+n+1,cmp);
	t[n+1].x=h,t[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(t[i].x+t[i].y-2,t[i].x-1);
		for(int j=1;j<i;j++)
		{
			if(t[i].x<t[j].x||t[i].y<t[j].y)
			continue;
			f[i]=(f[i]-f[j]*C(t[i].x-t[j].x+t[i].y-t[j].y,t[i].x-t[j].x))%mod;
		}
	}
	cout<<(f[n+1]+mod)%mod;
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10,M = 2e3 + 10,mod = 1e9 + 7;

typedef pair<int,int> PII;

PII a[M];

int f[M];

int fac[N << 1],invfac[N << 1];

int C(int n,int m){return n<m?0:(long long)fac[n]*invfac[m]%mod*invfac[n-m]%mod;}

void init(){
    fac[0]=invfac[0]=invfac[1]=1;
    for(int i=1;i<=2e5 + 10;i++)fac[i]=(long long)fac[i-1]*i%mod;
    for(int i=2;i<=2e5 + 10;i++)invfac[i]=(long long)(mod-mod/i)*invfac[mod%i]%mod;
    for(int i=2;i<=2e5 + 10;i++)invfac[i]=(long long)invfac[i-1]*invfac[i]%mod;
}

int main(){
    init();
    int h,w,n;
    cin >> h >> w >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i].first >> a[i].second;
    }
    sort(a + 1,a + n + 1);
    a[n + 1].first = h,a[n + 1].second = w;
    for (int i = 1; i <= n + 1; i++){
        f[i] = C(a[i].first+a[i].second - 2,a[i].first - 1);
        for (int j = 1; j < i; j++){
            if (a[j].first > a[i].first || a[j].second > a[i].second) continue;
            f[i] = (f[i] - 1ll * f[j] * C(a[i].first-a[j].first+a[i].second-a[j].second,a[i].first-a[j].first)) % mod;
        }
    }
    cout << (f[n + 1] + mod) % mod;
    return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#define il inline
#define reg register
#define f(i, s, t) for (reg int i = s; i != t; i++)
using namespace std;
typedef long long ll;

const int SIZE = 2006, P = 1000000007;
int H, W, N, f[SIZE];
ll ft[200006], ftInv[200006];
pair<int, int> a[SIZE];

il ll pow(ll a, int n) {
  reg ll ans = 1;
  for (; n; n >>= 1, a = a * a % P)
    if (n & 1) ans = ans * a % P;
  return ans;
}
il int C(int n, int m) { return ft[n] * ftInv[m] % P * ftInv[n - m] % P; }

int main() {
  ft[0] = 1, ftInv[0] = 1;
  f(i, 1, 200001) {
    ft[i] = ft[i - 1] * i % P;
    ftInv[i] = pow(ft[i], P - 2);
  }
  scanf("%d%d%d", &H, &W, &N);
  f(i, 1, N + 1) scanf("%d%d", &a[i].first, &a[i].second);
  sort(a + 1, a + 1 + N);
  a[N + 1].first = H, a[N + 1].second = W;
  f(i, 1, N + 2) {
    f[i] = C(a[i].first + a[i].second - 2, a[i].first - 1);
    f(j, 1, i) {
      if (a[j].first > a[i].first || a[j].second > a[i].second) continue;
      f[i] = (f[i] - (ll)f[j] *
                         C(a[i].first - a[j].first + a[i].second - a[j].second,
                           a[i].first - a[j].first) %
                         P) %
             P;
    }
  }
  printf("%d\n", (f[N + 1] + P) % P);
}


#include<bits/stdc++.h>
using namespace std;
#define go(i,a,b) for(int i=(a);i<=(b);++(i))
#define com(i,a,b) for(int i=(a);i>=(b);--(i))
#define mem(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define fin freopen("input.txt","r",stdin)
#define fout freopen("output.txt","w",stdout)
typedef long long ll;
const int maxn=1e5+100;
const int mod=1e9+7;
#define int long long
int n,m,cnt,f[2010],in[maxn+10],inv[maxn+10];
struct node{
	int x,y;
}p[2010];

int c(int i,int j){
	return in[i]*inv[j]%mod*inv[i-j]%mod;
}

int fast(int x,int p){
	int ans=1;
	for(;p;p>>=1,x=x*x%mod){
		if(p&1) ans=ans*x%mod;
	}
	return ans;
}

void init(){
	in[0]=inv[0]=1;
	go(i,1,maxn){
		in[i]=in[i-1]*i%mod;
		inv[i]=fast(in[i],mod-2);
	}
}

bool cmp(const node &a,const node &b){
	return a.x<b.x;
}
#define x(i) p[i].x
#define y(i) p[i].y
signed main()
{
	//fin;
	scanf("%lld%lld%lld",&n,&m,&cnt);
	int x,y;
	go(i,1,cnt){
		scanf("%lld%lld",&x,&y);
		p[i]=(node){x,y};
	}
	p[++cnt]=(node){n,m};
	sort(p+1,p+cnt+1,cmp);
	init();
	go(i,1,cnt){
		f[i]=c(p[i].x+p[i].y-2,p[i].x-1);
		go(j,1,i-1){
			if(p[j].y>p[i].y) continue;
			f[i]=((f[i]-f[j]*c(x(i)-x(j)+y(i)-y(j),x(i)-x(j)))%mod+mod)%mod;
		}
	}
	printf("%lld",f[cnt]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int N=2010,mod=1000000007;
ll jc[200010],jcinv[200010];
int n,w,h,f[N];
struct Node
{
	int x,y;
}a[N];

bool operator<(const Node &a,const Node &b){
	return a.x<b.x;
}

int C(int n,int m)
{
	return jc[n]*jcinv[m]%mod*jcinv[n-m]%mod;
}

ll ksm(ll x,int y)
{
	ll ans=1;
	while(y)
	{
		if(y%2==1) ans=(ans*x)%mod;
		x=(x*x)%mod;
		y=y/2;
	}
	return ans;
}
//m在上,n在下

int main()
{

	jc[0]=1;jcinv[0]=1;
	for(int i=1;i<=200000;i++)
	{
		jc[i]=jc[i-1]*i%mod;
		jcinv[i]=ksm(jc[i],mod-2);
	}
	cin>>h>>w>>n;
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1);
	a[n+1].x=h; a[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(a[i].x-1+a[i].y-1,a[i].x-1);
		for(int j=0;j<i;j++)
		{
			if(a[i].x<a[j].x||a[i].y<a[j].y) continue;
			f[i]=(f[i]-(long long)f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x))%mod;
		}
	}
	cout<<f[n+1];
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int h,w,n;
long long f[4000],jc[300000],inv[300000],mo;
struct node
{
	int x,y;
}a[5000];
inline bool cmp(node x,node y)
{
	if (x.x!=y.x) return x.x<y.x;
	else return x.y<y.y;
}
inline long long power(long long u,long long b)
{
	long long s=1;
	while (b)
	{
		if (b&1) s=s*u%mo;
		u=u*u%mo;
		b>>=1;
	}
	return s;
}
inline long long c(int u,int v)
{
	return jc[u]*inv[v]%mo*inv[u-v]%mo;
}
int main()
{
	mo=1000000007;
	scanf("%d%d%d",&h,&w,&n);
	jc[0]=0;
	jc[1]=1;
	int sd=200001;
	for (int i=2;i<=sd+1;i++)jc[i]=(jc[i-1]*i)%mo;
	inv[sd+1]=power(jc[sd+1],1LL*(mo-2));
	for (int i=sd;i>=0;i--)inv[i]=inv[i+1]*1LL*(i+1)%mo;
	for (int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp);
	n++;
	a[n].x=h;a[n].y=w;
	for (int i=1;i<=n;i++)
	{
		long long sum=0;
		f[i]=c(a[i].x+a[i].y-2,a[i].x-1);
		for (int j=1;j<i;j++)
		{
			if (a[j].x>a[i].x||a[j].y>a[i].y) continue;
			sum=(sum+f[j]*c(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mo)%mo;
		}
		f[i]-=sum;
	}
	printf("%lld",f[n]);
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define MOD(x) x>=mod?x-mod:x
using namespace std;
const int maxn=500010,mod=1e9+7;
struct poi{int x,y;}a[maxn];
int h,w,n;
int fac[maxn],f[maxn],inv[maxn];
void read(int &k)
{
    int f=1;k=0;char c=getchar();
    while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar();
    k*=f;
}
bool cmp(poi a,poi b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int power(int a,int b)
{
    int t=1;
    for(;b;a=1ll*a*a%mod,b>>=1)
    if(b&1)t=1ll*t*a%mod;
    return t;
}
int C(int n,int m){return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}
int main()
{
    read(h);read(w);read(n);
    for(int i=1;i<=n;i++)read(a[i].x),read(a[i].y);
    a[++n].x=h;a[n].y=w;sort(a+1,a+1+n,cmp);
    fac[0]=inv[0]=1;for(int i=1;i<=h+w;i++)fac[i]=1ll*fac[i-1]*i%mod;
    for(int i=1;i<=h+w;i++)inv[i]=power(fac[i],mod-2);
    for(int i=1;i<=n;i++)
    {
        f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
        for(int j=1;j<i;j++)
        if(a[j].x<=a[i].x&&a[j].y<=a[i].y)
        f[i]=MOD(1ll*f[i]-1ll*f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mod+mod);
    }
    printf("%d\n",f[n]);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define maxn 100005
#define maxx 2005
using namespace std;
int h,w,n;
ll dp[maxn];
ll fac[maxx<<1],inv_fac[maxx<<1],inv[maxx<<1];
struct node
{
	int x,y;
}dis[maxn];

ll cal(ll a,ll b)
{
	if(a<b || a<0 || b<0)
		return 0;
	return fac[a]*inv_fac[b] %mod *inv_fac[a-b]%mod;
}

bool cmp(node a,node b)
{
	return a.x<b.x;
}

int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++)
		scanf("%d %d",&dis[i].x,&dis[i].y);
	dis[n+1].x=h;
	dis[n+1].y=w;
	fac[0]=1;
	inv[1]=1;
	inv_fac[1]=1;
	inv_fac[0]=1;
	for(int i=1;i<=h+w;i++)
	{
		fac[i]=fac[i-1]*i%mod;
		if(i>1)
		{
			inv[i]=inv[mod%i]*(mod-mod/i)%mod;
			inv_fac[i]=inv_fac[i-1]*inv[i]%mod;
		}
	}
	sort(dis+1,dis+n+2,cmp);
	for(int i=1;i<=n+1;i++)
	{
		int xx=dis[i].x,yy=dis[i].y;
		dp[i]=cal(xx+yy-2,xx-1)%mod;
		for(int j=1;j<i;j++)
		{
			int xxx=dis[j].x,yyy=dis[j].y;
			dp[i]=(dp[i]+mod-dp[j]%mod*cal(xx+yy-xxx-yyy,xx-xxx)%mod)%mod;

		}
	}
	printf("%lld",(dp[n+1]%mod+mod)%mod);
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
const int M=1e9+7;
int h,w,n;
ll f[10005][10005];
int a[10005][10005];
int main(){
//	freopen("zx.txt","r",stdin);
    cin>>h>>w>>n;
    for(int i=1;i<=n;i++){
    	int x,y;
    	cin>>x>>y;
    	a[x][y]=1;
	}
	for(int i=1;i<=h;i++) f[i][1]=1;
	for(int i=1;i<=w;i++) f[1][i]=1;
	for(int i=2;i<=h;i++){
		for(int j=2;j<=w;j++){
			if(!a[i][j])
				f[i][j]=(f[i-1][j]+f[i][j-1])%M;
		}
	}
	cout<<f[h][w];
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
#define mod 1000000007
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
    	n=n*10+ch-'0';
    	ch=getchar();
    }
    return n*f;
}
int x[2001],y[2001],dp[2001],pos[2001],jc[400001],njc[400001];
int ksm(int n,int k)
{
	if(k==0)return 1;
	if(k==1)return n%mod;
	int nn=ksm(n,k/2);
	if(k%2==0)return 1LL*nn*nn%mod;
	else return 1LL*nn*nn%mod*1LL*n%mod;
}
bool bi(int c,int d)
{
	return x[c]<x[d];
}
signed main()
{
	int h,w,n;
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&x[i],&y[i]),pos[i]=i;
	x[n+1]=h;
	y[n+1]=w;
	pos[n+1]=n+1;
	sort(pos+1,pos+n+2,bi);
	jc[0]=1;
	for(int i=1;i<=400000;i++)jc[i]=1LL*i*jc[i-1]%mod;
	njc[400000]=ksm(jc[400000],mod-2);
	for(int i=399999;i>=0;i--){njc[i]=1LL*(i+1)*njc[i+1]%mod;}
	for(int i=1;i<=n+1;i++)
	{
		dp[pos[i]]=1LL*jc[x[pos[i]]+y[pos[i]]-2]*njc[x[pos[i]]-1]%mod*1LL*njc[y[pos[i]]-1]%mod;
		for(int j=1;j<i;j++)if(y[pos[j]]<=y[pos[i]])dp[pos[i]]=(dp[pos[i]]-1LL*dp[pos[j]]*jc[x[pos[i]]+y[pos[i]]-x[pos[j]]-y[pos[j]]]%mod*1LL*njc[x[pos[i]]-x[pos[j]]]%mod*1LL*njc[y[pos[i]]-y[pos[j]]]%mod+mod)%mod;
	}
    cout<<dp[pos[n+1]]<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int N=2020;
const int M=20200;
int f[N];
struct pnt
{
	int xi,yi;
}d[N];
bool operator<(pnt a,pnt b)
{
	return a.yi<b.yi||a.yi==b.yi&&a.xi<b.xi;
}
int jc[M],jcv[M];
int n,h,w;
int ksm(int x,int k)
{
	int res=1;
	while(k)
	{
		if(k&1) res=1ll*res*x%mod;
		k>>=1;x=1ll*x*x%mod;
	}
	return res;
}
int C(int n,int m)
{
	return 1ll*jc[n]*jcv[m]%mod*jcv[n-m]%mod;
}
int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&d[i].xi,&d[i].yi);
	sort(d+1,d+1+n);
	jc[0]=1;
	for(int i=1;i<M;i++)
	   jc[i]=1ll*jc[i-1]*i%mod;
	for(int i=0;i<M;i++)
	   jcv[i]=ksm(jc[i],mod-2);
	d[++n].xi=h,d[n].yi=w;
	for(int i=1;i<=n;i++)
	{
	    f[i]=C(d[i].xi+d[i].yi-2,d[i].xi-1);
		//printf("%d\n",f[i]);
		for(int j=1;j<i;j++)
		   if(d[j].xi<=d[i].xi&&d[j].yi<=d[i].yi)
		       f[i]-=1ll*C(d[i].xi+d[i].yi-d[j].xi-d[j].yi,d[i].xi-d[j].xi)*f[j]%mod,f[i]=(f[i]+mod)%mod;
		//printf("%d\n",f[i]);
	}
	printf("%d",(f[n]+mod)%mod);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 200005
#define mod 1000000007
#define ll long long
using namespace std;
int h,w,n;
ll inv[MAXN];
ll fact[MAXN];
ll dp[MAXN];
struct node
{
	int x,y;
}a[MAXN];
bool cmp(node a,node b)
{
	if(a.x!=b.x) return a.x<b.x;
	else return a.y<b.y;
}
void Init()
{
	inv[1]=1;
	inv[0]=1;
	fact[1]=1;
	for(int i=2;i<=MAXN;i++)
	{
		fact[i]=fact[i-1]%mod*i%mod;
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;

	}
	for(int i=2;i<=MAXN;i++)
	{
		inv[i]=inv[i]%mod*inv[i-1]%mod;
	}
}
ll C(int x,int y)
{
     return fact[x]%mod*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
	cin>>h>>w>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].x,&a[i].y);
	}
	a[n+1].x=h;a[n+1].y=w;
	sort(a+1,a+1+n,cmp);
	Init();
	for(int i=1;i<=n+1;i++)
	{
		int xi=a[i].x,yi=a[i].y;
		dp[i]=C(xi+yi-2,xi-1);
		for(int j=1;j<i;j++)
		{
			int xj=a[j].x,yj=a[j].y;
			if(xi<xj||yi<yj)continue;
			dp[i]=(dp[i]-dp[j]*C(xi-xj+yi-yj,xi-xj))%mod;
		}
		dp[i]=(dp[i]%mod+mod)%mod;
	}
	cout<<dp[n+1];
}


#include<bits/stdc++.h>
#define re register
#define il inline
#define REP(i,a,b) for(re int i=(a);i<=(b);++i)
#define DEP(i,a,b) for(re int i=(a);i>=(b);--i)
#define LL long long
#define int long long
using namespace std;
template<typename T>il void read(T &ff){
	T rr=1;ff=0;re char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')rr=-1;ch=getchar();}
	while(isdigit(ch)){ff=(ff<<1)+(ff<<3)+(ch^48);ch=getchar();}
	ff*=rr;
}
const int N=2020,M=2e5+7,MOD=1e9+7;
int h,w,n,x[N],y[N],b[N];
LL f[N],inv[M],jc[M];
il LL power(re LL a,re LL b){
	LL res=1; for(;b;b>>=1,a=a*a%MOD)if(b&1)res=res*a%MOD;
	return res;
}
il bool cmp(re int a,re int b){return x[a]==x[b]?y[a]<y[b]:x[a]<x[b];}
il LL C(re int n,re int m){return jc[m]*inv[m-n]%MOD*inv[n]%MOD;}
signed main(){
	read(h),read(w),read(n); REP(i,1,n)read(x[i]),read(y[i]),b[i]=i;
	jc[0]=inv[0]=1; REP(i,1,N-1)jc[i]=jc[i-1]*i%MOD,inv[i]=power(jc[i],MOD-2);
	f[0]=1,b[0]=0,b[n+1]=n+1,x[0]=y[0]=1,x[n+1]=h,y[n+1]=w;
	sort(b,b+n+2,cmp);
	REP(i,1,n+1)f[i]=C(x[i]-1,x[i]+y[i]-2);
	REP(i,1,n+1) REP(j,1,i-1) if(x[j]<=x[i]&&y[j]<=y[i])
		f[i]=(f[i]-f[j]*C(x[i]-x[j],x[i]-x[j]+y[i]-y[j])%MOD+MOD)%MOD;
	printf("%lld",f[n+1]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
struct hlydl{
	int x;
	int y;
}t[2005];
bool cmp(hlydl x,hlydl y)
{
	if(x.x!=y.x)
	return x.x<y.x;
	return x.y<y.y;
}
int f[2005];
int jc[100005];
int invjc[100005];
int mod=1e9+7;
int ksm(int a,int b)
{
	int ans=1,base=a;
	while(b)
	{
		if(b&1)
		ans=ans*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return ans;
}
int C(int x,int y)
{
	return jc[x]*invjc[y]%mod*invjc[x-y]%mod;
}
signed main()
{
	jc[0]=1;
	invjc[0]=1;
	for(int i=1;i<=100002;i++)
	jc[i]=jc[i-1]*i%mod;
	invjc[100002]=ksm(jc[100002],mod-2);
	for(int i=100001;i>=1;i--)
	invjc[i]=invjc[i+1]*(i+1)%mod;
	int h,w,n;
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;i++)
	scanf("%lld%lld",&t[i].x,&t[i].y);
	sort(t+1,t+n+1,cmp);
	t[n+1].x=h,t[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(t[i].x+t[i].y-2,t[i].x-1);
		for(int j=1;j<i;j++)
		{
			if(t[i].x<t[j].x||t[i].y<t[j].y)
			continue;
			f[i]=(f[i]-f[j]*C(t[i].x-t[j].x+t[i].y-t[j].y,t[i].x-t[j].x))%mod;
		}
	}
	cout<<f[n+1];
	return 0;
}


#include<cstdio>
#include<algorithm>
const long long p=1e9+7;
const int maxn=2e5+11;
long long inv[maxn],mul[maxn],n,m,k,f[maxn];
struct G{
	long long x;
	long long y;
}dat[maxn];
//inline void make(){
//	inv[0]=inv[1]=mul[0]=1;
//	for(int i=2;i<=maxn-5;i++) inv[i]=(p-p/i)%p*inv[p%i]%p;
////	for(int i=1;i<=k<<1;i++)printf("%d\n",inv[i]);
//	for(int i=1;i<=maxn-5;i++){
//		inv[i]=(inv[i]*inv[i-1])%p;
//		mul[i]=(mul[i-1]*i)%p;
//	}
////	for(int i=1;i<=k<<1;i++) printf("%d %d\n",mul[i],inv[i]);
//}
inline long long fp(long long a,long long b,long long mod){
	long long tmp=1;
	while(b){
		if(b&1)tmp=(tmp*a)%mod;
		a=(a*a)%mod; b>>=1;
		//if(a==2ll)printf("tmp=%lld\n",tmp);
	}
	return tmp;
}
inline void make(){
	inv[0]=inv[1]=mul[0]=1;
	//printf("fp=%lld\n",fp(1ll,p-2,p));
	for(int i=1;i<=maxn-5;i++){
		mul[i]=(mul[i-1]*i)%p;
		inv[i]=fp(mul[i],p-2,p);
		//if(i<=k<<4)printf("%lld %lld %lld\n",mul[i],p-2,p);
	}
	//for(int i=1;i<=k<<4;i++) printf("%lld %lld\n",mul[i],inv[i]);
}
inline long long C(long long i,long long j){
	return mul[i]%p*inv[j]%p*inv[i-j]%p;
}
bool cmp(G a,G b){
	if(a.y==b.y)return a.x<b.x;
	return a.y<b.y;
}
inline long long path(G b,G a){
//	printf("%lld %lld %lld %lld\n",a.x,a.y,b.x,b.y);
	long long i=a.x-b.x+1,j=a.y-b.y+1;
//	printf("c=%lld %lld %lld\n",C(i+j-2,i-1),i+j-2,i-1);
//	if(i+j-2<0||i-1<0||j-1<0)return 0;
	if(i+j-2<0||i-1<0)return 0;
	return C(i+j-2,i-1);
}
int main(){
	scanf("%lld%lld%lld",&n,&m,&k);
	if(k==0){
		printf("%lld\n",C(n+m-2,n-1));
		return 0;
	}
	make();
//	printf("%d%d%d",n,m,k);
	for(long long i=1;i<=k;i++)
	{
		scanf("%lld%lld",&dat[i].x,&dat[i].y);
//		dat[i].x=1;
//		blc[i].x=1;
//		printf("%lld:%lld %lld\n",i,dat[i].x,dat[i].y);
	}
	dat[++k].x=n; dat[k].y=m;
	std::stable_sort(dat+1,dat+k+1,cmp);
//	for(long long i=1;i<=k;i++)printf("%lld:%lld %lld\n",i,dat[i].x,dat[i].y);
	for(long long i=1;i<=k;i++){
		f[i]=path((G){1,1},dat[i]);
		for(long long j=1;j<i;j++)
			if(dat[j].x<=dat[i].x&&dat[j].y<=dat[i].y)
			f[i]-=(f[j]*path(dat[j],dat[i]))%p;
//		printf("%lld\n",f[i]);
	}
	printf("%lld\n",(f[k]+p)%p);
	return 0;
}
/*
6 6 6
6 1
3 2
5 3
2 4
1 6
5 6
*/


#include <iostream>
#include <cstdio>
#define ll long long
#define mod 1000000007
using namespace std;
int h,w,n;
int map[1005][1005];
ll dp[1005][1005];

int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		map[x][y]=1;
	}

	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			if(i==1 && j==1)
			{
				dp[i][j]=1;
				if(map[i][j])
					dp[i][j]=0;
				continue;
			}
			dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
			if(map[i][j])
				dp[i][j]=0;
		}
	}
	printf("%d",dp[h][w]);
}


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

inline int read() {
	int num = 0, sym = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			sym = -1;
		ch = getchar();
	}
	while ('0' <= ch && ch <= '9') {
		num = (num << 1) + (num << 3) + (ch ^ 48);
		ch = getchar();
	}
	return num * sym;
}

const int maxn = 200000 + 10;
const int mod = 1000000007;

struct ass {
	int x, y;
}a[maxn];

int H, W, N, f[maxn];
long long jc[maxn], jcinv[maxn];

int C(int n, int m) {
	return jc[m] * jcinv[n] % mod * jcinv[m - n] % mod;
}

long long power(long long a, int b) {
	long long c = 1;
	for (; b; b >>= 1) {
		if (b & 1)
			c = c * a % mod;
		a = a * a % mod;
	}
	return c;
}

bool cmp(ass e, ass f) {
	if (e.x <= f.x && e.y <= f.y)
		return true;
	else
		return false;
}

int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	jc[0] = 1;
	jcinv[0] = 1;
	for (int i = 1; i <= 200000; i++) {
		jc[i] = jc[i - 1] * i % mod;
		jcinv[i] = power(jc[i], mod - 2);
	}
	H = read();
	W = read();
	N = read();
	for (int i = 1; i <= N; i++) {
		a[i].x = read();
		a[i].y = read();
	}
	sort(a + 1, a + N + 1, cmp);
	a[N + 1].x = H;
	a[N + 1].y = W;
	for (int i = 1; i <= N + 1; i++) {
		f[i] = C(a[i].x - 1, (a[i].x - 1) + (a[i].y - 1));
		for (int j = 1; j < i; j++) {
			if (a[j].x > a[i].x || a[j].y > a[i].y)
				continue;
			f[i] = (f[i] - (long long)f[j] * C(a[i].x - a[j].x, (a[i].x - a[j].x) + (a[i].y - a[j].y))) % mod;
		}
	}
	printf("%lld", (f[N + 1] + mod) % mod);
	return 0;
}


#include<cstdio>
#include<algorithm>
#define int long long
int p=1e9+7;
using namespace std;
struct node{
	int x,y;
}nod[1000000];
bool cmp(node x,node y)
{
	if(x.x==y.x)return x.y<y.y;
	return x.x<y.x;
}
int ksm(int x,int k)
{
	int sum=1;
	while(k)
	{
		if(k&1)sum=sum*x%p;
		x=x*x%p;
		k>>=1;
	}
	return sum;
}
int jx[1000100],h,w,n,dp[1000000],inv[1000100];
int C(int x,int y)
{
	return jx[y]*inv[y-x]%p*inv[x]%p;
}
signed main()
{
	jx[0]=1;inv[0]=1;//求jx求逆元 初始化
	for(int i=1;i<=100100;++i)
	{
		jx[i]=(jx[i-1]*i)%p;
		inv[i]=ksm(jx[i],p-2)%p;
	}
	scanf("%lld%lld%lld",&h,&w,&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld%lld",&nod[i].x,&nod[i].y);
	}
	nod[n+1].x=h;
	nod[n+1].y=w;
	sort(nod+1,nod+n+1,cmp);
	for(int i=1;i<=n+1;++i)
	{
		dp[i]=C(nod[i].x-1,nod[i].x+nod[i].y-2);
		for(int j=1;j<i;++j)
		{
			if(nod[i].x<nod[j].x||nod[i].y<nod[j].y)continue;
			dp[i]-=dp[j]*C(nod[i].x-nod[j].x,nod[i].x+nod[i].y-nod[j].x-nod[j].y)%p;//到 x,y 的所有情况减去所有有黑点的情况
			dp[i]=(dp[i]+p)%p;
		}
	}
	printf("%lld",(dp[n+1]+p)%p);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) ((x)*(x))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int mn = 2005;
const int mod = 1e9 + 7;
#define P mod

int n, m, k;

ll d[mn];
ll powmod(ll a, ll b, ll P) {
    ll t = 1;
    for (; b; b >>= 1, a = a*a% P)
        if (b & 1) t = (t*a%P);
    return t;
}
const int U=210005;
int F[U + 3], Finv[U + 3];
void init() {
    F[0] = 1;
    for (int i = 1; i <= U; i++) F[i] = 1ll * F[i - 1] * i % P;
    Finv[U] = powmod(F[U],P-2,P);
    for (int i = U; i; i--) Finv[i - 1] = 1ll * Finv[i] * i % P;
}

int comb(int n, int m) {
    if (m < 0 || m > n) return 0;
    return F[n] * 1ll * Finv[n - m] % P * Finv[m] % P;
}

struct node{
    int x,y;
    bool operator < (const node &p)const{
        return x==p.x?y<p.y:x<p.x;
    }
}p[mn];
int main() {
    cin >> n >> m >> k;
    init();
    for (int i = 1; i <= k; i++) {
        scanf("%d%d", &p[i].x, &p[i].y);
    }
    p[k+1]={n,m};
    sort(p+1,p+k+1);
    for (int i = 1; i <= k+1; i++) {
        d[i]=comb(p[i].x-1+p[i].y-1,min(p[i].x,p[i].y)-1);
        for (int j = 1; j < i; j++) {
            if (p[i].x < p[j].x || p[i].y < p[j].y) continue;
            d[i]-=d[j]*comb(p[i].x+p[i].y-p[j].x-p[j].y,min(p[i].x-p[j].x,p[i].y-p[j].y));
            d[i]%=mod;
        }
        d[i]=(d[i]%mod+mod)%mod;
    }
    printf("%lld\n",d[k+1]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
const int MAXN= 2003;
const int MAXM = 1e5 + 3;
const ll mod = 1e9 + 7;
ll f[MAXN];
int h , w ;
struct node{
    int x , y;
    friend bool operator < ( node a , node b ){
        if( a.x == b.x )
            return a.y < b.y;
        return a.x < b.x;
    }
}a[MAXN];
int n;
ll nic[MAXM] , jc[MAXM];
void pre(){
    nic[0] = nic[1] = jc[0] = jc[1] = 1;
    for( int i = 2 ; i <= MAXM - 3 ; i ++ ){
        jc[i] = jc[i-1] * i % mod;
        nic[i] = nic[mod%i] * ( mod - mod / i ) % mod;
    }
    for( int i = 1 ; i <= MAXM - 3 ; i ++ ){
        nic[i] = nic[i-1] * nic[i] % mod;
    }
}
ll C( ll n , ll m ){
    if( n < m ) return 0;
    return jc[n] * nic[m] % mod * nic[n-m] % mod;
}
ll abs_( ll x ) {
    if( x < 0 )
        return -x;
    return x;
}
int main(){
    scanf( "%d%d%d" , &h , &w , &n );
    pre();
    for( int i = 1 ; i <= n ; i ++ ){
        scanf( "%d%d" , &a[i].x , &a[i].y );
    }
    a[++n].x = h , a[n].y  = w;
    sort( a + 1 , a + n + 1) ;
    memset( f, 0 , sizeof( f ) );
    f[1] = C( a[1].x + a[1].y - 2 , a[1].x - 1 ) % mod;
    for( int i = 2 ; i <= n ; i ++ ){
        f[i] = C( a[i].x + a[i].y - 2 , a[i].x - 1 ) % mod;
        for( int j = 1 ; j < i ; j ++ ){
            f[i] = ( ((f[i] - ( f[j] * C( a[i].x + a[i].y - a[j].x - a[j].y , a[i].x - a[j].x) % mod  ))%mod+ mod)% mod ) % mod;
        }
    }
    printf( "%lld\n" , f[n] );
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5,mod=1e9+7,P=1e5;
int n,m,k;
struct point{
	int x,y;
	bool operator <(const point &a)const{
		if(a.x!=x)return x<a.x;
		return y<a.y;
	}
}s[maxn];
int fastmod(int x,int p){
	int res=1;
	while(p){
		if(p&1)res=(1ll*res*x)%mod;
		x=1ll*x*x%mod;
		p>>=1;
	}
	return res%mod;
}

int Ft[maxn],F[maxn];
int C(int x,int y){//x>=y
	if(x<y)return 0;
	return 1ll*F[x]*Ft[y]%mod*Ft[x-y]%mod;
}
void init(){
	F[0]=1;
	for(int i=1;i<=P;++i){
		F[i]=1ll*F[i-1]*i%mod;

	}

	Ft[P]=fastmod(F[P],mod-2);
	for(int i=P-1;i>=1;--i){
		Ft[i]=1ll*Ft[i+1]*(i+1)%mod;
	}
	Ft[0]=1;
}
int dp[maxn];//表示从左上角到第i个黑块的路径数量
int main(){
	init();
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=k;++i)
	{
		scanf("%d%d",&s[i].x,&s[i].y);
	}
	s[++k]=(point){n,m};
	sort(s+1,s+k+1);
	for(int i=1;i<=k;++i){
		dp[i]=C(s[i].x+s[i].y-2,s[i].x-1);
		//printf("i=%d %d\n",i,dp[i]);
	}
	for(int i=1;i<=k;++i)
		for(int j=1;j<i;++j){
			if(s[i].x<s[j].x || s[i].y<s[j].y)continue;
			dp[i]=(dp[i]-1ll*dp[j]*C(s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x)%mod +mod)%mod;
		//	printf("%d %d %d\n",s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x,C(s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x));
		//	printf("i=%d j=%d %d %d\n",i,j,dp[i],dp[j]);
		}
	printf("%d\n",dp[k]%mod);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=1e5+5,mod=1e9+7,P=1e5;
ll n,m,k;
struct point{
	ll x,y;
	bool operator <(const point &a)const{
		if(a.x!=x)return x<a.x;
		return y<a.y;
	}
}s[maxn];
ll fastmod(ll x,ll p){
	ll res=1;
	while(p){
		if(p&1)res=(1ll*res*x)%mod;
		x=1ll*x*x%mod;
		p>>=1;
	}
	return res%mod;
}

ll Ft[maxn],F[maxn];
ll C(ll x,ll y){//x>=y
	if(x<y)return 0;
	return 1ll*F[x]*Ft[y]%mod*1ll*Ft[x-y]%mod;
}
void init(){
	F[0]=1;
	for(ll i=1;i<=P;++i){
		F[i]=1ll*F[i-1]*i%mod;

	}

	Ft[P]=fastmod(F[P],mod-2);
	for(ll i=P-1;i>=1;--i){
		Ft[i]=1ll*Ft[i+1]*(i+1)%mod;
	}
	Ft[0]=1;
}
ll dp[maxn];//表示从左上角到第i个黑块的路径数量
int main(){
	init();
	scanf("%lld%lld%lld",&n,&m,&k);
	for(ll i=1;i<=k;++i)
	{
		scanf("%lld%lld",&s[i].x,&s[i].y);
	}
	s[++k]=(point){n,m};
	sort(s+1,s+k+1);
	for(ll i=1;i<=k;++i){
		dp[i]=C(s[i].x+s[i].y-2,s[i].x-1);
		//printf("i=%d %d\n",i,dp[i]);
	}
	for(ll i=1;i<=k;++i)
		for(ll j=1;j<i;++j){
			if(s[i].x<s[j].x || s[i].y<s[j].y)continue;
			dp[i]=(dp[i]-1ll*dp[j]*C(s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x)%mod +mod)%mod;
		//	printf("%d %d %d\n",s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x,C(s[i].x+s[i].y-s[j].x-s[j].y,s[i].x-s[j].x));
		//	printf("i=%d j=%d %d %d\n",i,j,dp[i],dp[j]);
		}
	printf("%lld\n",dp[k]%mod);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2009;
const int M=1e5+9;
const int P=1e9+7;
ll f[N],inv[M],invn[M];
int h,w,n;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y&1) res=res*x%P;
		x=x*x%P;
		y>>=1;
	}
	return res;
}
ll C(int x,int y) {return inv[x]*invn[y]%P*invn[x-y]%P;}
struct node
{
	int x,y;
}a[N];
bool cmp(node a,node b)
{
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}

int main()
{
	scanf("%d%d%d",&h,&w,&n);
	inv[0]=1,invn[0]=1;
	for(int i=1;i<=M-9;i++)
		inv[i]=inv[i-1]*i%P,invn[i]=power(inv[i],P-2);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp);
	a[n+1].x=h,a[n+1].y=w;
	for(int i=1;i<=n+1;i++)
	{
		f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
		for(int j=1;j<i;j++)
		{
			if(a[j].x>a[i].x || a[j].y>a[i].y) continue;
			f[i]=(f[i]-f[j]*C(a[i].x+a[i].y-a[j].x-a[j].y,a[i].x-a[j].x))%P;
		}
	}
	printf("%lld\n",(f[n+1]%P+P)%P);
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define N 100010
#define ll long long
using namespace std;
int n,m,k;
struct node{int x,y;}o[3010];
bool cmp(node a,node b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
ll mul[N<<1],inv[N<<1],dp[N],ans;
ll C(int n,int m){return mul[n]*inv[m]%mod*inv[n-m]%mod;}
int a[1010][1010],f[1010][1010];
void part()
{
	for(int i = 1;i<=k;i++)
    {
    	int x,y;
    	scanf("%d%d",&x,&y);
    	a[x][y] = 1;
    }
    for(int i = 1;i<=n;i++)
    	for(int j = 1;j<=m;j++)
    		if(!a[i][j])f[i][j] =(f[i-1][j]+f[i][j-1])%mod;
	cout<<f[n][m],exit(0);
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    if(n<=1000&&m<=1000)part();
    mul[0] = mul[1] = inv[0] = inv[1] = 1;
    for(int i = 2;i<=n<<1;i++)mul[i] = mul[i-1]*i%mod;
    for(int i = 2;i<=n<<1;i++)inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(int i = 2;i<=n<<1;i++)inv[i] = inv[i]*inv[i-1]%mod;
    int flag = 1;
    for(int i = 1;i<=k;i++)
    {
        scanf("%d%d",&o[i].x,&o[i].y);
        if(o[i].x==1&&o[i].y==1)flag = 0;
    }if(!flag)puts("0"),exit(0);
    sort(o+1,o+1+k,cmp);
    ans = C(n+m-2,n-1);
    for(int i = k;i>=1;i--)
    {
        dp[i] = C(n-o[i].x+m-o[i].y,n-o[i].x);
        for(int j = 1;j<=k;j++)
        {
            if(i==j||o[j].x<o[i].x||o[j].y<o[i].y)continue;
            dp[i] = (dp[i]-C(o[j].x-o[i].x+o[j].y-o[i].y,o[j].x-o[i].x)*dp[j]%mod)%mod;
            dp[i] = (dp[i]+mod)%mod;
        }cout<<dp[i]<<endl;
    }
    for(int i = 1;i<=k;i++)
    {
        ans = (ans-dp[i]*C(o[i].x+o[i].y-2,o[i].x-1)%mod)%mod;
    }printf("%I64d",(ans+mod)%mod);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define maxx 100005
#define maxn 2005
using namespace std;
int h,w,n;
ll dp[maxn];
ll fac[maxx<<1],inv_fac[maxx<<1],inv[maxx<<1];
struct node
{
	int x,y;
}dis[maxn];

ll cal(ll a,ll b)
{
	if(a<b || a<0 || b<0)
		return 0;
	return fac[a]*inv_fac[b] %mod *inv_fac[a-b]%mod;
}

bool cmp(node a,node b)
{
	return a.x<b.x;
}

int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=n;i++)
		scanf("%d %d",&dis[i].x,&dis[i].y);
	dis[n+1].x=h;
	dis[n+1].y=w;
	fac[0]=1;
	inv[1]=1;
	inv_fac[1]=1;
	inv_fac[0]=1;
	for(int i=1;i<=h+w;i++)
	{
		fac[i]=fac[i-1]*i%mod;
		if(i>1)
		{
			inv[i]=inv[mod%i]*(mod-mod/i)%mod;
			inv_fac[i]=inv_fac[i-1]*inv[i]%mod;
		}
	}
	sort(dis+1,dis+n+2,cmp);
	for(int i=1;i<=n+1;i++)
	{
		int xx=dis[i].x,yy=dis[i].y;
		dp[i]=cal(xx+yy-2,xx-1)%mod;
		for(int j=1;j<i;j++)
		{
			int xxx=dis[j].x,yyy=dis[j].y;
			dp[i]=(dp[i]+mod-dp[j]%mod*cal(xx+yy-xxx-yyy,xx-xxx)%mod)%mod;

		}
	}
	printf("%lld",(dp[n+1]%mod+mod)%mod);
}


#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
int h,w,n,f[2005],b[200005],c[100005];
struct xy{int x,y;}a[2005];
bool cmp(const xy &x,const xy &y)
{
	if (x.x!=y.x) return x.x<y.x;
	return x.y<y.y;
}
void exgcd(int a,int b,int &x,int &y)
{
	if (b)
	{
		exgcd(b,a%b,x,y);
		int z=x;
		x=y;
		y=z-a/b*y;
	}
	else x=1,y=0;
}
int inv(int a,int b)
{
	int x,y;
	exgcd(a,b,x,y);
	if (x<0) x+=b;
	return x;
}
int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for (int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
	b[0]=c[0]=1;
	for (int i=1;i<h+w-1;i++) c[i]=(ll)c[i-1]*inv(i,mod)%mod,b[i]=(ll)b[i-1]*i%mod;
	sort(a+1,a+n+1,cmp);
	a[++n].x=h,a[n].y=w;
	for (int i=1;i<=n;i++)
	{
		int x=a[i].x,y=a[i].y;
		f[i]=(ll)b[x+y-2]*c[x-1]%mod*c[y-1]%mod;
		for (int j=1;j<i;j++)
			if (a[j].y<=a[i].y)
			{
				f[i]-=(ll)f[j]*b[x+y-a[j].x-a[j].y]%mod*c[x-a[j].x]%mod*c[y-a[j].y]%mod;
				if (f[i]<0) f[i]+=mod;
			}
	}
	printf("%d",f[n]);
	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10,M = 2e3 + 10,mod = 1e9 + 7;

typedef pair<int,int> PII;

PII a[M];

int f[M];

int fac[N << 1],invfac[N << 1];

int C(int n,int m){return n<m?0:(long long)fac[n]*invfac[m]%mod*invfac[n-m]%mod;}

void init(){
    fac[0]=invfac[0]=invfac[1]=1;
    for(int i=1;i<=2e5 + 10;i++)fac[i]=(long long)fac[i-1]*i%mod;
    for(int i=2;i<=2e5 + 10;i++)invfac[i]=(long long)(mod-mod/i)*invfac[mod%i]%mod;
    for(int i=2;i<=2e5 + 10;i++)invfac[i]=(long long)invfac[i-1]*invfac[i]%mod;
}

int main(){
    init();
    int h,w,n;
    cin >> h >> w >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i].first >> a[i].second;
    }
    sort(a + 1,a + n + 1);
    a[n + 1].first = h,a[n + 1].second = w;
    for (int i = 1; i <= n + 1; i++){
        f[i] = C(a[i].first+a[i].second - 2,a[i].first - 1);
        for (int j = 1; j < i; j++){
            if (a[j].first > a[i].first || a[j].second > a[i].second) continue;
            f[i] = (f[i] - (1ll * f[j] * C(a[i].first-a[j].first+a[i].second-a[j].second,a[i].first-a[j].first)) + mod) % mod;
        }
    }
    cout << f[n + 1] % mod;
    return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define N 200020
using namespace std;
typedef long long ll;
struct ljzz
{ll r,c;};
ljzz a[N];
ll h,w,n,dp[N],p[N],inv[N];
bool tmp(ljzz x,ljzz y)
{return x.r<y.r||(x.r==y.r&&x.c<y.c);}
ll cmbntn(ll x,ll y)
{return (x<y)?0:(p[x]%mod*inv[x-y]%mod*inv[y]%mod)%mod;}
ll counting(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
		ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans%mod;
}
int main()
{
	scanf("%lld%lld%lld",&h,&w,&n);
	for(ll i=1;i<=n;i++)
	scanf("%lld%lld",&a[i].r,&a[i].c);
	a[++n].r=h,a[n].c=w;
	sort(a+1,a+1+n,tmp);
	p[0]=inv[0]=1;
	for(ll i=1;i<N;i++)
	{
		p[i]=(p[i-1]*i)%mod;
		inv[i]=counting(p[i],mod-2);
	}
	for(ll i=1;i<=n;i++)
	dp[i]=cmbntn(a[i].r-1+a[i].c-1,a[i].r-1);
	for(ll i=1;i<=n;i++)
	for(ll j=1;j<i;j++)
	{
		if(a[i].r<a[j].r||a[i].c<a[j].c)continue;
		dp[i]=(dp[i]-dp[j]*cmbntn(a[i].r-a[j].r+a[i].c-a[j].c,a[i].r-a[j].r)+mod*2)%mod;
	}
	printf("%lld",dp[n]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long v=2010,p=1e9+7;
long long n,m,k,f[v],d[200010];
struct no{
	long long x,y;
}s[v];
long long ksm(long long x,long long q)
{
	long long r=1;
	for(;q;x=(x*x)%p,q/=2)
	if(q&1)r=(r*x)%p;
	return r;
}
long long C(long long a,long long b)
{
	long long r;
	r=d[b]*ksm(d[a],p-2)%p;
	r=r*ksm(d[b-a],p-2)%p;
	return r;
}
bool bl(no a,no b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
int main()
{
	int i,j;
	cin>>n>>m>>k;d[0]=1;//C(0,1)=1
	for(i=1;i<=n+m-2;i++)d[i]=(d[i-1]*i)%p;//(1,1)到(n,m) d[n+m-2]
	for(i=1;i<=k;i++)scanf("%lld%lld",&s[i].x,&s[i].y);
	s[++k].x=n;s[k].y=m;
	sort(s+1,s+k+1,bl);
	for(i=1;i<=k;i++)
	{
		f[i]=C(s[i].x-1,s[i].x+s[i].y-2);
		for(j=1;j<i;j++)
		if(s[i].y>=s[j].y)f[i]=(f[i]-(f[j]*C(s[i].x-s[j].x,s[i].x-s[j].x+s[i].y-s[j].y))%p+p)%p;
	}
	cout<<f[k];
	return 0;
}
//设f[i]表示从(1,1)走到第i个不能走的点，且不经过其它不能走的点的方案数。


#include<bits/stdc++.h>
using namespace std;
#define go(i,a,b) for(int i=(a);i<=(b);++(i))
#define com(i,a,b) for(int i=(a);i>=(b);--(i))
#define mem(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define fin freopen("input.txt","r",stdin)
#define fout freopen("output.txt","w",stdout)
typedef long long ll;
const int maxn=2e5+100;
const int mod=1e9+7;
int n,m,cnt,f[2010];
ll in[maxn+10],inv[maxn+10];
struct node{
    int x,y;
}p[2010];

ll c(int i,int j){
    return in[i]*inv[j]%mod*inv[i-j]%mod;
}

ll fast(ll x,int p){
    ll ans=1;
    for(;p;p>>=1,x=x*x%mod){
        if(p&1) ans=ans*x%mod;
    }
    return ans;
}

void init(){
    in[0]=inv[0]=1;
    go(i,1,2e5){
        in[i]=in[i-1]*i%mod;
        inv[i]=fast(in[i],mod-2);
    }
}

bool cmp(node a,node b){
    return a.x<b.x;
}

signed main()
{
    //fin;
    scanf("%d%d%d",&n,&m,&cnt);
    int x,y;
    go(i,1,cnt){
        scanf("%d%d",&x,&y);
        p[i]=(node){x,y};
    }
    sort(p+1,p+cnt+1,cmp);
    p[++cnt]=(node){n,m};
    init();
    go(i,1,cnt){
        f[i]=c(p[i].x+p[i].y-2,p[i].x-1);
        go(j,1,i-1){
            if(p[j].y>p[i].y) continue;
            f[i]=(f[i]-(ll)f[j]*c(p[i].x + p[i].y - p[j].x - p[j].y, p[i].x - p[j].x))%mod;
        }
    }
    printf("%d",(f[cnt]+mod)%mod);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define LL long long
const LL MAXN = 200005;
const LL MOD = 1e9+7;
LL read()
{
	LL x=0,flag=1;char c;
	while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
	while(c>='0' && c<='9') x=x*10+(c^48),c=getchar();
	return x*flag;
}
LL r,c,n;
LL fac[MAXN]={1},inv[MAXN],dp[2005];
struct node
{
	LL a,b;
	bool operator < (const node &p)const {
		if(a==p.a) return b<p.b;
		return a<p.a;
	}
}a[2005];
void init(LL n)
{
	inv[0]=inv[1]=fac[0]=fac[1]=1;
	for(LL i=2;i<=n;i++)
		inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
	for(LL i=2;i<=n;i++)
		inv[i]=inv[i]*inv[i-1]%MOD;
	for(LL i=1;i<=n;i++)
		fac[i]=fac[i-1]*i%MOD;
}
LL C(LL n,LL m)
{
	return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
}
int main()
{
	r=read(),c=read(),n=read();
	init(2e5);
	for(LL i=1;i<=n;i++)
		a[i]=node{read(),read()};
	a[++n]={r,c};
	sort(a+1,a+1+n);
	for(LL i=1;i<=n;i++)
	{
		dp[i]=C(a[i].a+a[i].b-2,a[i].a-1);
		for(LL j=1;j<i;j++)
			if(a[j].a<=a[i].a && a[j].b<=a[i].b)
				dp[i]=(dp[i]-dp[j]*C(a[i].a-a[j].a+a[i].b-a[j].b,a[i].a-a[j].a)%MOD)%MOD;
		dp[i]=(dp[i]%MOD+MOD)%MOD;
	}
	printf("%lld\n",dp[n]);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL P=1e9+7;
const int M=2e5;
int X[2005],Y[2005],h,w,n;
LL dp[2005],F[M+5],Fi[M+5];
int C(int n,int m){
	return F[n]*Fi[n-m]%P*Fi[m]%P;
}
LL fast_pow(LL x,int p){
	LL res=1;
	while(p){
		if(p&1)res=res*x%P;
		x=x*x%P;
		p>>=1;
	}
	return res;
}
void pre(){
	F[0]=1;
	for(int i=1;i<=M;++i)
		F[i]=F[i-1]*i%P;
	Fi[M]=fast_pow(F[M],P-2);
	for(int i=M-1;i>=0;--i)
		Fi[i]=Fi[i+1]*(i+1)%P;
}
int main(){
	cin>>h>>w>>n;
	pre();
	for(int i=1;i<=n;++i)
		scanf("%d%d",&X[i],&Y[i]);
	for(int i=1;i<=n;++i){
		dp[i]=C(X[i]+Y[i]-2,X[i]-1);
		for(int j=1;j<=n;++j){
			if(i!=j&&X[i]>=X[j]&&Y[i]>=Y[j])
				dp[i]-=dp[j]*C(X[i]+Y[i]-X[j]-Y[j],X[i]-X[j]);
		}
	}
	LL ans=C(h+w-2,h-1);
	for(int i=1;i<=n;++i){
		ans=(ans-dp[i]*C(h+w-X[i]-Y[i],h-X[i]))%P;
		if(ans<0)ans+=P;
	}
	printf("%lld",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int Mod=1000000007;
struct node{
	int x,y;
}black[200001];
int f[200010];
long long fac[200001];
inline int read(){
	int r=0;
	char c=getchar();
	while(c<'0'||c>'9')
		c=getchar();
	while(c>='0'&&c<='9')
		r=(r<<3)+(r<<1)+(c^48),c=getchar();
	return r;
}
inline void close(){
	fclose(stdin);
	fclose(stdout);
}
inline bool comp(const node &a,const node &b){
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}
inline int Path(int x,int y){
	int minx=black[x].x,miny=black[x].y,maxx=black[y].x,maxy=black[y].y;
	int xfw=maxx-minx+1,yfw=maxy-miny+1;
	int p=xfw+yfw-2,q=xfw-1;
	return (fac[p]/(fac[q]*fac[p-q]))%Mod;
}
int main(){
//	freopen("chess.in","r",stdin);
//	freopen("chess.out","w",stdout);
	int n=read(),m=read(),k=read();
	fac[1]=fac[0]=1;
	for(int i=2;i<=100000;++i)
		fac[i]=(i*fac[i-1])%Mod;
	for(int i=1;i<=k;++i)
		scanf("%d%d",&black[i].x,&black[i].y);
	black[0].x=1,black[0].y=1;
	black[k+1].x=n,black[k+1].y=m;
	sort(black+1,black+k+1,comp);
	for(int i=1;i<=k+1;++i){
		for(int j=0;j<i;++j)
			f[i]=(f[i]-f[j]*Path(j,i)%Mod)%Mod;
		f[i]=(f[i]+Path(0,i))%Mod;
	}
	printf("%d",f[k+1]);
	close();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
#define fir first
#define sec second
#define mp make_pair
using namespace std;

namespace flyinthesky {

	const LL MO = 1e9 + 7;

	struct data {
		int x, y;
		bool operator < (const data &rhs) const {
			return (x == rhs.x) ? y < rhs.y : x < rhs.x;
		}
	}pnt[2000 + 5];

	int h, w, n;
	LL fac[300000 + 5], inv_fac[300000 + 5], dp[2000 + 5];

	LL ksm(LL a, LL b) {
		LL bs = a, ans = 1ll;
		while (b) {
			if (b & 1ll) ans = (ans * bs) % MO;
			bs = (bs * bs) % MO;
			b >>= 1ll;
		}
		return ans;
	}
	LL C(LL n, LL m) {
		if (n < 0 || m < 0) return 0ll;
		if (n < m) return 0ll;
		LL ans = fac[n];
		ans = (ans * inv_fac[m]) % MO;
		ans = (ans * inv_fac[n - m]) % MO;
		return ans;
	}

	void clean() {
		ms(dp, 0);
	}
	int solve() {
		clean();
		scanf("%d%d%d", &h, &w, &n);
		pnt[n + 1] = (data){1, 1}, pnt[n + 2] = (data){h, w};
		for (int i = 1; i <= n; ++i) scanf("%d%d", &pnt[i].x, &pnt[i].y);
		n += 2, sort(pnt + 1, pnt + 1 + n);
		fac[0] = inv_fac[0] = 1ll;
		for (int i = 1; i <= 300000; ++i) fac[i] = (fac[i - 1] * i) % MO, inv_fac[i] = (inv_fac[i - 1] * ksm(i, MO - 2)) % MO;
		for (int i = 1; i <= n; ++i) {
			dp[i] = C(pnt[i].x + pnt[i].y - 2ll, pnt[i].x - 1ll);
			for (int j = 2; j < i; ++j) {
				int dx = pnt[i].x - pnt[j].x, dy = pnt[i].y - pnt[j].y;
				dp[i] = (dp[i] - C(dx + dy, dx) * dp[j] % MO + MO) % MO;
			}
		}
		cout << dp[n];
		return 0;
	}
}
int main() {
	flyinthesky::solve();
	return 0;
}
//21:17  -  :34  -  20:48   -  20:48   -  20:56
//Start  -  Finish -  Static  -  Sample  -   AC
/*
*/


#include<bits/stdc++.h>
#define X p[i].x
#define Y p[i].y
using namespace std;
const int P = 1e9 + 7,N = 1e5 + 7;
int H,W,n,jc[N << 1],f[2019];
struct black {
	int x,y;
	bool operator < (const black &a) const {
		return x == a.x ? y < a.y : x < a.x;
	}
}p[2019];

inline int ksm(int d,int c) {
	int ret = 1;
	while (c) {
		if (c & 1) ret = (1ll * ret * d) % P;
		d = (1ll * d * d) % P;
		c >>= 1;
	} return ret;
}

inline int C(int m,int n) {
	return 1ll * jc[n] * ksm(jc[m],P-2) % P * ksm(jc[n-m],P-2) % P;
}

int main()
{
	scanf("%d%d%d",&H,&W,&n);
	for(register int i=1;i<=n;++i)
		scanf("%d%d",&X,&Y);
	std::sort(p + 1,p + 1 + n);
	int fo = H + W + 2; jc[0] = jc[1] = 1;
	for(register int i=2;i<=fo;++i)
		jc[i] = 1ll * i * jc[i - 1] % P;
	p[n + 1].x = H; p[n + 1].y = W; // 定义 f 为只经过第i个黑色的步数。
	for(register int i=1;i<=n+1;++i) {
		f[i] = C(X - 1,X + Y - 2);
		for(register int j=1;j<i;++j) {
			if (p[j].x > X || p[j].y > Y) continue;
			f[i] = (f[i] - 1ll * f[j] * C(X - p[j].x,X + Y - p[j].x - p[j].y)) % P;
		}
	}
	printf("%d",(f[n + 1] % P + P) % P);
}


#include<bits/stdc++.h>
using namespace std;
#define go(i,a,b) for(int i=(a);i<=(b);++(i))
#define com(i,a,b) for(int i=(a);i>=(b);--(i))
#define mem(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define fin freopen("input.txt","r",stdin)
#define fout freopen("output.txt","w",stdout)
typedef long long ll;
#define int long long
const int maxn=2e5+100;
const int mod=1e9+7;
int n,m,cnt,f[2010],in[maxn+10],inv[maxn+10];
struct node{
	int x,y;
}p[2010];

int c(int i,int j){
	return in[i]*inv[j]%mod*inv[i-j]%mod;
}

int fast(int x,int p){
	int ans=1;
	for(;p;p>>=1,x=x*x%mod){
		if(p&1) ans=ans*x%mod;
	}
	return ans;
}

void init(){
	in[0]=inv[0]=1;
	go(i,1,2e5){
		in[i]=in[i-1]*i%mod;
		inv[i]=fast(in[i],mod-2);
	}
}

bool cmp(const node &a,const node &b){
	return a.x<b.x;
}
#define x(i) p[i].x
#define y(i) p[i].y
signed main()
{
	//fin;
	scanf("%lld%lld%lld",&n,&m,&cnt);
	int x,y;
	go(i,1,cnt){
		scanf("%lld%lld",&x,&y);
		p[i]=(node){x,y};
	}
	p[++cnt]=(node){n,m};
	sort(p+1,p+cnt+1,cmp);
	init();
	go(i,1,cnt){
		f[i]=c(p[i].x+p[i].y-2,p[i].x-1);
		go(j,1,i-1){
			if(p[j].y>p[i].y||x(j)>x(i)) continue;
			f[i]=(f[i]-f[j]*c(x(i)-x(j)+y(i)-y(j),x(i)-x(j)))%mod;
		}
	}
	printf("%lld",(f[cnt]+mod)%mod);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
using namespace std;

const int SIZE = 2010;
struct Node
{
	int x,y;
}a[SIZE];
bool operator<(const Node &a,const Node &b){
	return a.x<b.x;
}

int h, w, n, f[SIZE], mod = 1000000007;
long long jc[200010], jcinv[200010];

int C(int n, int m) {
	return jc[n] * jcinv[m] % mod * jcinv[n - m] % mod;
}

long long power(long long a, int b) {
	long long c = 1;
	for (; b; b >>= 1) {
		if (b & 1) c = c*a%mod;
		a = a*a%mod;
	}
	return c;
}

int main() {
	jc[0] = 1, jcinv[0] = 1;
	for (int i = 1; i <= 200000; i++) {
		jc[i] = jc[i - 1] * i % mod;
		jcinv[i] = power(jc[i], mod - 2);
	}
	cin >> h >> w >> n;
	for (int i = 1; i <= n; i++)
		scanf("%d%d", &a[i].x, &a[i].y);
	sort(a + 1, a + n + 1);
	a[n + 1].x = h, a[n + 1].y = w;
	for (int i = 1; i <= n + 1; i++) {
		f[i] = C(a[i].x + a[i].y - 2, a[i].x - 1);
		for (int j = 1; j < i; j++) {
			if (a[j].x > a[i].x || a[j].y > a[i].y) continue;
			f[i] = (f[i] - (long long)f[j] * C(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x)) % mod;
		}
	}
	cout << (f[n + 1] + mod) % mod << endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define mod 1000000007
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
    	n=n*10+ch-'0';
    	ch=getchar();
    }
    return n*f;
}
int x[2001],y[2001],dp[2001],pos[2001],jc[400001],njc[400001];
int ksm(int n,int k)
{
	if(k==0)return 1;
	if(k==1)return n%mod;
	int nn=ksm(n,k/2);
	if(k%2==0)return 1LL*nn*nn%mod;
	else return 1LL*nn*nn%mod*1LL*n%mod;
}
bool bi(int c,int d)
{
	return x[c]<x[d];
}
int main()
{
	int h,w,n;
	h=read();
	w=read();
	n=read();
	for(int i=1;i<=n;i++)x[i]=read(),y[i]=read(),pos[i]=i;
	x[n+1]=h;
	y[n+1]=w;
	pos[n+1]=n+1;
	sort(pos+1,pos+n+2,bi);
	jc[0]=1;
	for(int i=1;i<=400000;i++)jc[i]=1LL*i*jc[i-1]%mod;
	njc[400000]=ksm(jc[400000],mod-2);
	for(int i=399999;i>=0;i--){njc[i]=1LL*(i+1)*njc[i+1]%mod;}
	for(int i=1;i<=n+1;i++)
	{
		dp[pos[i]]=1LL*jc[x[pos[i]]+y[pos[i]]-2]*njc[x[pos[i]]-1]%mod*1LL*njc[y[pos[i]]-1]%mod;
		for(int j=1;j<i;j++)if(y[pos[j]]<=y[pos[i]])dp[pos[i]]=(dp[pos[i]]-1LL*dp[pos[j]]*jc[x[pos[i]]+y[pos[i]]-x[pos[j]]-y[pos[j]]]%mod*1LL*njc[x[pos[i]]-x[pos[j]]]%mod*1LL*njc[y[pos[i]]-y[pos[j]]]%mod+mod)%mod;
	}
	printf("%lld\n",dp[pos[n+1]]);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll; const ll MOD=1e9+7;
inline int rdint(){int X=0,Z=0; char CH=0;while(!isdigit(CH)) Z=CH=='-'?1:0,CH=getchar();while(isdigit(CH)) X=(X<<1)+(X<<3)+(CH^48),CH=getchar();return Z?-X:X;}
inline ll rdlon(){ll X=0,Z=0; char CH=0;while(!isdigit(CH)) Z=CH=='-'?1:0,CH=getchar();while(isdigit(CH)) X=(X<<1)+(X<<3)+(CH^48),CH=getchar();return Z?-X:X;}
inline int mymax(int X,int Y){return X>Y?X:Y;} inline int mymin(int X,int Y){return X<Y?X:Y;}
inline ll qpower(ll x,ll y){
	x%=MOD; ll ret=1;
	while(y){if(y%2) ret=ret*x%MOD; x=x*x%MOD; y>>=1;}
	return ret%MOD;
}
ll fac[4080],invfac[4080],f[2080]; int n,h,w;
inline void INIT(){register int i; fac[0]=fac[1]=1;
	for(i=2;i<=4040;++i) fac[i]=fac[i-1]*i%MOD;
	invfac[4040]=qpower(fac[4040],MOD-2);
	for(i=4039;i>0;--i) invfac[i]=invfac[i+1]*(i+1)%MOD;
}
inline ll combin(ll sm,ll la){return fac[la]*invfac[sm]%MOD*invfac[la-sm]%MOD;}
inline ll C(ll x,ll y){return combin(x-1,x+y-2);}
struct Node{ll x,y; bool operator < (const Node &a) const {if(x!=a.x) return x<a.x; else return y<a.y;}}line[2080];
main(){register int i,j;
	INIT();
	h=rdint(),w=rdint(),n=rdint();
	for(i=1;i<=n;++i) line[i].x=rdlon(),line[i].y=rdlon();
	sort(line+1,line+1+n);
	line[n+1]=(Node){h,w};
	for(i=1;i<=n+1;++i){
		f[i]=C(line[i].x,line[i].y);
		for(j=1;j<i;++j){
			f[i]=(f[i]+(MOD-(f[j]*C(line[i].x-line[j].x+1,line[i].y-line[j].y+1)%MOD) )%MOD)%MOD;
		}
	}
	cout<<f[n+1];
	return 0;
}


#include<iostream>
#include<vector>
#define ll long long
using namespace std;

int main()
{
   ll n; cin>>n;
   vector<ll> V;
   ll a,b; ll ck=0;ll mn=0;
   for(ll i=0;;i++)
   {
       b=i;
       if((b*7)>n) break;
       a=(n-(b*7))/4;
       if(!((n-(b*7))%4))
       {
           ck++;
           if(ck==1)
            {mn=a+b;for(ll j=1;j<=a;j++)V.push_back(4) ;
            for(ll j=1;j<=b;j++)V.push_back(7) ;  }
           else
            if(a+b<=mn)
            {mn=a+b;
            V.clear();
            for(ll j=1;j<=a;j++)V.push_back(4) ;
            for(ll j=1;j<=b;j++)V.push_back(7) ;

            }
       }
       //if((b*7)>n) break;
   }
   if(!ck) cout<<"-1"<<endl;
   else
   {
       for(ll i=0;i<V.size();i++)
        cout<<V[i];
   }
}


#include<bits/stdc++.h>

#define ll long long
using namespace std;

int main()
{
   ll n; cin>>n;
   vector<ll> V;
   ll a,b; ll ck=0;ll mn=0;
   while(n>3)
   {
        if( n % 7 == 0 && n >= 7 ){
            n-=7;

            V.push_back(7);
        }

        else if( n % 4 == 0 && n >= 4 ){
            n-=4;
            V.push_back(4);
        }

        else if( n % 7 != 0 && n >= 7 ){
            n-=7;
            V.push_back(7);
        }
        else if( n % 4 != 0  && n >= 4 ){
            n-=4;
            V.push_back(4);
        }

   }
   if(V.size()==0 || n) cout<<"-1"<<endl;
   else
   {
       sort(V.begin(),V.end());
       for(ll i=0;i<V.size();i++)
        cout<<V[i];
   }
}


#include<bits/stdc++.h>
#define l 	long
#define ll 	long long
#define vll vector<ll>
#define vi 	vector<int>

// #define rep(j,n,s)  for(long i=j;i<=n;i+=s)
#define forn(j,n,s) for(long i=j; i<n; i+=s)
#define rep(i,n,s) 	for(		; i<n; i+=s)

#define read(k)		ll k; cin>>k;
#define range(v)	begin(v), end(v)

using namespace std;
void solve()
{
    //Answer here.
    read(n);
    vll v = {0,8,16,24,4,12,20};
    int r = n%7, c = v[r] , s = n - c;

    if(r&&s<0)	cout<<-1<<endl;
    else
    {
    	s = s/7;
    	c = c/4;

    	forn(0,c,1)	cout<<4;
    	forn(0,s,1)	cout<<7;
    	cout<<endl;
    }

}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("input", "r", stdin);
	freopen("output","w",stdout);
	#endif

	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

    int t=1;  cin>>t;
    while(t--)  solve();

    return 0;
}


#include<bits/stdc++.h>
#define l 	long
#define ll 	long long
#define vll vector<ll>
#define vi 	vector<int>

// #define rep(j,n,s)  for(long i=j;i<=n;i+=s)
#define forn(j,n,s) for(long i=j; i<n; i+=s)
#define rep(i,n,s) 	for(		; i<n; i+=s)

#define read(k)		ll k; cin>>k;
#define range(v)	begin(v), end(v)

using namespace std;
void solve()
{
    //Answer here.
    read(n);
    vll v = {0,8,16,24,4,12,20};
    int r = n%7, c = v[r] , s = n - c;

    if(r&&s<0)	cout<<-1<<endl;
    else
    {
    	s = s/7;
    	c = c/4;

    	forn(0,c,1)	cout<<4;
    	forn(0,s,1)	cout<<7;
    	cout<<endl;
    }

}

int main(){

	#ifndef ONLINE_JUDGE
	freopen("input", "r", stdin);
	freopen("output","w",stdout);
	#endif

	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

    int t=1;  //cin>>t;
    while(t--)  solve();

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

int main()
{
    int i,j,k,f=0,t;
    int n;
    scanf("%d",&n);
    if(n%7==0){
        k=n/7;
        for(i=1;i<=k;i++)printf("7");
        cout<<endl;
    }
    k=n/7;
    for(i=k;i>=0;i--){
        j=n-i*7;
        if(j%4==0){
            f=1;
            break;
        }
    }
    t=i;
    if(f==0){cout<<-1<<endl;return 0;}
    j=j/4;
    for(i=1;i<=j;i++){
        printf("4");
    }
    for(i=1;i<=t;i++)printf("7");
    cout<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 ll;

int main()
{
    int n;
    scanf("%d",&n);
   int t1=n/7;
   int t2=n%7;
   int t3=0;
   int ans4=1e8,ans7=1e8;
   for(int i=0;i<=t1;i++)
   {
       int tmp=n-i*7;
       if(tmp%4==0)
       {
           int tmp3=tmp/4;
           if(tmp3+i<(ans4+ans7))
           {
               ans7=i;
               ans4=tmp3;
           }
       }
   }
   //printf("%d  %d\n",ans4,ans7);
    if(ans4==1e8&&ans7==1e8) printf("-1\n");
    else
  {
   for(int i=1;i<=ans4;i++) printf("4");
   for(int i=1;i<=ans7;i++) printf("7");
   printf("\n");
  }
    return 0;
}


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

int main(){
    string ans;
    int n, i, d, four, seven, flag, j;
    cin >> n;
    for(i = n/4, flag = 0; i >= 0; i--){
        j = (n - i*4)/7;
        if(7*j == (n - i*4)){
            flag = 1;
            four = i;
            seven = j;
        }
    }
    if(flag == 0)
    cout << "-1" << endl;
    else{
        for(i = 1; i <= four; i++){
            printf("4");
        }
        for(i = 1; i <= seven; i++){
            printf("7");
        }
        cout << endl;
    }
    return 0;
}


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

int main(){
    string ans;
    int n, i, d, four, seven, flag, j;
    cin >> n;
    for(i = 0, flag = 0; i*4 <= n; i++){
        j = (n - i*4)/7;
        if(7*j == (n - i*4)){
            flag = 1;
            four = i;
            seven = j;
        }
    }
    if(flag == 0)
    cout << "-1" << endl;
    else{
        for(i = 1; i <= four; i++){
            printf("4");
        }
        for(i = 1; i <= seven; i++){
            printf("7");
        }
        cout << endl;
    }
    return 0;
}


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

vector<long long >seven;
vector<long long > four;
long long N;

int binarysearch(int aim)
{
    int low=0, high=four.size()-1, mid;
    while(low<= high)
    {
        mid=(low+high)/2;
        if(four[mid] == aim)
                return mid;
        if(four[mid] > aim)
                high=mid-1;
        else
                low=mid+1;
    }
    return -1;
}
int main()
        {
            cin>>N;
            for(int i=0;i<N;i++)
                {
                    seven.push_back(i*7);
                    four.push_back(i*4);
                    if(four.back() >= N)
                            break;
                }

            for(int i=0;i<=four.size();i++)
                {
                    int mid=binarysearch(N-seven[i]);
                    if(mid != -1)
                        {
                            for(int j=0;j<seven[i]/7;j++)
                                    cout<<7;
                            if(four[mid] >= 28 && (four[mid]%28)%4==0)
                                    {

                                        for(int j=0;j<(four[mid]/28)*4;j++)
                                                cout<<7;
                                        for(int j=0;j<(four[mid]%28)/4;j++)
                                                cout<<4;
                                    }
                            else
                                        for(int j=0;j<four[mid]/4;j++)
                                                cout<<4;

                        return 0;
                        }

                }
            cout<<-1<<"\n";
            return 0;
        }


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

vector<long long >seven;
vector<long long > four;
long long N;

int binarysearch(int aim)
{
    int low=0, high=four.size()-1, mid;
    while(low<= high)
    {
        mid=(low+high)/2;
        if(four[mid] == aim)
                return mid;
        if(four[mid] > aim)
                high=mid-1;
        else
                low=mid+1;
    }
    return -1;
}
int main()
        {
            cin>>N;
            for(int i=0;i<N;i++)
                {
                    seven.push_back(i*7);
                    four.push_back(i*4);
                    if(four.back() >= N)
                            break;
                }

            for(int i=0;i<=four.size();i++)
                {
                    int mid=binarysearch(N-seven[i]);
                    if(mid != -1)
                        {
                            if(four[mid] >= 28 && (four[mid]%28)%4==0)
                                    {
                                        for(int j=0;j<(four[mid]%28)/4;j++)
                                                cout<<4;
                                        for(int j=0;j<(four[mid]/28)*4;j++)
                                                cout<<7;
                                    }
                            else
                                        for(int j=0;j<four[mid]/4;j++)
                                                cout<<4;

                            for(int j=0;j<seven[i]/7;j++)
                                    cout<<7;
                            return 0;
                        }

                }
            cout<<-1<<"\n";
            return 0;
        }


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

int N;
bool vis[1000001];

void bfs(int source)
{
    deque<int> DQ;
    DQ.push_back(source);
    vis[source]=true;
    while(!DQ.empty())
    {
        int node=DQ.front();
        DQ.pop_front();
        if(node == N)
                return ;
        if(node + 4 <= N && !vis[node + 4])
            {
                ///check=false;
                DQ.push_back(node+4);
                vis[node + 4]=true;
            }
        if(node + 7 <= N && !vis[node + 7])
            {
               /// check=false;
                DQ.push_back(node+7);
                vis[node + 7]=true;
            }
    }
    cout<<-1<<"\n";
    exit(0);
}

int main()
{
    cin >> N;

    bfs(0);

    for(int i=0;i<7;i++)
    {
        if((N - i*4)%7 == 0)
        {
            for(int j=0;j<i;j++)
                cout<<4;
            for(int z=0;z<(N-i*4)/7;z++)
                cout<<7;
            break;
        }
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    ll n;
    cin>>n;
    ll s = n / 7;
    n = n % 7;
    ll f = n/4;
    n = n % 4;

    ll ans = 0;
    while(n != 0 && n % 4 != 0){
        n += 7;
        ans++;
        if(ans > s){
            cout<<-1<<"\n";
            return 0;
        }
    }
    s = s - ans;
    f += n/4;
    for(ll i = 0 ; i < f; i++){
        cout<<4;
    }
    for(ll i = 0 ; i < s; i++){
        cout<<7;
    }
    return 0;
}