MOD = 10**9+7 H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] MHR = [1] * H MWR = [1] * W total = 1 for h in range(H): for w in range(W): MHR[h] *= A[h][w] MHR[h] %= MOD MWR[w] *= A[h][w] MWR[w] %= MOD total *= A[h][w] total %= MOD for h in range(H): MHR[h] = pow(MHR[h],-1,MOD) for w in range(W): MWR[w] = pow(MWR[w],-1,MOD) for _ in range(int(input())): r,c = map(int,input().split()) r -= 1 c -= 1 ans = total * A[r][c] ans %= MOD ans *= MHR[r] ans %= MOD ans *= MWR[c] ans %= MOD print(ans)