#include #include #include #include #define DLEN 4 using namespace std; const int MAXN = 5005; const int MAXM = 2e5 + 5; const long long mod = 1e9 + 7; ifstream fin("connectem.in"); ofstream fout("connectem.out"); long long fact[MAXM], invFact[MAXM]; long long fastPow(long long x, long long p) { long long ans = 1, curr = x; while(p!=0) { if(p%2!=0) ans = (ans*curr)%mod; curr = (curr*curr)%mod; p/=2; } return ans; } long long inv(long long x) { return fastPow(x, mod-2); } struct State { int len; long long ways; State(){} State(int len) { this->len = len; this->ways = 1; } State(int len, long long ways) { this->len = len; this->ways = ways; } }; State operator +(State A, State B) { if(A.len==B.len) return State(A.len, (A.ways+B.ways)%mod); if(A.len>B.len) return A; if(A.len a, pair b) { int dx = abs(a.first-b.first); int dy = abs(a.second-b.second); long long ans = fact[dx+dy]; ans = (ans*invFact[dx])%mod; ans = (ans*invFact[dy])%mod; return ans; } State dp[MAXN]; pair a[MAXN]; void solveTestcase() { int n, m, k; fin >> n >> m >> k; for(int i = 0;i> a[i].first >> a[i].second; sort(a, a+k); dp[0] = State(1); for(int i = 1;ibestLen) bestLen = dp[j].len; for(int j = 0;j> T; while(T--) solveTestcase(); }