package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of `strconv.Atoi()`.
// If `strconv.Atoi()` returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var h, w int
var R, C []int

var cells [1000 + 5][1000 + 5]int  // 1: 黒, 0: 白
var dones [1000 + 5][1000 + 5]bool // true: 確定

func main() {
	h, w = ReadInt2()
	R = ReadIntSlice(h)
	C = ReadIntSlice(w)

	// 行は無責任に決める
	for i := 0; i < h; i++ {
		r := R[i]
		for j := 0; j < r; j++ {
			cells[i][j] = 1
			dones[i][j] = true
		}

		// 白で確定させる
		cells[i][r] = 0
		dones[i][r] = true
	}

	// 列はチェックしながら決める
	for j := 0; j < w; j++ {
		c := C[j]
		for i := 0; i < c; i++ {
			if dones[i][j] {
				// すでに決定済みかつ、白でないとダメなら矛盾
				// 黒だったならOK
				if cells[i][j] == 0 {
					fmt.Println(0)
					return
				}
			} else {
				cells[i][j] = 1
				dones[i][j] = true
			}
		}

		// 白で確定させる
		if dones[c][j] {
			// すでに決定済みかつ、黒でないとダメなら矛盾
			// 白だったならOK
			if cells[c][j] == 1 {
				fmt.Println(0)
				return
			}
		} else {
			cells[c][j] = 0
			dones[c][j] = true
		}
	}

	ans := int64(1)
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if !dones[i][j] {
				ans *= int64(2)
				ans %= int64(MOD)
			}
		}
	}
	fmt.Println(ans)
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	"fmt"
)

func ans(x int) int64 {

	var s int64 = 1

	for ; x > 0; x-- {
		s = s * 2 % 1000000007
	}
	return s
}

var data [1002][1002]int

func main() {
	var h, w, t int
	fmt.Scanf("%d %d\n", &h, &w)

	for i := 0; i < h; i++ {
		if i == h-1 {
			fmt.Scanf("%d\n", &t)
		} else {
			fmt.Scanf("%d", &t)
		}
//		fmt.Printf("%d ", t)
		data[i][t] = 1
		for j := 0; j < t; j++ {
			data[i][j] = 2
		}
	}

	for i := 0; i < w; i++ {
		if i == w-1 {
			fmt.Scanf("%d\n", &t)
		} else {
			fmt.Scanf("%d", &t)
		}

//		fmt.Printf("%d ", t)

		if data[t][i] == 2 {
			fmt.Printf("0\n")
			return
		}
		data[t][i] = 1
		for j := 0; j < t; j++ {
			if data[j][i] == 1 {
				fmt.Printf("0\n")
				return
			}

			data[j][i] = 2
		}
	}

//	fmt.Scan()

	s := 0

	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if data[i][j] == 0 {
				s++
			}
		}
	}

	fmt.Printf("%d\n", ans(s))
}

// Author: sighduck
// URL: https://codeforces.com/contest/1228/problem/B

package main

import (
	"bufio"
	"fmt"
	"os"
)

const M = 1000000007

func readArray(size int, reader *bufio.Reader) []int {
	arr := make([]int, size)
	for i := 0; i < size; i++ {
		fmt.Fscanf(reader, "%d", &arr[i])
	}
	fmt.Fscanf(reader, "\n")
	return arr
}

func _fastPow(base int64, power int, acc int64) int64 {
	if power == 0 {
		return acc % M
	} else if power == 1 {
		return base * acc % M
	} else if power%2 == 0 {
		return _fastPow(base*base%M, power/2, acc%M)
	}
	return _fastPow(base*base%M, power/2, acc*base%M)
}

func fastPow(base int64, power int) int64 {
	return _fastPow(base, power, 1)
}

func Solve(h int, w int, r []int, c []int) int64 {
	grid := make([][]int, h)
	for i := 0; i < h; i++ {
		grid[i] = make([]int, w)
	}
	for i, ri := range r {
		for j := 0; j < ri; j++ {
			grid[i][j] = 1
		}
		if ri < w {
			grid[i][ri] = -1
		}
	}
	for i, ci := range c {
		for j := 0; j < ci; j++ {
			if grid[j][i] == -1 {
				return 0
			}
			grid[j][i] = 1
		}
		if ci < h {
			if grid[ci][i] == 1 {
				return 0
			}
			grid[ci][i] = -1
		}
	}
	count := 0
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if grid[i][j] == 0 {
				count += 1
			}
		}
	}
	return fastPow(int64(2), count)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var h, w int
	fmt.Fscanf(reader, "%d %d\n", &h, &w)
	r := readArray(h, reader)
	c := readArray(w, reader)

	fmt.Fprintf(writer, "%d\n", Solve(h, w, r, c))
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	h, w := readInt(), readInt()
	r := make([]int, h)
	c := make([]int, w)
	a := make([][]int, h)
	for i := 0; i < h; i++ {
		r[i] = readInt()
		a[i] = make([]int, w)
	}
	for i := 0; i < w; i++ {
		c[i] = readInt()
	}

	for i := 0; i < h; i++ {
		for j := 0; j < r[i]; j++ {
			a[i][j] = 2
		}
		if r[i] < w {
			a[i][r[i]] = 1
		}
	}

	for i := 0; i < w; i++ {
		for j := 0; j < c[i]; j++ {
			if a[j][i] == 1 {
				printInt(0)
				return
			}
			a[j][i] = 2
		}
		if c[i] < h {
			if a[c[i]][i] == 2 {
				printInt(0)
				return
			}
			a[c[i]][i] = 1
		}
	}

	ans := int64(1)
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if a[i][j] == 0 {
				ans = (ans * 2) % 1000000007
			}
		}
	}

	printInt64(ans)

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of `strconv.Atoi()`.
// If `strconv.Atoi()` returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var h, w int
var R, C []int

var cells [1000 + 5][1000 + 5]int
var dones [1000 + 5][1000 + 5]bool

func main() {
	h, w = ReadInt2()
	R = ReadIntSlice(h)
	C = ReadIntSlice(w)

	// 行は無責任に決める
	for i := 0; i < h; i++ {
		r := R[i]
		for j := 0; j < r; j++ {
			cells[i][j] = 1
			dones[i][j] = true
		}

		// if r > 0 {
		// 白で確定させる
		cells[i][r] = 0
		dones[i][r] = true
		// }
	}
	// for i := 0; i < h; i++ {
	// 	fmt.Println(dones[i][:w])
	// }

	// 列はチェックしながら決める
	for j := 0; j < w; j++ {
		c := C[j]
		for i := 0; i < c; i++ {
			if dones[i][j] {
				// すでに決定済みかつ、白でないとダメなら矛盾
				// 黒だったならOK
				if cells[i][j] == 0 {
					fmt.Println(0)
					return
				}
			} else {
				cells[i][j] = 1
				dones[i][j] = true
			}
		}

		// if c > 0 {
		// 白で確定させる
		if dones[c][j] {
			// すでに決定済みかつ、黒でないとダメなら矛盾
			// 白だったならOK
			if cells[c][j] == 1 {
				fmt.Println(0)
				return
			}
		} else {
			cells[c][j] = 0
			dones[c][j] = true
		}
		// }
	}
	// for i := 0; i < h; i++ {
	// 	fmt.Println(dones[i][:w])
	// }

	ans := int64(1)
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if !dones[i][j] {
				ans *= int64(2)
				ans %= int64(MOD)
			}
		}
	}
	fmt.Println(ans)

	// for i := 0; i < h; i++ {
	// 	fmt.Println(dones[i][:w])
	// }
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var h, w int
	Fscan(in, &h, &w)
	r := make([]int, h)
	for i := range r {
		Fscan(in, &r[i])
	}
	c := make([]int, w)
	for i := range c {
		Fscan(in, &c[i])
	}
	const mx = 1005
	cell := [mx][mx]int{}
	for i, ri := range r {
		if ri == 0 {
			cell[i][0] = 1
		} else {
			for j := 0; j < ri; j++ {
				cell[i][j] = 2
			}
			cell[i][ri] = 1
		}
	}
	for j, ci := range c {
		if ci == 0 {
			if cell[0][j] == 2 {
				Fprintln(out, 0)
				return
			}
			cell[0][j] = 1
		} else {
			for i := 0; i < ci; i++ {
				if cell[i][j] == 1 {
					Fprintln(out, 0)
					return
				}
				cell[i][j] = 2
			}
			if cell[ci][j] == 2 {
				Fprintln(out, 0)
				return
			}
			cell[ci][j] = 1
		}
	}

	const mod int64 = 1e9 + 7
	ans := int64(1)
	for i := range r {
		for j := range c {
			if cell[i][j] == 0 {
				ans = ans * 2 % mod
			}
		}
	}
	Fprintln(out, ans)
}

func main() {
	solve(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
)

func main() {
	defer Flush()

	h := readi()
	w := readi()
	_, r := readInts(h)
	_, c := readInts(w)

	f := make([][][2]bool, h)
	for i := range f {
		f[i] = make([][2]bool, w)
	}

	for i, tw := range r {
		for j := 0; j < tw; j++ {
			f[i][j][0] = true
		}
		if tw < w {
			f[i][tw][1] = true
		}
	}

	for j, th := range c {
		for i := 0; i < th; i++ {
			f[i][j][0] = true
		}
		if th < h {
			f[th][j][1] = true
		}
	}

	ans := 1
	for i := range f {
		for j := range f[i] {
			if f[i][j][0] && f[i][j][1] {
				ans = 0
			}
			if !f[i][j][0] && !f[i][j][1] {
				ans = ans * 2 % INF
			}
		}
	}
	println(ans)
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
)

func main() {
	defer Flush()

	h := readi()
	w := readi()
	_, r := readInts(h)
	_, c := readInts(w)

	g := make([][]int, h)
	f := make([][]int, h)
	for i := range f {
		f[i] = make([]int, w)
		g[i] = make([]int, w)
		for j := range f[i] {
			f[i][j] = 2
			g[i][j] = 2
		}
	}

	for i := range r {
		for j := 0; j < min(r[i]+1, w); j++ {
			f[i][j] = 1
			if j < r[i] {
				g[i][j] = 1
			} else {
				g[i][j] = 0
			}
		}
	}

	for j := range c {
		for i := 0; i < min(c[j]+1, h); i++ {
			v := 1
			if i == c[j] {
				v = 0
			}
			if f[i][j] == 1 {
				if g[i][j] != v {
					f[i][j] = 0
					continue
				}
			}
			f[i][j] = 1
		}
	}

	ans := 1
	for i := range f {
		for j := range f[i] {
			ans = ans * f[i][j] % INF
		}
	}
	println(ans)
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

import (
	"fmt"
)

func ans(x int) int64 {

	var s int64 = 1

	for ; x > 0; x-- {
		s = s * 2 % 1000000007
	}
	return s
}

var data [1002][1002]int

func main() {
	var h, w, t int
	fmt.Scanf("%d %d\n", &h, &w)

	for i := 0; i < h; i++ {
		fmt.Scanf("%d", &t)
		fmt.Printf("%d ", t)
		data[i][t] = 1
		for j := 0; j < t; j++ {
			data[i][j] = 2
		}
	}
	fmt.Scan()

	for i := 0; i < w; i++ {
		fmt.Scanf("%d", &t)

		fmt.Printf("%d ", t)

		if data[t][i] == 2 {
			fmt.Printf("0\n")
			return
		}
		data[t][i] = 1
		for j := 0; j < t; j++ {
			if data[j][i] == 1 {
				fmt.Printf("0\n")
				return
			}

			data[j][i] = 2
		}
	}

	fmt.Scan()

	s := 0

	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if data[i][j] == 0 {
				s++
			}
		}
	}

	fmt.Printf("%d\n", ans(s))
}

package main

import (
	"fmt"
)

func ans(x int) int64 {

	var s int64 = 1

	for ; x > 0; x-- {
		s = s * 2 % 1000000007
	}
	return s
}

var data [1002][1002]int

func main() {
	var h, w, t int
	fmt.Scanf("%d %d\n", &h, &w)

	for i := 0; i < h; i++ {
		fmt.Scanf("%d", &t)

		data[i][t] = 1
		for j := 0; j < t; j++ {
			data[i][j] = 2
		}
	}
	fmt.Scan()

	for i := 0; i < w; i++ {
		fmt.Scanf("%d", &t)

		if data[t][i] == 2 {
			fmt.Printf("0\n")
			return
		}
		data[t][i] = 1
		for j := 0; j < t; j++ {
			if data[j][i] == 1 {
				fmt.Printf("0\n")
				return
			}

			data[j][i] = 2
		}
	}

	fmt.Scan()

	s := 0

	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if data[i][j] == 0 {
				s++
			}
		}
	}

	fmt.Printf("%d\n", ans(s))
}

package main

import (
	"fmt"
)

func ans(x int) int64 {

	var s int64 = 1

	for ; x > 0; x-- {
		s = s * 2 % 1000000007
	}
	return s
}

var data [1002][1002]int

func main() {
	var h, w, t int
	fmt.Scanf("%d %d\n", &h, &w)

	for i := 0; i < h; i++ {
		if i == h-1 {
			fmt.Scanf("%d\n", &t)
		} else {
			fmt.Scanf("%d", &t)
		}
		fmt.Printf("%d ", t)
		data[i][t] = 1
		for j := 0; j < t; j++ {
			data[i][j] = 2
		}
	}

	for i := 0; i < w; i++ {
		if i == w-1 {
			fmt.Scanf("%d\n", &t)
		} else {
			fmt.Scanf("%d", &t)
		}

		fmt.Printf("%d ", t)

		if data[t][i] == 2 {
			fmt.Printf("0\n")
			return
		}
		data[t][i] = 1
		for j := 0; j < t; j++ {
			if data[j][i] == 1 {
				fmt.Printf("0\n")
				return
			}

			data[j][i] = 2
		}
	}

	fmt.Scan()

	s := 0

	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if data[i][j] == 0 {
				s++
			}
		}
	}

	fmt.Printf("%d\n", ans(s))
}

// Author: sighduck
// URL: https://codeforces.com/contest/1228/problem/B

package main

import (
	"bufio"
	"fmt"
	"os"
)

const M = 1000000007

func readArray(size int, reader *bufio.Reader) []int {
	arr := make([]int, size)
	for i := 0; i < size; i++ {
		fmt.Fscanf(reader, "%d", &arr[i])
	}
	fmt.Fscanf(reader, "\n")
	return arr
}

func _fastPow(base int64, power int, acc int64) int64 {
	if power == 0 {
		return acc % M
	} else if power == 1 {
		return base * acc % M
	} else if power%2 == 0 {
		return _fastPow(base*base%M, power/2, acc%M)
	}
	return _fastPow(base*base%M, power/2, acc*base%M)
}

func fastPow(base int64, power int) int64 {
	return _fastPow(base, power, 1)
}

func Solve(h int, w int, r []int, c []int) int64 {
	grid := make([][]int, h)
	for i := 0; i < h; i++ {
		grid[i] = make([]int, w)
	}
	for i, ri := range r {
		for j := 0; j < ri; j++ {
			grid[i][j] = 1
		}
		if ri < w {
			grid[i][ri] = -1
		}
	}
	for i, ci := range c {
		for j := 0; j < ci; j++ {
			if grid[j][i] == -1 {
				return 0
			}
			grid[j][i] = 1
		}
		if ci < h {
			grid[ci][i] = -1
		}
	}
	count := 0
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if grid[i][j] == 0 {
				count += 1
			}
		}
	}
	return fastPow(int64(2), count)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var h, w int
	fmt.Fscanf(reader, "%d %d\n", &h, &w)
	r := readArray(h, reader)
	c := readArray(w, reader)

	fmt.Fprintf(writer, "%d\n", Solve(h, w, r, c))
}


package main

import (
	"fmt"
)

func main() {
	var n int
	var s1, s2 string
	fmt.Scanf("%d\n%s\n%s\n", &n, &s1, &s2)

	var pairs [26][26]int
	distance := 0
	for i := 0; i < n; i++ {
		pairs[s1[i]-'a'][s2[i]-'a'] = i + 1
		if s1[i] != s2[i] {
			distance++
		}
	}

	for i := 1; i < 26; i++ {
		for j := 0; j < i; j++ {
			if pairs[i][j] > 0 && pairs[j][i] > 0 {
				fmt.Printf(
					"%d\n%d %d\n", distance-2, pairs[i][j], pairs[j][i])
				return
			}
		}
	}

	for i := 0; i < 26; i++ {
		for j := 0; j < 26; j++ {
			if i != j && pairs[i][j] > 0 {
				for k := 0; k < 26; k++ {
					if j != k && pairs[j][k] > 0 {
						fmt.Printf(
							"%d\n%d %d\n", distance-1, pairs[i][j], pairs[j][k])
						return
					}
				}
			}
		}
	}

	fmt.Printf("%d\n%d %d\n", distance, -1, -1)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	scanner = bufio.NewScanner(os.Stdin)
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
)

const MAXC int = 26

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, dis int
	var s, t string
	var chars [MAXC][MAXC]int
	scan(&n, &s, &t)

	for i := 0; i < n; i++ {
		if s[i] != t[i] {
			dis++
			chars[s[i] - 'a'][t[i] - 'a'] = i+1
			// println(string(s[i]), string(t[i]))
		}
	}

	for i := 0; i < MAXC; i++ {
		for j := 0; j < MAXC; j++ {
			if chars[i][j] != 0 && chars[j][i] != 0 {
				println(dis-2)
				println(chars[i][j], chars[j][i])
				return
			}
		}
	}

	for i := 0; i < MAXC; i++ {
		for j := 0; j < MAXC; j++ {
			if chars[i][j] != 0 {
				for k := 0; k < MAXC; k++ {
					if chars[j][k] != 0 {
						println(dis-1)
						println(chars[i][j], chars[j][k])
						return
					}
				}
			}
		}
	}

	println(dis)
	println(-1, -1)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
  "os"
  "bufio"
  "strconv"
  "strings"
  "fmt"
)

type Pair struct {
  X, Y byte
}

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')
  a := []byte(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')
  b := []byte(strings.TrimSpace(line))
  pairs := map[Pair]int{}
  singlesA, singlesB := map[byte]int{}, map[byte]int{}
  swapI, swapJ := -1, -1
  for i := 0; i < n; i++ {
    x, y := a[i], b[i]
    if x == y {
      continue
    }
    pair, reversed := Pair{ x, y }, Pair{ y, x }
    if pairs[reversed] > 0 {
      swapI, swapJ = i+1, pairs[reversed]
      break
    }
    pairs[pair] = i+1
    if singlesA[y] > 0 {
      swapI, swapJ = i+1, singlesA[y]
    }
    if singlesB[x] > 0 {
      swapI, swapJ = i+1, singlesB[x]
    }
    singlesA[x], singlesB[y] = i+1, i+1
  }
  if swapI != -1 {
    a[swapI-1], a[swapJ-1] = a[swapJ-1], a[swapI-1]
  }
  //writer.WriteString(fmt.Sprintf("%s\n%s\n", a, b))
  //writer.WriteString(fmt.Sprintf("n = %d, swapI = %d, swapJ = %d\n",
  //    n, swapI, swapJ))
  hamming := 0
  for i := 0; i < n; i++ {
    if a[i] != b[i] {
      hamming++
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n%d %d\n", hamming, swapJ, swapI))
}


package main

import (
	"bufio"
	."fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1554C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		m++
		ans := 0
		for i := 29; i >= 0; i-- {
			x, y := n>>i&1, m>>i&1
			if x > 0 && y == 0 {
				break
			}
			if x == 0 && y > 0 {
				ans |= 1 << i
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1554C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		for i := 0; i <= 29; i++ {
			if ((n^ans)>>i)&1 == 0 {
				ans |= 1 << i
				ans &= ^(1<<i-1)
				break
			}
		}
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		for i := 0; i <= 29; i++ {
			if (m>>i)&1 == 0 {
				ans &= ^((1 << i) - 1)
				ans |= 1 << i
				break
			}
		}
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1554C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		m++
		ans := 0
		for i := 29; i >= 0; i-- {
			x, y := n>>i&1, m>>i&1
			if x > 0 && y == 0 {
				break
			}
			if x == 0 && y > 0 {
				ans |= 1 << i
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1554C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"os"
)

var (
	in  = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Read() int {
	in.Scan()
	r := 0
	for _, v := range in.Bytes() {
		r = r*10 + int(v-'0')
	}
	return r
}

func init() {
	const MaxInt = int(^uint(0) >> 1)
	in.Buffer(make([]byte, 1<<12), MaxInt)
	in.Split(bufio.ScanWords)
}

func main() {
	defer out.Flush()
	for t := Read(); t > 0; t-- {
		n, m := uint32(Read()), uint32(Read())
		k := ^uint32(0) ^ n
		for i := 31; i >= 0; i-- {
			if k^1<<i^n > m {
				k &= ^uint32(1 << i)
			}
		}
		Fprintln(out, k)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		for i := 0; i <= 29; i++ {
			if (m>>i)&1 == 0 {
				ans |= 1 << i
				break
			}
		}
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	if n == m {
		printf("%v\n", n+1)
		return
	}
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		ans++
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		for i := 0; i <= 29; i++ {
			if ((n^ans)>>i)&1 == 0 {
				ans |= 1 << i
				break
			}
		}
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		if ans == 0 {
			ans = n + 1
		} else {
			ans++
		}
	}
	printf("%v\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)
	var ans = 0
	for i := 29; i >= 0; i-- {
		if n^ans > m {
			break
		}
		if (m>>i)&1 > (n>>i)&1 {
			ans |= 1 << i
		}
	}
	if ans^n == m {
		ans++
	}
	printf("%v\n", ans)
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1409B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	f := func(a, b, x, y, n int64) int64 {
		d := min(a-x, n)
		a -= d
		n -= d
		d = min(b-y, n)
		b -= d
		n -= d
		return a * b
	}

	var T, a, b, x, y, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &a, &b, &x, &y, &n)
		Fprintln(out, min(f(a, b, x, y, n), f(b, a, y, x, n)))
	}
}

func main() { CF1409B(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func getans(a, b, x, y, n int64) int64 {
	an := a - min(n, a - x)
	n = n - (a - an)

	bn := b - min(n, b - y)
	n = n - (b - bn)
	return an*bn
}

func solve() {
	var a, b, x, y, n int64
	scanf("%d %d %d %d %d\n", &a, &b, &x, &y, &n)

	printf("%d\n", min(getans(a, b, x, y, n), getans(b, a, y, x, n)))
}

func main() {
	t := 0
	scanf("%d\n", &t)
	for t > 0 {
		t--
		solve()
	}

	writer.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	T := sc.NextInt()
	for i := 0; i < T; i++ {
		a, b, x, y, n := sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt()

		A := core(a, b, x, y, n, 0)
		B := core(a, b, x, y, n, 1)
		fmt.Println(min(A, B))
	}

}

func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func core(a int, b int, x int, y int, n int, typ int) int64 {
	if typ == 0 {
		a, n = funcName(a, x, n)
		b, n = funcName(b, y, n)
	} else {
		b, n = funcName(b, y, n)
		a, n = funcName(a, x, n)
	}
	return int64(a) * int64(b)
}

func funcName(a int, x int, n int) (int, int) {
	if a-x > n {
		a -= n
		n = 0
	} else {
		n -= a - x
		a = x
	}
	return a, n
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func getProd(a, b, x, y, n int64) int64 {
	// decrease a
	a -= n
	n = 0
	if a < x {
		n = x - a
		a = x
	}

	if n > 0 {
		b -= n
		if b < y {
			b = y
		}
	}

	return a * b
}

func main() {
	// DEBUG ONLY
	// f, _ := os.Open("data.txt")
	// scanner := bufio.NewScanner(f)

	scanner := bufio.NewScanner(os.Stdin)

	var nCount int64
	scanner.Scan()

	fmt.Sscanf(scanner.Text(), "%d", &nCount)

	var a, b, x, y, n, prod1, prod2 int64

	for ; nCount > 0; nCount-- {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d %d %d %d", &a, &b, &x, &y, &n)
		prod1 = getProd(a, b, x, y, n)
		prod2 = getProd(b, a, y, x, n)

		if prod2 < prod1 {
			prod1 = prod2
		}

		fmt.Printf("%d\n", prod1)
	}

}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// Credits - github.com/EndlessCheng/codeforces-go
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var a,b, x,y, n int64
		Fscan(in, &a, &b, &x, &y, &n);

		min:=func(a,b int64) int64{
			if a>b {
				return b
			}
			return a
		}

		solve:=func(a,x,b,y,n int64) int64{
			k:=min(a-x,n)
			a-=k
			n-=k
			k=min(b-y,n)
			b-=k
			n-=k
			return a*b
		}

		Fprintln(out,min(solve(a,x,b,y,n),solve(b,y,a,x,n)))
	}
}

func main() { solve(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		a, b, x, y, n := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()

		try := func(a, b, x, y, n int) int64 {
			aa := min(n, a-x)
			a -= aa
			n -= aa
			bb := min(n, b-y)
			b -= bb
			return int64(a) * int64(b)
		}
		ans := min64(try(a, b, x, y, n), try(b, a, y, x, n))
		io.Println(ans)
	}
}

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func min64(i, j int64) int64 {
	if i < j {
		return i
	}
	return j
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func max(i, j int) int {
	if i < j {
		return j
	}

	return i
}

func min(i, j int) int {
	if i > j {
		return j
	}

	return i
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	for t := in(); t > 0; t-- {
		a, b, x, y, n := in(), in(), in(), in(), in()

		a1 := min(a-x, n)
		a1 = max(0, a1)
		b1 := n - a1
		b1 = min(b-y, b1)

		var v1 int64 = int64(a-a1) * int64(b-b1)

		a1 = min(b-y, n)
		a1 = max(0, a1)
		b1 = n - a1
		b1 = min(a-x, b1)

		var v2 int64 = int64(b-a1) * int64(a-b1)

		if v1 < v2 {
			fmt.Println(v1)
		} else {
			fmt.Println(v2)
		}

	}
}

package main

import (
	"fmt"
	"bufio"
	"os"
)

func main(){
	var (
		t int
		a,b,x,y,n,ma,mb,ans uint64
	)
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i:=1;i<=t;i++ {
		fmt.Fscan(in, &a, &b, &x, &y, &n)
		if a-x<=n {
			ma = x
		}else{
			ma = a-n
		}
		if b-y<=n {
			mb = y
		}else{
			mb = b-n
		}
		if ma<=mb {
			n = n-(a-ma)
			if b-y<=n {
				mb = y
			}else{
				mb = b-n
			}
		}else{
			n = n-(b-mb)
			if a-x<=n {
				ma = x
			}else{
				ma = a-n
			}
		}
		ans = ma*mb
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	T := sc.NextInt()
	for i := 0; i < T; i++ {
		a, b, x, y, n := sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt()

		A := core(a, b, x, y, n, 0)
		B := core(a, b, x, y, n, 1)
		fmt.Println(min(A, B))
	}

}

func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func core(a int, b int, x int, y int, n int, typ int) int {
	if typ == 0 {
		a, n = funcName(a, x, n)
		b, n = funcName(b, y, n)
	} else {
		b, n = funcName(b, y, n)
		a, n = funcName(a, x, n)
	}
	return a * b
}

func funcName(a int, x int, n int) (int, int) {
	if a-x > n {
		a -= n
		n = 0
	} else {
		n -= a - x
		a = x
	}
	return a, n
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {

	n := getInt()
	if n > 26 {
		fmt.Println("-1")
		return
	}
	w := getWord()
	cnt := map[rune]bool{}
	for _, c := range w {
		cnt[c] = true
	}
	fmt.Println(n - len(cnt))
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

import "fmt"

func main() {
	var l int
	var s string
	fmt.Scanf("%d\n%s", &l, &s)
	if l > 26 {
		fmt.Println(-1)
		return
	}
	mp := make(map[int]int)
	for _, v := range s {
		mp[int(v)]++
	}
	fmt.Println(l - len(mp))
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		if n > 26 {
			fmt.Println(-1)
		} else if n < 2 {
			fmt.Println(0)
		} else {
			cnts := make([]int, 26)
			for i := 0; s[i] != '\r' && s[i] != '\n'; i++ {
				cnts[int(s[i]-'a')]++
			}
			dup_cnt := 0
			for i := 0; i < 26; i++ {
				if cnts[i] > 1 {
					dup_cnt += (cnts[i] - 1)
				}
			}
			fmt.Println(dup_cnt)
		}
	}
}

package main

import (
	"fmt"
)

func solve(n int, s string) int {
	if n > 26 {
		return -1
	}

	counts := make([]int, 26, 26)
	for i := 0; i < len(s); i++ {
		counts[s[i]-'a']++
	}

	diff := 0
	for i := 0; i < 26; i++ {
		if counts[i] != 0 {
			diff++
		}
	}

	return len(s) - diff
}

func main() {
	var n int
	fmt.Scan(&n)
	var s string
	fmt.Scan(&s)

	fmt.Println(solve(n, s))
}

package main

import "fmt"

func main() {
	var (
		strLen int
		str string
	)
	fmt.Scanf("%d\n", &strLen)
	fmt.Scan(&str)

	charMap := make(map[rune]int)

	for i := 0; i< strLen; i++ {
		charMap[rune(str[i])]++
	}

	totalChanges := 0
	diffCharsCount := len(charMap)
	for _, amount := range charMap {
		totalChanges += amount - 1
	}
	if totalChanges > (26 - diffCharsCount) {
		fmt.Println(-1)
	} else {
		fmt.Println(totalChanges)
	}
}

package main

import "fmt"

func main() {
    var n int
    var str string
    fmt.Scan(&n, &str)

    var runeCnt []int = make([]int, 256)
    var cnt int
    for _, r := range str {
        runeCnt[int(r)]++
        if runeCnt[int(r)] > 1 {
            cnt++
        }
    }
    if n > 26 {
        fmt.Print(-1)
    } else {
        fmt.Print(cnt)
    }
}

package main

import "fmt"

func main() {
	var l int
	var s string
	fmt.Scanf("%d\n%s", &l, &s)
	if l > 26 {
		fmt.Println(-1)
	}
	mp := make(map[int]int)
	for _, v := range s {
		mp[int(v)]++
	}
	fmt.Println(l - len(mp))
}


package main

import "fmt"

func main(){
	var f, g, j, k, n, m int;
	var d [55]int
	fmt.Scan(&n, &m)
	for i := 0; i<n; i++ {
		fmt.Scan(&k)
		g=0
		for x:=0; x<k; x++ {
			fmt.Scan(&j)
			if m > j && g == 0 {
				d[f] = i
				f++
				g=1
			}
		}
	}
	fmt.Println(f)
	for i :=0; i<f; i++ {
		fmt.Print(d[i]+1, " ")
	}
	fmt.Println()
}

package main
import (
    "fmt"
    //"unicode/utf8"
    "strconv"
)

func main() {
    var n,n2 int
    var modal string
    flag:=false
    simpan :=make([]int,51)
    kumpul :=make([]string,51)
    counter:=0
    fmt.Scan(&n,&modal)
    asd, _ := strconv.Atoi(modal)
    for i:=0;i<n;i++ {
        flag=false
        fmt.Scan(&n2)
        for j:=0;j<n2;j++ {
            fmt.Scan(&kumpul[j])
            abc, _ := strconv.Atoi(kumpul[j])
                if abc < asd {
                    flag=true
                    simpan[i+1]++
                }
        }
        if flag==true {
            counter++
        }
    }
    fmt.Println(counter)
    for i:=0;i<51;i++ {
        if simpan[i]>0 {
            fmt.Print(i," ")
        }
    }

}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, v := nextInt(), nextInt()

	countSellers := 0
	sellers := make([]bool, n)

	for i:=0; i<n; i++ {
		item := nextInt()

		for j:=0; j<item; j++ {
			currentPrice := nextInt()

			if !sellers[i] && currentPrice < v {
				countSellers++
				sellers[i] = true
			}
		}
	}

	println(countSellers)
	if countSellers > 0 {
		for i := range sellers {
			if sellers[i] {
				print(i+1, " ")
			}
		}
	}
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var n,v,k,s int
    fmt.Scan(&n,&v)
    cnt, ans := 0, ""
    for i := 1; i <= n; i++ {
        fmt.Scan(&k)
        deal := false
        for j := 0; j < k; j++ {
            fmt.Scan(&s)
            if v > s { deal = true }
        }
        if deal {
            cnt++
            if ans != "" {
                ans += " "
            }
            ans += strconv.Itoa(i)
        }
    }
    fmt.Println(cnt)
    fmt.Println(ans)
}

package main

import (
    "fmt"
    "bufio"
    "os"
    "strings"
    "strconv"
)

func getInts(s *bufio.Scanner, n int) []int {
    s.Scan()
    ab := strings.Fields(s.Text())
    a := make([]int,n)
    for i:=0; i<n; i++ {
        a[i], _ = strconv.Atoi(ab[i])
    }
    return a
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    nv := getInts(scanner,2)
    n, v := nv[0], nv[1]
    number := make([]int,n)
    count := 0

    for i := 0; i < n; i++ {
        scanner.Scan()
        ss := strings.Fields(scanner.Text())
        m, _ := strconv.Atoi(ss[0])

        for j := 1; j < m+1; j++ {
            b, _ := strconv.Atoi(ss[j])
            if v > b {
                number[count] = i
                count++
                break
            }
        }
    }

    fmt.Println(count)
    for i := 0; i < count; i++ {
        fmt.Printf("%d ", number[i]+1)
    }

}

package main

import (
	"fmt"
	"strings"
)

func main() {
	// i sellers (i < n)
	// k_i items
	// price ... s_ij
	var n, v int
	fmt.Scan(&n)
	fmt.Scan(&v)
	z := 0
	var sellers []int
	for i := 0; i < n; i++ {
		var k int
		fmt.Scan(&k)
		var s [50]int
		for j := 0; j < k; j++ {
			var x int
			fmt.Scan(&x)
			s[j] = x
		}
		for j := 0; j < k; j++ {
			if s[j] < v {
				sellers = append(sellers, i)
				z++
				break
			}
		}
	}
	fmt.Println(z)
	ans := make([]string, len(sellers))
	for i := 0; i < len(ans); i++ {
		ans[i] = fmt.Sprintf("%d", sellers[i]+1)
	}
	fmt.Println(strings.Join(ans, " "))
}

package main

import "fmt"

var sign [55]bool

func main() {
	var n, u, v, k int
	fmt.Scan(&n, &u)

	var on []int
	for i := 1; i <= n; i++ {
		fmt.Scan(&k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			if u > v && !sign[i] {
				sign[i] = true
				on = append(on, i)
			}
		}
	}

	fmt.Println(len(on))
	for i := 0; i < len(on); i++ {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(on[i])
	}
}

package main

import (
    "fmt"
    )

func main() {
    var n, v int
    fmt.Scan(&n, &v)

    sellers := make([]bool, n, n)
    count := 0
    for i := 0; i < n; i++ {
        var k, s int
        min := 1000001
        fmt.Scan(&k)
        for j := 0; j < k; j++ {
            fmt.Scan(&s)
            if s < min {
                min = s
            }
        }
        if min < v {
            count++
            sellers[i] = true
        }
    }

    fmt.Println(count)
    if count > 0 {
        for i := 0; i < n; i++ {
            if sellers[i] {
                fmt.Print(i+1, " ")
            }
        }
        fmt.Println("")
    }
}

// 441A-mic
package main

import (
    "fmt"
)

func main() {
    var n, money, c int
    fmt.Scan(&n, &money)
    var seller [55][55]int
    var canbuy [55]int
    k := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&k[i])
        for m := 0; m < k[i]; m++ {
            fmt.Scan(&seller[i][m])
        }
    }
    for i := 0; i < n; i++ {
        for m := 0; m < k[i]; m++ {
            if money > seller[i][m] {
                canbuy[c] = i + 1
                c++
                break
            }
        }
    }
    fmt.Println(c)
    for i := 0; i < c; i++ {
        fmt.Print(canbuy[i])
        if i != c-1 {
            fmt.Print(" ")
        }
    }
    fmt.Println()
}

package main
import (
	"fmt"
	//"unicode/utf8"
	"strconv"
)

func main() {
	var n,n2 int
	var modal string
	simpan :=make([]int,51)
	kumpul :=make([]string,51)
	counter:=0
	fmt.Scan(&n,&modal)
	asd, _ := strconv.Atoi(modal)
	for i:=0;i<n;i++ {
		fmt.Scan(&n2)
		for j:=0;j<n2;j++ {
			fmt.Scan(&kumpul[j])

			if j==0 {
				abc, _ := strconv.Atoi(kumpul[0])
				if abc < asd {
					counter++
					simpan[i+1]++
				}
			}
		}
	}
	fmt.Println(counter)
	for i:=0;i<51;i++ {
		if simpan[i]>0 {
			fmt.Print(i," ")
		}
	}

}

package main
import (
	"fmt"
	//"unicode/utf8"
	"strconv"
)

func main() {
	var n,n2 int
	var modal string
	simpan :=make([]int,51)
	kumpul :=make([]string,51)
	counter:=0
	fmt.Scan(&n,&modal)
	asd, _ := strconv.Atoi(modal)
	for i:=0;i<n;i++ {
		fmt.Scan(&n2)
		for j:=0;j<n2;j++ {
			fmt.Scan(&kumpul[j])
			abc, _ := strconv.Atoi(kumpul[j])
				if abc < asd {
					counter++
					simpan[i+1]++
					continue
				}
		}
	}
	fmt.Println(counter)
	for i:=0;i<51;i++ {
		if simpan[i]>0 {
			fmt.Print(i," ")
		}
	}

}

package main

import "fmt"

func main() {
    var n,v,k,s int
    fmt.Scan(&n,&v)
    cnt, ans := 0, ""
    for i := 1; i <= n; i++ {
        fmt.Scan(&k)
        deal := false
        for j := 0; j < k; j++ {
            fmt.Scan(&s)
            if v > s { deal = true }
        }
        if deal {
            cnt++
            if ans != "" {
                ans += " "
            }
            ans += string(i+'0')
        }
    }
    fmt.Println(cnt)
    fmt.Println(ans)
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	// i sellers (i < n)
	// k_i items
	// price ... s_ij
	var n, v int
	fmt.Scan(&n)
	fmt.Scan(&v)
	z := 0
	var sellers []int
	for i := 0; i < n; i++ {
		var k int
		fmt.Scan(&k)
		for j := 0; j < k; j++ {
			var s int
			fmt.Scan(&s)
			if v > s {
				sellers = append(sellers, i+1)
				z++
			}
		}
	}
	fmt.Println(z)
	ans := make([]string, len(sellers))
	for i := 0; i < len(ans); i++ {
		ans[i] = fmt.Sprintf("%d", sellers[i])
	}
	fmt.Println(strings.Join(ans, " "))
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	// i sellers (i < n)
	// k_i items
	// price ... s_ij
	var n, v int
	fmt.Scan(&n)
	fmt.Scan(&v)
	z := 0
	var sellers []int
	for i := 0; i < n; i++ {
		var k int
		fmt.Scan(&k)
		for j := 0; j < k; j++ {
			var s int
			fmt.Scan(&s)
			if v > s {
				sellers = append(sellers, i+1)
				z++
				break
			}
		}
	}
	fmt.Println(z)
	ans := make([]string, len(sellers))
	for i := 0; i < len(ans); i++ {
		ans[i] = fmt.Sprintf("%d", sellers[i])
	}
	fmt.Println(strings.Join(ans, " "))
}

package main

import "fmt"

func main() {
	var n, u, v, k int
	fmt.Scan(&n, &u)

	var on []int
	for i := 1; i <= n; i++ {
		fmt.Scan(&k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			if u > v {
				on = append(on, i)
			}
		}
	}

	fmt.Println(len(on))
	for i := 0; i < len(on); i++ {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(on[i])
	}
}

package main

import (
    "fmt"
    )

func main() {
    var n, v int
    fmt.Scan(&n, &v)

    sellers := make([]bool, n, n)
    count := 0
    for i := 0; i < n; i++ {
        var k, s int
        fmt.Scan(&k)
        for j := 0; j < k; j++ {
            fmt.Scan(&s)
            if s < v {
                sellers[i] = true
                count++
            }
        }
    }

    fmt.Println(count)
    if count > 0 {
        for i := 0; i < n; i++ {
            if sellers[i] {
                fmt.Print(i+1, " ")
            }
        }
        fmt.Println("")
    }
}

package main

import (
    "fmt"
    )

func main() {
    var n, v int
    fmt.Scan(&n, &v)

    sellers := make([]bool, n, n)
    count := 0
    for i := 0; i < n; i++ {
        var k, s int
        fmt.Scan(&k)
        for j := 0; j < k; j++ {
            fmt.Scan(&s)
            if j == 0 && s < v {
                sellers[i] = true
                count++
            }
        }
    }

    fmt.Println(count)
    if count > 0 {
        for i := 0; i < n; i++ {
            if sellers[i] {
                fmt.Print(i+1, " ")
            }
        }
        fmt.Println("")
    }
}

// 441A-mic
package main

import (
    "fmt"
)

func main() {
    var n, money, c int
    fmt.Scan(&n, &money)
    var seller [55][55]int
    var canbuy [55]int
    k := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&k[i])
        for m := 0; m < k[i]; m++ {
            fmt.Scan(&seller[i][m])
        }
    }
    for i := 0; i < n; i++ {
        for m := 0; m < k[i]; m++ {
            if money > seller[i][m] {
                c++
                canbuy[i] = i + 1
                break
            } else {
                canbuy[i] = 0
            }
        }
    }
    fmt.Println(c)
    for i := 0; i < c; i++ {
        fmt.Print(canbuy[i])
        if i != c-1 {
            fmt.Print(" ")
        }
    }
    fmt.Println()
}

// 441A-mic
package main

import (
    "fmt"
)

func main() {
    var n, money, c int
    fmt.Scan(&n, &money)
    var seller [55][55]int
    var canbuy [55]int
    k := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&k[i])
        for m := 0; m < k[i]; m++ {
            fmt.Scan(&seller[i][m])
        }
    }
    for i := 0; i < n; i++ {
        for m := 0; m < k[i]; m++ {
            if money > seller[i][m] {
                canbuy[i] = i + 1
                c++
                break
            } else {
                canbuy[i] = 0
            }
        }
    }
    fmt.Println(c)
    for i := 0; i < c; i++ {
        fmt.Print(canbuy[i])
        if i != c-1 {
            fmt.Print(" ")
        }
    }
    fmt.Println()
}

// 441A-mic
package main

import (
    "fmt"
)

func main() {
    var n, money, c int
    fmt.Scan(&n, &money)
    var seller [55][55]int
    var canbuy [55]int
    k := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&k[i])
        for m := 0; m < k[i]; m++ {
            fmt.Scan(&seller[i][m])
        }
    }
    for i := 0; i < n; i++ {
        for m := 0; m < k[i]; m++ {
            if money > seller[i][m] {
                c++
                canbuy[i] = i + 1
                break
            }
        }
    }
    fmt.Println(c)
    for i := 0; i < c; i++ {
        fmt.Print(canbuy[i])
        if i != c-1 {
            fmt.Print(" ")
        }
    }
    fmt.Println()
}


package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	T := NextInt()

	a := make([]int, 200010)
	was := make([]bool, 200010)
	left := make([]bool, 200010)
	right := make([]bool, 200010)
	for t := 0; t < T; t++ {
		n := NextInt()
		for i := 0; i < n; i++ {
			a[i] = NextInt() - 1
			was[i] = false
			left[i] = false
			right[i] = false
		}
		was[n] = false
		left[n] = false
		right[n] = false
		for i, mex := 0, 0; i < n; i++ {
			if was[a[i]] {
				break
			}
			was[a[i]] = true
			for was[mex] {
				mex++
			}
			left[i] = mex == i+1
		}
		for i := 0; i <= n; i++ {
			was[i] = false
		}
		for i, mex := n-1, 0; i >= 0; i-- {
			if was[a[i]] {
				break
			}
			was[a[i]] = true
			for was[mex] {
				mex++
			}
			right[i] = mex == n-i
		}

		res := 0
		for i := 0; i+1 < n; i++ {
			if left[i] && right[i+1] {
				res++
			}
		}
		fmt.Fprintln(out, res)
		for i := 0; i+1 < n; i++ {
			if left[i] && right[i+1] {
				fmt.Fprintln(out, i+1, n-(i+1))
			}
		}

	}

}

/*
URL:
https://codeforces.com/problemset/problem/1330/B
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int

	n int
	A []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		A = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	forward, backward := make([]bool, n), make([]bool, n)

	cmax := 0
	fm := make(map[int]int)
	for i := 0; i < n; i++ {
		ChMax(&cmax, A[i])
		fm[A[i]]++

		if i+1 == len(fm) && cmax == len(fm) {
			forward[i] = true
		} else {
			forward[i] = false
		}
	}

	cmax = 0
	bm := make(map[int]int)
	for i := n - 1; i >= 0; i-- {
		ChMax(&cmax, A[i])
		bm[A[i]]++

		if n-i == len(bm) && cmax == len(bm) {
			backward[i] = true
		} else {
			backward[i] = false
		}
	}

	answers := [][2]int{}
	for i := 0; i < n-1; i++ {
		if forward[i] && backward[i+1] {
			answers = append(answers, [2]int{i + 1, n - (i + 1)})
		}
	}

	fmt.Println(len(answers))
	for _, P := range answers {
		fmt.Println(P[0], P[1])
	}
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

type node struct {
	lr       [2]*node
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node) size() int {
	if o != nil {
		return o.sz
	}
	return 0
}

func (o *node) pushUp() { o.sz = 1 + o.lr[0].size() + o.lr[1].size() }

func (o *node) rotate(d int8) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	o.pushUp()
	x.pushUp()
	return x
}

type treap struct {
	rd   uint
	root *node
}

func (t *treap) fastRand() uint {
	t.rd ^= t.rd << 13
	t.rd ^= t.rd >> 17
	t.rd ^= t.rd << 5
	return t.rd
}

func (t *treap) less(a, b int) int8 {
	switch {
	case a < b:
		return 0
	case a > b:
		return 1
	default:
		return -1
	}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: t.fastRand(), key: key, dupCnt: 1, sz: 1}
	}
	if d := t.less(key, o.key); d >= 0 {
		o.lr[d] = t._put(o.lr[d], key)
		if o.lr[d].priority > o.priority {
			o = o.rotate(d ^ 1)
		}
	} else {
		o.dupCnt++
	}
	o.pushUp()
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if d := t.less(key, o.key); d >= 0 {
		o.lr[d] = t._delete(o.lr[d], key)
	} else {
		if o.dupCnt > 1 {
			o.dupCnt--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			d = 0
			if o.lr[0].priority > o.lr[1].priority {
				d = 1
			}
			o = o.rotate(d)
			o.lr[d] = t._delete(o.lr[d], key)
		}
	}
	o.pushUp()
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) get(key int) *node {
	for o := t.root; o != nil; {
		if d := t.less(key, o.key); d >= 0 {
			o = o.lr[d]
		} else {
			return o
		}
	}
	return nil
}

func (t *treap) min() (min *node) {
	for o := t.root; o != nil; o = o.lr[0] {
		min = o
	}
	return
}

func (t *treap) max() (max *node) {
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return
}

func newTreap() *treap { return &treap{rd: 1} }

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func() {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		s1, s2 := newTreap(), newTreap()
		s1.put(a[0])
		for _, v := range a[1:] {
			s2.put(v)
		}
		ans := [][2]int{}
		for i := 1; i < n; i++ {
			if s1.root.size() == i && s2.root.size() == n-i {
				if s1.min().key == 1 && s1.max().key == i {
					if s2.min().key == 1 && s2.max().key == n-i {
						ans = append(ans, [2]int{i, n - i})
					}
				}
			}
			s1.put(a[i])
			s2.delete(a[i])
		}
		Fprintln(out, len(ans))
		for _, v := range ans {
			Fprintln(out, v[0],v[1])
		}
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		solve()
	}
}

func main() { run(os.Stdin, os.Stdout) }