H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] H_inv = [0]*H W_inv = [0]*W mod = 10**9+7 al = 1 for i in range(H): sm = 1 for j in range(W): al *= A[i][j] sm *= A[i][j] al %= mod sm %= mod H_inv[i] = pow(sm, mod-2, mod) for j in range(W): sm = 1 for i in range(H): sm *= A[i][j] sm %= mod W_inv[j] = pow(sm, mod-2, mod) Q = int(input()) for i in range(Q): r, c = map(int, input().split()) print((al*H_inv[r-1]*W_inv[c-1]*A[r-1][c-1])%mod)