import scala.io.StdIn.readLine object Main { def MOD_NUMBER = 1000000007 def modMultiply(x: Int, y: Int): Int = { val result = x * y if (result > MOD_NUMBER) result % MOD_NUMBER else result } def main(args: Array[String]): Unit = { val arr = readLine().split(" ").map(_.toInt) val h = arr(0) val w = arr(1) val numbers = 0 until h map(_ => readLine().split(" ").map(_.toInt)) val q = readLine().toInt val lines = 0 until q map(_ => readLine().split(" ").map(_.toInt)) val rowResults = numbers map(row => row.reduceLeft(_ * _)) lines foreach { case Array(row, column) => { val result = 0 until h filter(_ != row - 1) map(i => rowResults(i) / numbers(i)(column - 1)) reduceLeft modMultiply System.out.println(result) } } } }