h,w = map(int,input().split()) a = [[int(i) for i in input().split()] for j in range(h)] q = int(input()) rc = [[int(i)-1 for i in input().split()] for i in range(q)] mod = 10**9+7 ttl = 1 fz = False zc = 0 for i in range(h): for j in range(w): if a[i][j] == 0: zc += 1 fz = True continue ttl *= a[i][j] ttl %= mod tate = [1]*h tz = [0]*h yoko = [1]*w yz = [0]*w for i in range(h): for j in range(w): if a[i][j] == 0: tz[i] += 1 continue tmp = pow(a[i][j],mod-2,mod) tate[i] *= tmp tate[i] %= mod for i in range(w): for j in range(h): if a[j][i] == 0: yz[i] += 1 continue tmp = pow(a[j][i],mod-2,mod) yoko[i] *= tmp yoko[i] %= mod for i in range(q): r,c = rc[i] if fz == True: add = 0 if a[r][c] == 0: add = 1 if zc != tz[r]+yz[c]-add: print(0) continue ans = ttl*tate[r] ans %= mod ans *= yoko[c] ans %= mod if a[r][c] != 0: ans *= a[r][c] ans %= mod print(ans)