MOD = 10**9+7 h,w = map(int,input().split()) a = [list(map(int,input().split())) for _ in range(h)] row = [1]*h zrow = [0]*h for i in range(h): v = 1 for j in range(w): if a[i][j]: v = v*a[i][j]%MOD else: zrow[i] += 1 row[i] = v col = [1]*w zcol = [0]*w for j in range(w): v = 1 for i in range(h): if a[i][j]: v = v*a[i][j]%MOD else: zcol[j] += 1 col[j] = v tot = 1 for v in row: tot = tot*v%MOD ztot = sum(zrow) def inv(x): assert x return pow(x,MOD-2,MOD) Q = int(input()) for _ in range(Q): r,c = map(int,input().split()) r -= 1 c -= 1 if ztot - zrow[r] - zcol[c] + (a[r][c]==0): print(0) else: print(tot*inv(row[r]*col[c]%MOD)%MOD*max(a[r][c],1)%MOD)