package main import ( "bufio" "fmt" "os" "sort" "strconv" ) var sc = bufio.NewScanner(os.Stdin) var wr = bufio.NewWriter(os.Stdout) func out(x ...interface{}) { fmt.Fprintln(wr, x...) } func getI() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func getF() float64 { sc.Scan() i, e := strconv.ParseFloat(sc.Text(), 64) if e != nil { panic(e) } return i } func getInts(N int) []int { ret := make([]int, N) for i := 0; i < N; i++ { ret[i] = getI() } return ret } func getS() string { sc.Scan() return sc.Text() } // min, max, asub, absなど基本関数 func max(a, b int) int { if a > b { return a } return b } func min(a, b int) int { if a < b { return a } return b } func asub(a, b int) int { if a > b { return a - b } return b - a } func abs(a int) int { if a >= 0 { return a } return -a } func lowerBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] >= x }) return idx } func upperBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] > x }) return idx } //---------------------------------------- // modint //---------------------------------------- type modint struct { mod int fracMemo []int ifracMemo []int } func newModint(m int) *modint { var ret modint ret.mod = m ret.fracMemo = []int{1, 1} ret.ifracMemo = []int{1, 1} return &ret } func (m *modint) add(a, b int) int { ret := (a + b) % m.mod if ret < 0 { ret += m.mod } return ret } func (m *modint) sub(a, b int) int { ret := (a - b) % m.mod if ret < 0 { ret += m.mod } return ret } func (m *modint) mul(a, b int) int { ret := a * b % m.mod if ret < 0 { ret += m.mod } return ret } func (m *modint) div(a, b int) int { ret := a * m.modinv(b) ret %= m.mod return ret } func (m *modint) pow(p, n int) int { ret := 1 x := p for n != 0 { if n%2 == 1 { ret *= x ret %= m.mod } n /= 2 x = x * x % m.mod } return ret } // 逆元を使った割り算(MOD) // mod. m での a の逆元 a^{-1} を計算する func (m *modint) modinv(a int) int { b := m.mod u := 1 v := 0 for b != 0 { t := a / b a -= t * b a, b = b, a u -= t * v u, v = v, u } u %= m.mod if u < 0 { u += m.mod } return u } //----------------------------------------------- // 行列累乗 //  A[][]のp乗を求める //----------------------------------------------- func (m *modint) powModMatrix(A [][]int, p int) [][]int { N := len(A) ret := make([][]int, N) for i := 0; i < N; i++ { ret[i] = make([]int, N) ret[i][i] = 1 } for p > 0 { if p&1 == 1 { ret = m.mulMod(ret, A) } A = m.mulMod(A, A) p >>= 1 } return ret } func (m *modint) mulMod(A, B [][]int) [][]int { H := len(A) W := len(B[0]) K := len(A[0]) C := make([][]int, W) for i := 0; i < W; i++ { C[i] = make([]int, W) } for i := 0; i < H; i++ { for j := 0; j < W; j++ { for k := 0; k < K; k++ { C[i][j] += A[i][k] * B[k][j] C[i][j] %= m.mod } } } return C } //--------------------------------------------------- // nCk 計算関連: TELすることがあるかも // ※pow(x, p-2)を何度も取るので // 厳しそうな場合は、ここを削除して高速なのを使う //--------------------------------------------------- func (m *modint) mfrac(n int) int { if len(m.fracMemo) > n { return m.fracMemo[n] } if len(m.fracMemo) == 0 { m.fracMemo = append(m.fracMemo, 1) } for len(m.fracMemo) <= n { size := len(m.fracMemo) m.fracMemo = append(m.fracMemo, m.fracMemo[size-1]*size%m.mod) } return m.fracMemo[n] } func (m *modint) mifrac(n int) int { if len(m.ifracMemo) > n { return m.ifracMemo[n] } if len(m.ifracMemo) == 0 { m.fracMemo = append(m.ifracMemo, 1) } for len(m.ifracMemo) <= n { size := len(m.ifracMemo) m.ifracMemo = append(m.ifracMemo, m.ifracMemo[size-1]*m.pow(size, m.mod-2)%m.mod) } return m.ifracMemo[n] } func (m *modint) nCr(n, r int) int { if n == r { return 1 } if n < r || r < 0 { return 0 } ret := 1 ret *= m.mfrac(n) ret %= m.mod ret *= m.mifrac(r) ret %= m.mod ret *= m.mifrac(n - r) ret %= m.mod return (ret) } func main() { defer wr.Flush() sc.Split(bufio.ScanWords) sc.Buffer([]byte{}, 1000000) // this template is new version. // use getI(), getS(), getInts(), getF() H, W := getI(), getI() a := make([][]int, H) for i := 0; i < H; i++ { a[i] = getInts(W) } mod := newModint(1e9 + 7) tot := 1 RZ := make([]int, H) CZ := make([]int, W) R := make([]int, H) C := make([]int, W) zero := 0 for i := 0; i < H; i++ { R[i] = 1 } for i := 0; i < W; i++ { C[i] = 1 } for i := 0; i < H; i++ { for j := 0; j < W; j++ { if a[i][j] == 0 { RZ[i]++ CZ[j]++ zero++ } else { R[i] = mod.mul(R[i], a[i][j]) C[j] = mod.mul(C[j], a[i][j]) tot = mod.mul(tot, a[i][j]) } } } Q := getI() for i := 0; i < Q; i++ { r, c := getI()-1, getI()-1 if zero == 0 { ans := tot ans = mod.mul(ans, a[r][c]) ans = mod.div(ans, R[r]) ans = mod.div(ans, C[c]) out(ans) } else { zcnt := RZ[r] + CZ[c] if a[r][c] == 0 { zcnt-- } if zcnt == zero { ans := tot if a[r][c] != 0 { ans = mod.mul(ans, a[r][c]) } ans = mod.div(ans, R[r]) ans = mod.div(ans, C[c]) out(ans) } else { out(0) } } } }