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 = [1 for _ in range(H)] c = [1 for _ in range(W)] tot = 1 for h in range(H): for w in range(W): 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()) print((tot * pow(r[x-1], MOD-2, MOD) * pow(c[y-1], MOD-2, MOD) * a[x-1][y-1]) % MOD)