MOD = 10 ** 9 + 7 H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] AS = [[0] * (W + 1) for _ in range(H + 1)] for i in range(H): for j in range(W): tmp = 1 if A[i][j] == 0 else 0 tmp += AS[i + 1][j] tmp += AS[i][j + 1] tmp -= AS[i][j] AS[i + 1][j + 1] = tmp AM = [[1] * (W + 1) for _ in range(H + 1)] for i in range(H): for j in range(W): if A[i][j] == 0: AM[i + 1][j + 1] = 1 else: tmp = A[i][j] tmp *= AM[i + 1][j] tmp %= MOD tmp *= AM[i][j + 1] tmp %= MOD tmp *= pow(AM[i][j],-1,MOD) tmp %= MOD AM[i + 1][j + 1] = tmp def check(x1, y1, x2, y2): ret = AS[x1][y1] ret += AS[x2 - 1][y2 - 1] ret -= AS[x1][y2 - 1] ret -= AS[x2 - 1][y1] return ret > 0 def calc(x1, y1, x2, y2): ret = AM[x1][y1] ret %= MOD ret *= AM[x2][y2] ret %= MOD ret *= pow(AM[x1][y2], -1, MOD) ret %= MOD ret *= pow(AM[x2][y1], -1, MOD) ret %= MOD return ret for _ in range(int(input())): r, c = map(int, input().split()) if check(r - 1, c - 1, 0, 0) or check(H, W, r, c) or check(H, c - 1, r, 0) or check(r - 1, W, 0, c): print(0) continue ans = calc(r - 1, c - 1, 0, 0) ans %= MOD ans *= calc(H, W, r, c) ans %= MOD ans *= calc(H, c - 1, r, 0) ans %= MOD ans *= calc(r - 1, W, 0, c) ans %= MOD print(ans)