Suppose there is a $$$h \times w$$$ grid consisting of empty or full cells. Let's make some definitions:  $$$r_{i}$$$ is the number of consecutive full cells connected to the left side in the $$$i$$$-th row ($$$1 \le i \le h$$$). In particular, $$$r_i=0$$$ if the leftmost cell of the $$$i$$$-th row is empty.  $$$c_{j}$$$ is the number of consecutive full cells connected to the top end in the $$$j$$$-th column ($$$1 \le j \le w$$$). In particular, $$$c_j=0$$$ if the topmost cell of the $$$j$$$-th column is empty. In other words, the $$$i$$$-th row starts exactly with $$$r_i$$$ full cells. Similarly, the $$$j$$$-th column starts exactly with $$$c_j$$$ full cells.    These are the $$$r$$$ and $$$c$$$ values of some $$$3 \times 4$$$ grid. Black cells are full and white cells are empty. You have values of $$$r$$$ and $$$c$$$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $$$r$$$ and $$$c$$$. Since the answer can be very large, find the answer modulo $$$1000000007\,(10^{9} + 7)$$$. In other words, find the remainder after division of the answer by $$$1000000007\,(10^{9} + 7)$$$.

The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 \le h, w \le 10^{3}$$$) — the height and width of the grid. The second line contains $$$h$$$ integers $$$r_{1}, r_{2}, \ldots, r_{h}$$$ ($$$0 \le r_{i} \le w$$$) — the values of $$$r$$$. The third line contains $$$w$$$ integers $$$c_{1}, c_{2}, \ldots, c_{w}$$$ ($$$0 \le c_{j} \le h$$$) — the values of $$$c$$$.

NoteIn the first example, this is the other possible case.  In the second example, it's impossible to make a grid to satisfy such $$$r$$$, $$$c$$$ values.In the third example, make sure to print answer modulo $$$(10^9 + 7)$$$.

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  // 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 (
	"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++ {
		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 (
	"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))
}


Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.Help him do this!

The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T. The second line contains string S. The third line contains string T. Each of the lines only contains lowercase Latin letters.

In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S. In the second line, either print the indexes i and j (1 ≤ i, j ≤ n, i ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters. If there are multiple possible answers, print any of them.

NoteIn the second test it is acceptable to print i = 2, j = 3.

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))
}


You are given two integers $$$n$$$ and $$$m$$$. Find the $$$\operatorname{MEX}$$$ of the sequence $$$n \oplus 0, n \oplus 1, \ldots, n \oplus m$$$. Here, $$$\oplus$$$ is the bitwise XOR operator.$$$\operatorname{MEX}$$$ of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, $$$\operatorname{MEX}(0, 1, 2, 4) = 3$$$, and $$$\operatorname{MEX}(1, 2021) = 0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 30\,000$$$)  — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n, m \le 10^9$$$).

For each test case, print a single integer  — the answer to the problem.

NoteIn the first test case, the sequence is $$$3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5$$$, or $$$3, 2, 1, 0, 7, 6$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$4$$$.In the second test case, the sequence is $$$4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6$$$, or $$$4, 5, 6, 7, 0, 1, 2$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$3$$$.In the third test case, the sequence is $$$3 \oplus 0, 3 \oplus 1, 3 \oplus 2$$$, or $$$3, 2, 1$$$. The smallest non-negative integer which isn't present in the sequence i. e. the $$$\operatorname{MEX}$$$ of the sequence is $$$0$$$.

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"
	"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)
}


You are given four integers $$$a$$$, $$$b$$$, $$$x$$$ and $$$y$$$. Initially, $$$a \ge x$$$ and $$$b \ge y$$$. You can do the following operation no more than $$$n$$$ times:  Choose either $$$a$$$ or $$$b$$$ and decrease it by one. However, as a result of this operation, value of $$$a$$$ cannot become less than $$$x$$$, and value of $$$b$$$ cannot become less than $$$y$$$. Your task is to find the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains five integers $$$a$$$, $$$b$$$, $$$x$$$, $$$y$$$ and $$$n$$$ ($$$1 \le a, b, x, y, n \le 10^9$$$). Additional constraint on the input: $$$a \ge x$$$ and $$$b \ge y$$$ always holds.

For each test case, print one integer: the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.

NoteIn the first test case of the example, you need to decrease $$$b$$$ three times and obtain $$$10 \cdot 7 = 70$$$.In the second test case of the example, you need to decrease $$$a$$$ one time, $$$b$$$ one time and obtain $$$11 \cdot 7 = 77$$$.In the sixth test case of the example, you need to decrease $$$a$$$ five times and obtain $$$5 \cdot 11 = 55$$$.In the seventh test case of the example, you need to decrease $$$b$$$ ten times and obtain $$$10 \cdot 1 = 10$$$.

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"
	"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"
	"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
		}
	}
}


A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s. The second line contains the string s of length n consisting of only lowercase English letters.

If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.

NoteIn the first sample one of the possible solutions is to change the first character to 'b'.In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".

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 (
	"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)
	}
	mp := make(map[int]int)
	for _, v := range s {
		mp[int(v)]++
	}
	fmt.Println(l - len(mp))
}


You are given a set of strings $$$S$$$. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:  if the current string is $$$t$$$, choose some lowercase Latin letter $$$c$$$ and append it to the back of $$$t$$$, so the current string becomes $$$t + c$$$. This action takes $$$1$$$ second;  use autocompletion. When you try to autocomplete the current string $$$t$$$, a list of all strings $$$s \in S$$$ such that $$$t$$$ is a prefix of $$$s$$$ is shown to you. This list includes $$$t$$$ itself, if $$$t$$$ is a string from $$$S$$$, and the strings are ordered lexicographically. You can transform $$$t$$$ into the $$$i$$$-th string from this list in $$$i$$$ seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from $$$S$$$?Note that the strings from $$$S$$$ are given in an unusual way.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^6$$$). Then $$$n$$$ lines follow, the $$$i$$$-th line contains one integer $$$p_i$$$ ($$$0 \le p_i < i$$$) and one lowercase Latin character $$$c_i$$$. These lines form some set of strings such that $$$S$$$ is its subset as follows: there are $$$n + 1$$$ strings, numbered from $$$0$$$ to $$$n$$$; the $$$0$$$-th string is an empty string, and the $$$i$$$-th string ($$$i \ge 1$$$) is the result of appending the character $$$c_i$$$ to the string $$$p_i$$$. It is guaranteed that all these strings are distinct. The next line contains one integer $$$k$$$ ($$$1 \le k \le n$$$) — the number of strings in $$$S$$$. The last line contains $$$k$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_k$$$ ($$$1 \le a_i \le n$$$, all $$$a_i$$$ are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set $$$S$$$ — formally, if we denote the $$$i$$$-th generated string as $$$s_i$$$, then $$$S = {s_{a_1}, s_{a_2}, \dots, s_{a_k}}$$$.

Print $$$k$$$ integers, the $$$i$$$-th of them should be equal to the minimum number of seconds required to type the string $$$s_{a_i}$$$.

NoteIn the first example, $$$S$$$ consists of the following strings: ieh, iqgp, i, iqge, ier.

package main

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

var ch [1000005][26] int
var id [1000005] int
var a [1000005]int
var dp [1000005]int
var vis[1000005]bool
var tot int

func dfs1(u int) {
	if vis[u] ==true {
		tot++
		id[u] = tot
	}
	for i := 0; i < 26; i++ {
		if ch[u][i] != 0 {
			dfs1(ch[u][i])
		}
	}
}
func dfs2(u, w int) {
	val:=max(w,tot-dp[u]);
	if vis[u]==true{
		tot++
	}
	for i:=0;i<26;i++{
		v:=ch[u][i]
		if v==0 {
			continue;
		}
		dp[v]=dp[u]+1
		if vis[v]==true{
			dp[v]=min(dp[v],id[v]-val)
		}
		dfs2(v,val)
	}
}
func main() {
	n := ReadInt()
	for i := 1; i <= n; i++ {
		x:=ReadInt()
		s:=ReadString()
		ch[x][s[0]-'a']=i
	}
	q:=ReadInt()
	for i:=1;i<=q;i++{
		a[i]=ReadInt()
		vis[a[i]]=true
	}
	tot=0;dfs1(0)
	tot=0;dfs2(0,0)
	for i:=1;i<=q;i++{
		fmt.Printf("%d ",dp[a[i]])
	}
}

////////////////////////////////////

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr, 10, 32)
	return int(i)
}
func ReadInt64() int64 {
	intstr := ReadString()
	i, _ := strconv.ParseInt(intstr, 10, 64)
	return int64(i)
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}


Valera is a collector. Once he wanted to expand his collection with exactly one antique item.Valera knows n sellers of antiques, the i-th of them auctioned ki items. Currently the auction price of the j-th object of the i-th seller is sij. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.Unfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.

The first line contains two space-separated integers n, v (1 ≤ n ≤ 50; 104 ≤ v ≤ 106) — the number of sellers and the units of money the Valera has. Then n lines follow. The i-th line first contains integer ki (1 ≤ ki ≤ 50) the number of items of the i-th seller. Then go ki space-separated integers si1, si2, ..., siki (104 ≤ sij ≤ 106) — the current prices of the items of the i-th seller.

In the first line, print integer p — the number of sellers with who Valera can make a deal. In the second line print p space-separated integers q1, q2, ..., qp (1 ≤ qi ≤ n) — the numbers of the sellers with who Valera can make a deal. Print the numbers of the sellers in the increasing order.

NoteIn the first sample Valera can bargain with each of the sellers. He can outbid the following items: a 40000 item from the first seller, a 20000 item from the second seller, and a 10000 item from the third seller.In the second sample Valera can not make a deal with any of the sellers, as the prices of all items in the auction too big for him.

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"
	"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"
	//"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"
    "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()
}