import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int h = Integer.parseInt(sa[0]); int w = Integer.parseInt(sa[1]); int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { sa = br.readLine().split(" "); for (int j = 0; j < w; j++) { a[i][j] = Integer.parseInt(sa[j]); } } int q = Integer.parseInt(br.readLine()); int[] r = new int[q]; int[] c = new int[q]; for (int i = 0; i < q; i++) { sa = br.readLine().split(" "); r[i] = Integer.parseInt(sa[0]) - 1; c[i] = Integer.parseInt(sa[1]) - 1; } br.close(); int mod = 1000000007; int zero = 0; long total = 1; long[] sx = new long[h]; int[] zx = new int[h]; for (int i = 0; i < h; i++) { sx[i] = 1; for (int j = 0; j < w; j++) { if (a[i][j] == 0) { zero++; zx[i]++; } else { total *= a[i][j]; total %= mod; sx[i] *= a[i][j]; sx[i] %= mod; } } } long[] sy = new long[w]; int[] zy = new int[w]; for (int j = 0; j < w; j++) { sy[j] = 1; for (int i = 0; i < h; i++) { if (a[i][j] == 0) { zy[j]++; } else { sy[j] *= a[i][j]; sy[j] %= mod; } } } PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < q; i++) { int z = zero - zx[r[i]] - zy[c[i]]; if (a[r[i]][c[i]] == 0) { z++; } if (z == 0) { long ans = total * modinv(sx[r[i]], mod) % mod; ans *= modinv(sy[c[i]], mod) % mod; ans %= mod; if (a[r[i]][c[i]] != 0) { ans *= a[r[i]][c[i]] % mod; ans %= mod; } pw.println(ans); } else { pw.println(0); } } pw.flush(); } static long modinv(long a, int m) { long b = m; long u = 1; long v = 0; long tmp = 0; while (b > 0) { long t = a / b; a -= t * b; tmp = a; a = b; b = tmp; u -= t * v; tmp = u; u = v; v = tmp; } u %= m; if (u < 0) u += m; return u; } }