def main(): from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! ''' または、か 0が含まれてるパターンがえぐい 二次元を1次元に変えてしまえばセグメントツリーが使えるのかな だから田なのか ''' H,W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] r_acm = [1]*W c_acm = [1]*H zero = 0 z_r = [0]*W z_c = [0]*H sm = 1 for i in range(H): for j in range(W): if A[i][j] == 0: z_r[j] += 1 z_c[i] += 1 zero += 1 else: sm *= A[i][j] r_acm[j] *= A[i][j] c_acm[i] *= A[i][j] sm %= mod1 r_acm[j] %= mod1 c_acm[i] %= mod1 Q = int(input()) for _ in range(Q): r,c = map(int, input().split()) r -= 1 c -= 1 z = zero-z_r[c]-z_c[r]+int(A[r][c]==0) if z > 0: print(0) else: ans = sm * pow(r_acm[c]*c_acm[r]%mod1,mod1-2,mod1) if A[r][c] == 0: print(ans) else: print(ans*A[r][c]%mod1) if __name__ == '__main__': main()