mod = 10**9 + 7 h, w = map(int, input().split()) a = [list(map(int, input().split())) for i in range(h)] product_all = 1 product_row = [1] * h product_column = [1] * w for i in range(h): for j in range(w): product_all *= a[i][j] product_all %= mod product_row[i] *= a[i][j] product_row[i] %= mod product_column[j] *= a[i][j] product_column[j] %= mod q = int(input()) for i in range(q): r, c = map(int, input().split()) print(product_all * pow(product_row[r - 1], mod - 2, mod) * pow(product_column[c - 1], mod - 2, mod) * a[r - 1][c - 1] % mod)