MOD = 10 ** 9 + 7 h, w = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(h)] prod = 1 zero = 0 prod_row = [1] * h prod_col = [1] * w zero_row = [1] * h zero_col = [1] * w for i in range(h): for j in range(w): if A[i][j] == 0: zero += 1 zero_row[i] += 1 zero_col[j] += 1 else: prod *= A[i][j] prod_row[i] *= A[i][j] prod_col[j] *= A[i][j] prod %= MOD prod_row[i] %= MOD prod_col[j] %= MOD Q = int(input()) for _ in range(Q): r, c = map(int, input().split()) r -= 1 c -= 1 z = zero - zero_row[r] - zero_col[c] + int(A[r][c] == 0) if z > 0: print(0) else: ans = prod * pow(prod_row[r] * prod_col[c] % MOD, MOD - 2, MOD) % MOD if A[r][c] != 0: ans *= A[r][c] ans %= MOD print(ans)