H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] H_n = [1]*H W_n = [1]*W H_z = [0]*H W_z = [0]*W mod = 10**9+7 al = 1 al_z = 0 for i in range(H): for j in range(W): if A[i][j] == 0: H_z[i] += 1 W_z[j] += 1 al_z += 1 else: H_n[i] *= A[i][j] W_n[j] *= A[i][j] al *= A[i][j] H_n[i] %= mod W_n[j] %= mod al %= mod Q = int(input()) for i in range(Q): r, c = map(int, input().split()) if H_z[r-1]+W_z[c-1]-(A[r-1][c-1]==0) != al_z: print(0) else: p = 1 if A[r-1][c-1] == 0 else A[r-1][c-1] print((al*pow(H_n[r-1], mod-2, mod)*pow(W_n[c-1], mod-2, mod)*p)%mod)