Kindly someone elaborate on the process in which input is taken in the following code?
Problem Statement :
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Check2 implements Runnable{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();\r
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
static LinkedList <Integer> adj[];
static int co=0,f=0;
static int n,m;
static void Check2(int n){
adj=new LinkedList[n+1];
for(int i=0;i<=n;i++){
adj[i]=new LinkedList();
}
}
static void add(int i,int j){
adj[i].add(j);
adj[j].add(i);
}
public static void main(String[] args) throws Exception {
new Thread(null, new Check2(), "Check2", 1<<26).start();
}
public void run(){
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
//Scanner in=new Scanner(System.in);
InputReader in=new InputReader(System.in);
PrintWriter w=new PrintWriter(System.out);
long mod=1000000007l;
long fact[]=new long[1000001];
fact[0]=1;
fact[1]=1;
for(int i=2;i<=1000000;i++)
{
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
int t=in.nextInt();
while(t-->0){
n=in.nextInt();
m=in.nextInt();
co=1;
int q=in.nextInt();
int a[][]=new int[n+1][m+1];
// Check2(n);
for(int i=0;i<q;i++)
{
int c=in.nextInt();
int d=in.nextInt();
a[c][d]=1;
// add(c,d);
}
int v[][]=new int[n+1][m+1];
long ans=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++){
if(v[i][j]==0&&a[i][j]==1){
co=1;
dfs2(i,j,v,a);
// w.println(co+" "+i+" "+j+"hiiiiiiiiiii");
ans=ans*fact[co];
ans%=mod;
}
}
}
w.println(ans);
}
w.close();
}
static void dfs(int i,int v[]){
if(v[i]==1){
return ;
}
else{
v[i]=1;
Iterator <Integer>p=adj[i].iterator();
while(p.hasNext()){
Integer ne=p.next();
dfs(ne,v);
//
// System.out.println(q+" "+i);
//System.out.println(i+" "+max1+" "+max2);
}
}
}
static void dfs2(int i,int j,int v[][],int a[][])
{
v[i][j]=1;
if(i<n-1&&j!=m&&a[i+2][j+1]==1&&v[i+2][j+1]==0)
{
co++;
v[i+2][j+1]=1;
dfs2(i+2,j+1,v,a);
}
if(i<n-1&&j!=0&&a[i+2][j-1]==1&&v[i+2][j-1]==0)
{
co++;
v[i+2][j-1]=1;
dfs2(i+2,j-1,v,a);
}
if(i>1&&j!=m&&a[i-2][j+1]==1&&v[i-2][j+1]==0)
{
co++;
v[i-2][j+1]=1;
dfs2(i-2,j+1,v,a);
}
if(i>1&&j!=0&&a[i-2][j-1]==1&&v[i-2][j-1]==0)
{
co++;
v[i-2][j-1]=1;
dfs2(i-2,j-1,v,a);
}
if(i!=n&&j>=2&&a[i+1][j-2]==1&&v[i+1][j-2]==0)
{
co++;
v[i+1][j-2]=1;
dfs2(i+1,j-2,v,a);
}
if(i!=n&&j<m-1&&a[i+1][j+2]==1&&v[i+1][j+2]==0)
{
co++;
v[i+1][j+2]=1;
dfs2(i+1,j+2,v,a);
}
if(i>=1&&j<m-1&&a[i-1][j+2]==1&&v[i-1][j+2]==0)
{
// System.out.println(i+" "+j);
co++;
v[i-1][j+2]=1;
dfs2(i-1,j+2,v,a);
}
if(i>=1&&j>=2&&a[i-1][j-2]==1&&v[i-1][j-2]==0)
{
co++;
v[i-1][j-2]=1;
dfs2(i-1,j-2,v,a);
}
}
static class node{
int y;
int val;
node(int a,int b){
y=a;
val=b;
}
}
}