import sys def input(): return sys.stdin.buffer.readline()[:-1] MOD = 10**9+7 H, W = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(H)] r_zero = [0 for _ in range(H)] c_zero = [0 for _ in range(W)] tot_zero = 0 r = [1 for _ in range(H)] c = [1 for _ in range(W)] tot = 1 for h in range(H): for w in range(W): if a[h][w] == 0: r_zero[h] += 1 c_zero[w] += 1 tot_zero += 1 else: r[h] *= a[h][w] r[h] %= MOD c[w] *= a[h][w] c[w] %= MOD tot *= a[h][w] tot %= MOD for _ in range(int(input())): x, y = map(int, input().split()) x, y = x-1, y-1 res = tot if r_zero[x] + c_zero[y] - (a[h][w] == 0) != tot_zero: print(0) else: res *= pow(r[x], MOD-2, MOD) * pow(c[y], MOD-2, MOD) if a[x][y] != 0: res *= a[x][y] print(res%MOD)