package main

import (
	"fmt"
)

var (
	n int
	p []int
	A [][]bool
)

func in() {
	fmt.Scan(&n)
	p = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
	}
	A = make([][]bool, n)
	for i := 0; i < n; i++ {
		A[i] = make([]bool, n)
		var line string
		fmt.Scan(&line)
		for j, ch := range line {
			A[i][j] = ch == '1'
		}
	}
}

func out() {
	fmt.Print(p[0])
	for i := 1; i < n; i++ {
		fmt.Print(" ", p[i])
	}
	fmt.Println()
}

func run() {
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if A[i][k] && A[j][k] && !A[i][j] {
					A[i][j] = true
				}
			}
		}
	}

	for i := 0; i < n-1; i++ {
		var min_j int = 0
		min := p[i]
		for j := i + 1; j < n; j++ {
			if A[i][j] && p[j] < min {
				min = p[j]
				min_j = j
			}
		}
		if min_j > 0 {
			p[i], p[min_j] = p[min_j], p[i]
		}
	}
}

func main() {
	in()
	run()
	out()
}

package main

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

var (
	scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
	writer  *bufio.Writer  = bufio.NewWriter(os.Stdout)
	n       int
	p       []int
	A       [][]bool
)

func in() {
	n = readInt()
	p = make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = readInt()
	}
	A = make([][]bool, n)
	for i := 0; i < n; i++ {
		A[i] = make([]bool, n)
		var line string = readString()
		for j, ch := range line {
			A[i][j] = ch == '1'
		}
	}
}

func out() {
	printInt(p[0])
	for i := 1; i < n; i++ {
		printSpace()
		printInt(p[i])
	}
	printLine()
	defer writer.Flush()
}

func run() {
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if A[i][k] && A[j][k] && !A[i][j] {
					A[i][j] = true
				}
			}
		}
	}

	for i := 0; i < n-1; i++ {
		var min_j int = 0
		min := p[i]
		for j := i + 1; j < n; j++ {
			if A[i][j] && p[j] < min {
				min = p[j]
				min_j = j
			}
		}
		if min_j > 0 {
			p[i], p[min_j] = p[min_j], p[i]
		}
	}
}

func main() {
	scanner.Split(bufio.ScanWords)
	in()
	run()
	out()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printSpace() {
	printString(" ")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFloat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

package main

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

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

var a [][]bool
var p []int
var used []bool
var n int
//Wrong

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

	n = nextInt()
	a = make([][]bool, n)
	p = make([]int, n)
	used = make([]bool, n)

	for i := 0; i < n; i++ {
		p[i] = nextInt()
	}

	for i := range a {
		a[i] = make([]bool, n)
		s := next()
		for j := range s {
			// print(j, s[j], " ")
			if s[j] == '1' {
				a[i][j] = true
			}
		}
		// println()
	}

	// for i := range a {
	// 	println(a[i])
	// }
	// println()
	for i := range a {
		if !used[i] {
			dfs(i)
		}
	}
	// println('1')
	// for i := range a {
	// 	println(a[i])
	// }
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if p[i] > p[j] && a[i][j] {
				p[i], p[j] = p[j], p[i]
			}
		}
	}

	for i := range p {
		print(p[i], " ")
	}

	println()
}

func dfs(x int) {
	used[x] = true

	for i := 0; i < n; i++ {
		if a[x][i] {
			for j := 0; j < n; j++ {
				if a[x][j] {
					// println(x, i, j)
					a[i][j] = true
				}
			}

			if !used[i] {
				dfs(i)
			}
		}
	}
}

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

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 print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	//fmt.Println(matrix)
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}
	//
	//fmt.Println(s.permutation)
	//fmt.Println(s.unions)

	for i, val := range s.permutation {
		index := s.find(i)
		//fmt.Println(i, val, index)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		format := "%d "
		if i + 1 == len(permutation) {
			format = "%d\n"
		}
		fmt.Fscanf(inBuf,format, &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/500/B

package main

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

func Solve(n int, p []int, a []string) []int {
	visited := make(map[int]bool)
	components := [][]int{}
	for i := 1; i <= n; i++ {
		if !visited[i] {
			queue := []int{i}
			visited[i] = true
			component := []int{}
			for len(queue) > 0 {
				curr := queue[0]
				queue = queue[1:]
				component = append(component, curr)
				for j := 1; j <= n; j++ {
					if curr != j && a[curr-1][j-1] == '1' && !visited[j] {
						visited[j] = true
						queue = append(queue, j)
					}
				}
			}
			components = append(components, component)
		}
	}
	for _, c := range components {
		sort.Ints(c)
		pc := make([]int, len(c))
		for i, elem := range c {
			pc[i] = p[elem-1]
		}
		sort.Ints(pc)
		for i, elem := range c {
			p[elem-1] = pc[i]
		}
	}
	return p
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	p := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &p[i])
	}
	fmt.Fscanf(reader, "\n")
	a := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &a[i])
	}

	for _, elem := range Solve(n, p, a) {
		fmt.Fprintf(writer, "%d ", elem)
	}
	fmt.Fprintf(writer, "\n")
}

package main

import (
	"bufio"
	"fmt"
	"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 := nextInt()
	p := make([]int, n)
	a := make([]string, n)

	for i := 0; i < n; i++ {
		p[i] = nextInt()
	}

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	for i := 0; i < n; i++ {
		if swapRow(i, p, a) {
			for j := 0; j < len(p); j++ {
				swapRow(j, p, a)
			}
		}
	}
	println(p)
}

func swapRow(i int, p []int, a []string) bool {
	index := i

	for j := i + 1; j < len(p); j++ {
		if a[i][j] == '1' && p[index] > p[j] {
			index = j
		}
	}

	if i != index {
		p[i], p[index] = p[index], p[i]
		// println(p, i, index)
		return true
	}

	// println(p, i, index)
	return false
}

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

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

package main

import (
	"bufio"
	"fmt"
	"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 := nextInt()
	p := make([]int, n)
	a := make([]string, n)

	for i := 0; i < n; i++ {
		p[i] = nextInt()
	}

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	for i := 0; i < n; i++ {
		if swapRow(i, p, a) {
			for j := 0; j < len(p); j++ {
				swapRow(j, p, a)
			}
		}
	}
	println(p)
}

func swapRow(i int, p []int, a []string) bool {
	index := i

	for j := i + 1; j < len(p); j++ {
		if a[i][j] == '1' && p[index] > p[j] {
			index = j
		}
	}

	if i != index {
		p[i], p[index] = p[index], p[i]
		println(p, i, index)
		return true
	}

	println(p, i, index)
	return false
}

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

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

package main

import (
	"bufio"
	"fmt"
	"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 := nextInt()
	p := make([]int, n)
	a := make([]string, n)

	for i := 0; i < n; i++ {
		p[i] = nextInt()
	}

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	checkAll(p, a)

	for i := 0; i < len(p); i++ {
		print(p[i], " ")
	}
}

func checkAll(p []int, a []string) {
	for i := 0; i < len(p); i++ {
		swapCol(i, p, a)
	}
}

func swapCol(index int, p []int, a []string) {

	for j := 0; j < index; j++ {
		if a[index][j] == '1' && p[index] < p[j] {
			p[index], p[j] = p[j], p[index]
			println(p, index, j)
			checkAll(p, a)
			break
		}
	}

	i := index

	for j := index + 1; j < len(p); j++ {
		if a[index][j] == '1' && p[i] > p[j] {
			i = j
		}
	}

	if index != i {
		p[index], p[i] = p[i], p[index]
		println(p, index, i)
		checkAll(p, a)
	}

	// println(p, index, i)
}

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

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 print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"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 := nextInt()
	p := make([]int, n)
	a := make([]string, n)

	for i := 0; i < n; i++ {
		p[i] = nextInt()
	}

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	checkAll(p, a)

	for i := 0; i < len(p); i++ {
		print(p[i], " ")
	}
}

func checkAll(p []int, a []string) {
	for i := 0; i < len(p); i++ {
		swapCol(i, p, a)
	}
}

func swapCol(index int, p []int, a []string) {

	for j := 0; j < index; j++ {
		if a[index][j] == '1' && p[index] < p[j] {
			p[index], p[j] = p[j], p[index]
			// println(p, index, j)
			checkAll(p, a)
			break
		}
	}

	i := index

	for j := index + 1; j < len(p); j++ {
		if a[index][j] == '1' && p[i] > p[j] {
			i = j
		}
	}

	if index != i {
		p[index], p[i] = p[i], p[index]
		// println(p, index, i)
		checkAll(p, a)
	}

	// println(p, index, i)
}

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

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 print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	fmt.Println(s.permutation)
	fmt.Println(s.unions)

	for i, val := range s.permutation {
		index := s.find(i)
		fmt.Println(i, val, index)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' {
				s.union(i, j)
				fmt.Println(i, j, s.unions)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

//func (s *Solver) Solve() []int {
//	numbersInUnions := make([][]int, len(s.permutation))
//	for i := range numbersInUnions {
//		numbersInUnions[i] = make([]int, 0)
//	}
//
//	fmt.Println(s.permutation)
//	fmt.Println(s.unions)
//
//	for i, val := range s.permutation {
//		index := s.find(i)
//		fmt.Println(i, val, index)
//		numbersInUnions[index] = append(numbersInUnions[index], val)
//	}
//
//	for _, arr := range numbersInUnions {
//		sort.Ints(arr)
//	}
//
//	ans := make([]int, len(s.permutation))
//	lastUsedIndex := make([]int, len(s.permutation))
//	for i := range s.permutation {
//		index := s.find(i)
//		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
//		lastUsedIndex[index] += 1
//	}
//
//	return ans
//}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	//answer := solver.Solve()
	//
	//outBuf := bufio.NewWriter(os.Stdout)
	//defer func() { outBuf.Flush() }()
	//
	//for _, val := range answer {
	//	fmt.Fprintf(outBuf, "%d ", val)
	//}
	//fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	fmt.Println(matrix)
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	fmt.Println(s.permutation)
	fmt.Println(s.unions)

	for i, val := range s.permutation {
		index := s.find(i)
		fmt.Println(i, val, index)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		format := "%d "
		if i + 1 == len(permutation) {
			format = "%d\n"
		}
		fmt.Fscanf(inBuf,format, &permutation[i])

	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	fmt.Println(s.unions)
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' {
				s.union(i, j)
				fmt.Println(s.unions)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

//func (s *Solver) Solve() []int {
//	numbersInUnions := make([][]int, len(s.permutation))
//	for i := range numbersInUnions {
//		numbersInUnions[i] = make([]int, 0)
//	}
//
//	fmt.Println(s.permutation)
//	fmt.Println(s.unions)
//
//	for i, val := range s.permutation {
//		index := s.find(i)
//		fmt.Println(i, val, index)
//		numbersInUnions[index] = append(numbersInUnions[index], val)
//	}
//
//	for _, arr := range numbersInUnions {
//		sort.Ints(arr)
//	}
//
//	ans := make([]int, len(s.permutation))
//	lastUsedIndex := make([]int, len(s.permutation))
//	for i := range s.permutation {
//		index := s.find(i)
//		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
//		lastUsedIndex[index] += 1
//	}
//
//	return ans
//}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	//answer := solver.Solve()
	//
	//outBuf := bufio.NewWriter(os.Stdout)
	//defer func() { outBuf.Flush() }()
	//
	//for _, val := range answer {
	//	fmt.Fprintf(outBuf, "%d ", val)
	//}
	//fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	fmt.Println(s.permutation)

	for i, val := range s.permutation {
		index := s.find(i)
		fmt.Println(i, val, index)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	for i, val := range s.permutation {
		index := s.find(i)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	fmt.Println(answer)

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	for i, val := range s.permutation {
		index := s.find(i)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	for i, val := range s.permutation {
		index := s.find(i)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
		fmt.Println(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	matrix []string
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.matrix = matrix
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	for i, line := range s.matrix {
		for j, val := range line {
			if i > j && val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

func (s *Solver) Solve() []int {
	numbersInUnions := make([][]int, len(s.permutation))
	for i := range numbersInUnions {
		numbersInUnions[i] = make([]int, 0)
	}

	for i, val := range s.permutation {
		index := s.find(i)
		numbersInUnions[index] = append(numbersInUnions[index], val)
	}

	for _, arr := range numbersInUnions {
		sort.Ints(arr)
	}

	ans := make([]int, len(s.permutation))
	lastUsedIndex := make([]int, len(s.permutation))
	for i := range s.permutation {
		index := s.find(i)
		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
		lastUsedIndex[index] += 1
	}

	return ans
}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	answer := solver.Solve()

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for _, val := range answer {
		fmt.Fprintf(outBuf, "%d ", val)
	}
	fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	fmt.Println(s.unions)
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' && s.find(i) != s.find(j) {
				s.union(i, j)
				fmt.Println(s.unions)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

//func (s *Solver) Solve() []int {
//	numbersInUnions := make([][]int, len(s.permutation))
//	for i := range numbersInUnions {
//		numbersInUnions[i] = make([]int, 0)
//	}
//
//	fmt.Println(s.permutation)
//	fmt.Println(s.unions)
//
//	for i, val := range s.permutation {
//		index := s.find(i)
//		fmt.Println(i, val, index)
//		numbersInUnions[index] = append(numbersInUnions[index], val)
//	}
//
//	for _, arr := range numbersInUnions {
//		sort.Ints(arr)
//	}
//
//	ans := make([]int, len(s.permutation))
//	lastUsedIndex := make([]int, len(s.permutation))
//	for i := range s.permutation {
//		index := s.find(i)
//		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
//		lastUsedIndex[index] += 1
//	}
//
//	return ans
//}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	//answer := solver.Solve()
	//
	//outBuf := bufio.NewWriter(os.Stdout)
	//defer func() { outBuf.Flush() }()
	//
	//for _, val := range answer {
	//	fmt.Fprintf(outBuf, "%d ", val)
	//}
	//fmt.Fprintf(outBuf, "\n")
}

package main

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

type Solver struct {
	permutation []int
	unions []int
}

func (s *Solver) Init(permutation []int, matrix[]string) {
	s.permutation = permutation
	s.unions = make([]int, len(s.permutation))
	for i := range s.unions {
		s.unions[i] = i
	}
	fmt.Println(matrix)
	for i, line := range matrix {
		for j, val := range line {
			if val == '1' {
				s.union(i, j)
				fmt.Println(i, j, s.unions)
			}
		}
	}
}

func (s *Solver) union(a, b int) {
	s.unions[s.find(a)] = s.find(b)
}

func (s *Solver) find(a int) int {
	ans := s.unions[a]
	for ans != s.unions[ans] {
		ans = s.unions[ans]
	}
	return ans
}

//func (s *Solver) Solve() []int {
//	numbersInUnions := make([][]int, len(s.permutation))
//	for i := range numbersInUnions {
//		numbersInUnions[i] = make([]int, 0)
//	}
//
//	fmt.Println(s.permutation)
//	fmt.Println(s.unions)
//
//	for i, val := range s.permutation {
//		index := s.find(i)
//		fmt.Println(i, val, index)
//		numbersInUnions[index] = append(numbersInUnions[index], val)
//	}
//
//	for _, arr := range numbersInUnions {
//		sort.Ints(arr)
//	}
//
//	ans := make([]int, len(s.permutation))
//	lastUsedIndex := make([]int, len(s.permutation))
//	for i := range s.permutation {
//		index := s.find(i)
//		ans[i] = numbersInUnions[index][lastUsedIndex[index]]
//		lastUsedIndex[index] += 1
//	}
//
//	return ans
//}

func main()  {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	permutation := make([]int, n)
	for i := range permutation {
		fmt.Fscanf(inBuf,"%d ", &permutation[i])
	}

	matrix := make([]string, n)
	for i := range matrix {
		fmt.Fscanf(inBuf, "%s\n", &matrix[i])
	}

	solver := Solver{}
	solver.Init(permutation, matrix)
	//answer := solver.Solve()
	//
	//outBuf := bufio.NewWriter(os.Stdout)
	//defer func() { outBuf.Flush() }()
	//
	//for _, val := range answer {
	//	fmt.Fprintf(outBuf, "%d ", val)
	//}
	//fmt.Fprintf(outBuf, "\n")
}

package main

import (
	"fmt"
)

var (
	n int
	p []int
	A [][]bool
)

func in() {
	fmt.Scanln(&n)
	p = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &p[i])
	}
	A = make([][]bool, n)
	for i := 0; i < n; i++ {
		A[i] = make([]bool, n)
		var line string
		fmt.Scanln(&line)
		for j, ch := range line {
			A[i][j] = ch == '1'
		}
	}
}

func out() {
	fmt.Print(p[0])
	for i := 1; i < n; i++ {
		fmt.Print(" ", p[i])
	}
	fmt.Println()
}

func run() {
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if A[i][k] && A[j][k] && !A[i][j] {
					A[i][j] = true
				}
			}
		}
	}

	for i := 0; i < n-1; i++ {
		var min_j int = 0
		min := p[i]
		for j := i + 1; j < n; j++ {
			if A[i][j] && p[j] < min {
				min = p[j]
				min_j = j
			}
		}
		if min_j > 0 {
			p[i], p[min_j] = p[min_j], p[i]
		}
	}
}

func main() {
	in()
	run()
	out()
}

package main

import (
	"fmt"
)

var (
	n int
	p []int
	A [][]bool
)

func in() {
	fmt.Scan(&n)
	p = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
	}
	A = make([][]bool, n)
	for i := 0; i < n; i++ {
		A[i] = make([]bool, n)
		var line string
		fmt.Scan(&line)
		for j, ch := range line {
			A[i][j] = ch == '1'
		}
	}
	fmt.Println(n, p, A)
}

func out() {
	fmt.Print(p[0])
	for i := 1; i < n; i++ {
		fmt.Print(" ", p[i])
	}
	fmt.Println()
}

func run() {
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if A[i][k] && A[j][k] && !A[i][j] {
					A[i][j] = true
				}
			}
		}
	}

	for i := 0; i < n-1; i++ {
		var min_j int = 0
		min := p[i]
		for j := i + 1; j < n; j++ {
			if A[i][j] && p[j] < min {
				min = p[j]
				min_j = j
			}
		}
		if min_j > 0 {
			p[i], p[min_j] = p[min_j], p[i]
		}
	}
}

func main() {
	in()
	run()
	out()
}

package main

import (
	"fmt"
)

var (
	n int
	p []int
	A [][]bool
)

func in() {
	fmt.Scanln(&n)
	p = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &p[i])
	}
	A = make([][]bool, n)
	for i := 0; i < n; i++ {
		A[i] = make([]bool, n)
		var line string
		fmt.Scanln(&line)
		for j, ch := range line {
			A[i][j] = ch == '1'
		}
	}
	fmt.Println(n, p, A)
}

func out() {
	fmt.Print(p[0])
	for i := 1; i < n; i++ {
		fmt.Print(" ", p[i])
	}
	fmt.Println()
}

func run() {
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if A[i][k] && A[j][k] && !A[i][j] {
					A[i][j] = true
				}
			}
		}
	}

	for i := 0; i < n-1; i++ {
		var min_j int = 0
		min := p[i]
		for j := i + 1; j < n; j++ {
			if A[i][j] && p[j] < min {
				min = p[j]
				min_j = j
			}
		}
		if min_j > 0 {
			p[i], p[min_j] = p[min_j], p[i]
		}
	}
}

func main() {
	in()
	run()
	out()
}


package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) *Node {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
	return x
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	return t.EraseNode(t.Find(key))
}

func (t *Treap) EraseNode(x *Node) bool {
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (t *Treap) Tail() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.r != nil {
		x = x.r
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func (x *Node) Prev() *Node {
	if x.l != nil {
		x = x.l
		for x.r != nil {
			x = x.r
		}
		return x
	} else {
		for x.p != nil && x.p.l == x {
			x = x.p
		}
		return x.p
	}
}

type Tree struct {
	n int
	lg int
	rn int
	ns []Tnode
}

type Tnode struct {
	dep int
	pnt []*Tnode
	rnk int
	adj []*Tnode
}

func NewTree(n int) *Tree {
	lg := 1
	for d := 1; d < n; d <<= 1 {
		lg++
	}
	t := &Tree{
		n: n,
		lg: lg,
		ns: make([]Tnode, n),
	}
	for i := range t.ns {
		x := &t.ns[i]
		x.dep = -1
		x.pnt = make([]*Tnode, lg)
	}
	t.rn = 0
	return t
}

func (t *Tree) Link(u, v int) {
	x := &t.ns[u]
	y := &t.ns[v]
	x.adj = append(x.adj, y)
	y.adj = append(y.adj, x)
}

func (t *Tree) Build() {
	t.build(&t.ns[0], &t.ns[0], 0)
	for d := 1; d < t.lg; d++ {
		for i := range t.ns {
			x := &t.ns[i]
			x.pnt[d] = x.pnt[d-1].pnt[d-1]
		}
	}
}

func (t *Tree) build(u, p *Tnode, d int) {
	u.pnt[0] = p
	u.dep = d
	u.rnk = t.rn
	t.rn++
	for _, v := range u.adj {
		if v.dep == -1 {
			t.build(v, u, d+1)
		}
	}
}

func (t *Tree) Lca(u, v int) int {
	x := &t.ns[u]
	y := &t.ns[v]
	z := t.lca(x, y)
	return int((uintptr(unsafe.Pointer(z)) - uintptr(unsafe.Pointer(x))) / unsafe.Alignof(t.ns[0]))
}

func (t *Tree) lca(x, y *Tnode) *Tnode {
	if x.dep < y.dep {
		x, y = y, x
	}
	d := x.dep - y.dep
	for i := 0; i < t.lg; i++ {
		if d&(1<<uint(i)) > 0 {
			x = x.pnt[i]
		}
	}
	for i := t.lg - 1; i >= 0; i-- {
		x1 := x.pnt[i]
		y1 := y.pnt[i]
		if x1 != y1 {
			x, y = x1, y1
		}
	}
	if x != y {
		x = x.pnt[0]
	}
	return x
}

func (t *Tree) Dist(u, v int) int {
	x := &t.ns[u]
	y := &t.ns[v]
	z := t.lca(x, y)
	return x.dep + y.dep - 2*z.dep
}

func (t *Tree) Rank(u int) int {
	x := &t.ns[u]
	return x.rnk
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(reader, "%d %d\n", &n, &k)
	k = 2*k - 2
	tree := NewTree(n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		tree.Link(u-1, v-1)
	}
	tree.Build()

	ans := 1
	update := func(c int) {
		if ans < c {
			ans = c
		}
	}

	t := New()
	next := func(x *Node) *Node {
		if y := x.Next(); y != nil {
			return y
		}
		return t.Head()
	}
	prev := func(x *Node) *Node {
		if y := x.Prev(); y != nil {
			return y
		}
		return t.Tail()
	}

	ss := 0
	for i, j := 0, 0; j < n; {
		for ; j < n && ss <= k; j++ {
			update(j - i)
//			fmt.Println(i, j, ss)
//			fmt.Println("insert", j)
			x := t.Insert(tree.Rank(j), j)
			l := prev(x)
			r := next(x)
			ss -= tree.Dist(l.Val, r.Val)
			ss += tree.Dist(l.Val, x.Val)
			ss += tree.Dist(x.Val, r.Val)
//			fmt.Println("after", i, j+1, ss)
		}
		if ss <= k {
			update(j - i)
		}
		for ; i < j && ss > k; i++ {
//			fmt.Println("remove", i)
			x := t.Find(tree.Rank(i))
			l := prev(x)
			r := next(x)
			ss += tree.Dist(l.Val, r.Val)
			ss -= tree.Dist(l.Val, x.Val)
			ss -= tree.Dist(x.Val, r.Val)
			t.EraseNode(x)
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) *Node {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
	return x
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	return t.EraseNode(t.Find(key))
}

func (t *Treap) EraseNode(x *Node) bool {
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (t *Treap) Tail() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.r != nil {
		x = x.r
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func (x *Node) Prev() *Node {
	if x.l != nil {
		x = x.l
		for x.r != nil {
			x = x.r
		}
		return x
	} else {
		for x.p != nil && x.p.l == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(reader, "%d %d\n", &n, &k)
	k = 2*k - 2
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}

	lg := 1
	for d := 1; d < n; d <<= 1 {
		lg++
	}
	pnt := make([][]int, n)
	dep := make([]int, n)
	rnk := make([]int, n)
	for i := 0; i < n; i++ {
		dep[i] = -1
		pnt[i] = make([]int, lg)
	}
	rn := 0
	var dfs func(u, p, d int)
	dfs = func(u, p, d int) {
		pnt[u][0] = p
		dep[u] = d
		rnk[u] = rn
		rn++
		for _, v := range vout[u] {
			if dep[v] == -1 {
				dfs(v, u, d+1)
			}
		}
	}
	dfs(0, 0, 0)

	for d := 1; d < lg; d++ {
		for i := 0; i < n; i++ {
			pnt[i][d] = pnt[pnt[i][d-1]][d-1]
		}
	}
//	fmt.Println(dep)
//	fmt.Println(pnt)
//	fmt.Println(rnk)

	lca := func(x, y int) int {
		if dep[x] < dep[y] {
			x, y = y, x
		}
		d := dep[x] - dep[y]
		for i := 0; i < lg; i++ {
			if d&(1<<uint(i)) > 0 {
				x = pnt[x][i]
			}
		}
		for i := lg - 1; i >= 0; i-- {
			x1 := pnt[x][i]
			y1 := pnt[y][i]
			if x1 != y1 {
				x, y = x1, y1
			}
		}
		if x != y {
			x = pnt[x][0]
		}
		return x
	}
	dist := func(x, y int) int {
		z := lca(x, y)
//		fmt.Printf("dist %d, %d; lca = %d, d = %d.\n", x, y, z, dep[x]+dep[y]-2*dep[z])
		return dep[x] + dep[y] - 2*dep[z]
	}

	ans := 1
	update := func(c int) {
		if ans < c {
			ans = c
		}
	}

	t := New()
	next := func(x *Node) *Node {
		if y := x.Next(); y != nil {
			return y
		}
		return t.Head()
	}
	prev := func(x *Node) *Node {
		if y := x.Prev(); y != nil {
			return y
		}
		return t.Tail()
	}

	ss := 0
	for i, j := 0, 0; j < n; {
		for ; j < n && ss <= k; j++ {
			update(j - i)
//			fmt.Println(i, j, ss)
//			fmt.Println("insert", j)
			x := t.Insert(rnk[j], j)
			l := prev(x)
			r := next(x)
			ss -= dist(l.Val, r.Val)
			ss += dist(l.Val, x.Val)
			ss += dist(x.Val, r.Val)
//			fmt.Println("after", i, j+1, ss)
		}
		if ss <= k {
			update(j - i)
		}
		for ; i < j && ss > k; i++ {
//			fmt.Println("remove", i)
			x := t.Find(rnk[i])
			l := prev(x)
			r := next(x)
			ss += dist(l.Val, r.Val)
			ss -= dist(l.Val, x.Val)
			ss -= dist(x.Val, r.Val)
			t.EraseNode(x)
		}
	}
	fmt.Println(ans)
}


package main

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

const mod = 1e9 + 7

type seg []struct {
	l, r         int
	mul, mulTodo int64
	or, orTodo   int64
}

func pow(x int64, n int) int64 {
	res := int64(1)
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t seg) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	t[o].mul = lo.mul * ro.mul % mod
	t[o].or = lo.or | ro.or
}

func (t seg) _build(a []int, ors []int64, o, l, r int) {
	t[o].l, t[o].r, t[o].mulTodo = l, r, 1
	if l == r {
		t[o].mul = int64(a[l-1])
		t[o].or = ors[l-1]
		return
	}
	m := (l + r) >> 1
	t._build(a, ors, o<<1, l, m)
	t._build(a, ors, o<<1|1, m+1, r)
	t._pushUp(o)
}

func (t seg) _spread(o int) {
	lo, ro := &t[o<<1], &t[o<<1|1]
	if mul := t[o].mulTodo; mul != 1 {
		lo.mul = lo.mul * pow(mul, lo.r-lo.l+1) % mod
		ro.mul = ro.mul * pow(mul, ro.r-ro.l+1) % mod
		lo.mulTodo = lo.mulTodo * mul % mod
		ro.mulTodo = ro.mulTodo * mul % mod
		t[o].mulTodo = 1
	}
	if or := t[o].orTodo; or != 0 {
		lo.or |= or
		ro.or |= or
		lo.orTodo |= or
		ro.orTodo |= or
		t[o].orTodo = 0
	}
}

func (t seg) _update(o, l, r, mul int, orVal int64) {
	ol, or := t[o].l, t[o].r
	if l <= ol && or <= r {
		t[o].mul = t[o].mul * pow(int64(mul), or-ol+1) % mod
		t[o].mulTodo = t[o].mulTodo * int64(mul) % mod
		t[o].or |= orVal
		t[o].orTodo |= orVal
		return
	}
	t._spread(o)
	m := (ol + or) >> 1
	if l <= m {
		t._update(o<<1, l, r, mul, orVal)
	}
	if m < r {
		t._update(o<<1|1, l, r, mul, orVal)
	}
	t._pushUp(o)
}

func (t seg) _query(o, l, r int) (mul, or int64) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].mul, t[o].or
	}
	t._spread(o)
	mul = 1
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		mul, or = t._query(o<<1, l, r)
	}
	if m < r {
		a, b := t._query(o<<1|1, l, r)
		mul = mul * a % mod
		or |= b
	}
	return
}

func (t seg) init(a []int, ors []int64)      { t._build(a, ors, 1, 1, len(a)) }
func (t seg) update(l, r, mul int, or int64) { t._update(1, l, r, mul, or) }
func (t seg) query(l, r int) (int64, int64)  { return t._query(1, l, r) }

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	const mx = 300
	factors := [mx + 1]int64{}
	p1p := []int64{}
	for i := 2; i <= mx; i++ {
		if factors[i] == 0 {
			for j := i; j <= mx; j += i {
				factors[j] |= 1 << len(p1p)
			}
			p1p = append(p1p, int64(i-1)*pow(int64(i), mod-2)%mod)
		}
	}

	var n, q, l, r, x int
	var s []byte
	Fscan(in, &n, &q)
	a := make([]int, n)
	ors := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		ors[i] = factors[a[i]]
	}

	t := make(seg, 4*n)
	t.init(a, ors)
	for ; q > 0; q-- {
		Fscan(in, &s, &l, &r)
		if s[0] == 'M' {
			Fscan(in, &x)
			t.update(l, r, x, factors[x])
		} else {
			ans, ps := t.query(l, r)
			for i, v := range p1p {
				if ps>>i&1 == 1 {
					ans = ans * v % mod
				}
			}
			Fprintln(out, ans)
		}
	}
}

package main

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

const mod int64 = 1e9 + 7

type lazyST []struct {
	l, r         int
	mul, mulTodo int64
	or, orTodo   int64
}

func (lazyST) pow(x int64, n int) int64 {
	res := int64(1)
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t lazyST) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	t[o].mul = lo.mul * ro.mul % mod
	t[o].or = lo.or | ro.or
}

func (t lazyST) _build(a []int, ors []int64, o, l, r int) {
	t[o].l, t[o].r, t[o].mulTodo = l, r, 1
	if l == r {
		t[o].mul = int64(a[l-1])
		t[o].or = ors[l-1]
		return
	}
	m := (l + r) >> 1
	t._build(a, ors, o<<1, l, m)
	t._build(a, ors, o<<1|1, m+1, r)
	t._pushUp(o)
}

func (t lazyST) _spread(o int) {
	lo, ro := &t[o<<1], &t[o<<1|1]
	if mul := t[o].mulTodo; mul != 1 {
		lo.mul = lo.mul * t.pow(mul, lo.r-lo.l+1) % mod
		ro.mul = ro.mul * t.pow(mul, ro.r-ro.l+1) % mod
		lo.mulTodo = lo.mulTodo * mul % mod
		ro.mulTodo = ro.mulTodo * mul % mod
		t[o].mulTodo = 1
	}
	if or := t[o].orTodo; or != 0 {
		lo.or |= or
		ro.or |= or
		lo.orTodo |= or
		ro.orTodo |= or
		t[o].orTodo = 0
	}
}

func (t lazyST) _update(o, l, r, mul int, orVal int64) {
	ol, or := t[o].l, t[o].r
	if l <= ol && or <= r {
		t[o].mul = t[o].mul * t.pow(int64(mul), or-ol+1) % mod
		t[o].mulTodo = t[o].mulTodo * int64(mul) % mod
		t[o].or |= orVal
		t[o].orTodo |= orVal
		return
	}
	t._spread(o)
	m := (ol + or) >> 1
	if l <= m {
		t._update(o<<1, l, r, mul, orVal)
	}
	if m < r {
		t._update(o<<1|1, l, r, mul, orVal)
	}
	t._pushUp(o)
}

func (t lazyST) _query(o, l, r int) (mul, or int64) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].mul, t[o].or
	}
	t._spread(o)
	mul = 1
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		mul, or = t._query(o<<1, l, r)
	}
	if m < r {
		a, b := t._query(o<<1|1, l, r)
		mul = mul * a % mod
		or |= b
	}
	return
}

func (t lazyST) init(a []int, ors []int64)      { t._build(a, ors, 1, 1, len(a)) }
func (t lazyST) update(l, r, mul int, or int64) { t._update(1, l, r, mul, or) }
func (t lazyST) query(l, r int) (int64, int64)  { return t._query(1, l, r) }

// github.com/EndlessCheng/codeforces-go
func CF1114F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	pow := func(x, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(p int64) int64 { return (p - 1) * pow(p, mod-2) % mod }
	const mx = 300
	factors := [mx + 1]int64{}
	p1p := []int64{}
	for i := 2; i <= mx; i++ {
		if factors[i] == 0 {
			for j := i; j <= mx; j += i {
				factors[j] |= 1 << len(p1p)
			}
			p1p = append(p1p, div(int64(i)))
		}
	}

	var n, q, l, r, x int
	var s []byte
	Fscan(in, &n, &q)
	a := make([]int, n)
	ors := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		ors[i] = factors[a[i]]
	}

	t := make(lazyST, 4*n)
	t.init(a, ors)
	for ; q > 0; q-- {
		Fscan(in, &s, &l, &r)
		if s[0] == 'M' {
			Fscan(in, &x)
			t.update(l, r, x, factors[x])
		} else {
			ans, ps := t.query(l, r)
			for i, v := range p1p {
				if ps>>i&1 == 1 {
					ans = ans * v % mod
				}
			}
			Fprintln(out, ans)
		}
	}
}

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

package main

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

const mod int64 = 1e9 + 7

type mulST []struct {
	l, r      int
	mul, todo int64
}

func (mulST) pow(x int64, n int) int64 {
	res := int64(1)
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			res = res * x % mod
		}
		x = x * x % mod
	}
	return res
}

func (t mulST) _pushUp(o int) {
	t[o].mul = t[o<<1].mul * t[o<<1|1].mul % mod
}

func (t mulST) _build(a []int, o, l, r int) {
	t[o].l, t[o].r, t[o].todo = l, r, 1
	if l == r {
		t[o].mul = int64(a[l-1])
		return
	}
	m := (l + r) >> 1
	t._build(a, o<<1, l, m)
	t._build(a, o<<1|1, m+1, r)
	t._pushUp(o)
}

func (t mulST) _spread(o int) {
	if mul := t[o].todo; mul != 1 {
		lo, ro := &t[o<<1], &t[o<<1|1]
		lo.mul = lo.mul * t.pow(mul, lo.r-lo.l+1) % mod
		ro.mul = ro.mul * t.pow(mul, ro.r-ro.l+1) % mod
		lo.todo = lo.todo * mul % mod
		ro.todo = ro.todo * mul % mod
		t[o].todo = 1
	}
}

func (t mulST) _update(o, l, r, val int) {
	ol, or := t[o].l, t[o].r
	if l <= ol && or <= r {
		t[o].mul = t[o].mul * t.pow(int64(val), or-ol+1) % mod
		t[o].todo = t[o].todo * int64(val) % mod
		return
	}
	t._spread(o)
	m := (ol + or) >> 1
	if l <= m {
		t._update(o<<1, l, r, val)
	}
	if m < r {
		t._update(o<<1|1, l, r, val)
	}
	t._pushUp(o)
}

func (t mulST) _query(o, l, r int) (res int64) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].mul
	}
	t._spread(o)
	res = 1
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		res = t._query(o<<1, l, r)
	}
	if m < r {
		res = res * t._query(o<<1|1, l, r) % mod
	}
	return
}

func (t mulST) init(a []int)         { t._build(a, 1, 1, len(a)) }
func (t mulST) update(l, r, val int) { t._update(1, l, r, val) }
func (t mulST) query(l, r int) int64 { return t._query(1, l, r) }

type orST []struct {
	l, r     int
	or, todo int64
}

func (t orST) _pushUp(o int) {
	t[o].or = t[o<<1].or | t[o<<1|1].or
}

func (t orST) _build(a []int64, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].or = a[l-1]
		return
	}
	m := (l + r) >> 1
	t._build(a, o<<1, l, m)
	t._build(a, o<<1|1, m+1, r)
	t._pushUp(o)
}

func (t orST) _spread(o int) {
	if or := t[o].todo; or != 0 {
		lo, ro := &t[o<<1], &t[o<<1|1]
		lo.or |= or
		ro.or |= or
		lo.todo |= or
		ro.todo |= or
		t[o].todo = 0
	}
}

func (t orST) _update(o, l, r int, val int64) {
	ol, or := t[o].l, t[o].r
	if l <= ol && or <= r {
		t[o].or |= val
		t[o].todo |= val
		return
	}
	t._spread(o)
	m := (ol + or) >> 1
	if l <= m {
		t._update(o<<1, l, r, val)
	}
	if m < r {
		t._update(o<<1|1, l, r, val)
	}
	t._pushUp(o)
}

func (t orST) _query(o, l, r int) (res int64) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].or
	}
	t._spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		res = t._query(o<<1, l, r)
	}
	if m < r {
		res |= t._query(o<<1|1, l, r)
	}
	return
}

func (t orST) init(a []int64)             { t._build(a, 1, 1, len(a)) }
func (t orST) update(l, r int, val int64) { t._update(1, l, r, val) }
func (t orST) query(l, r int) int64       { return t._query(1, l, r) }

// github.com/EndlessCheng/codeforces-go
func CF1114F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	pow := func(x, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(p int64) int64 { return (p - 1) * pow(p, mod-2) % mod }

	const mx = 300
	factors := [mx + 1]int64{}
	p1p := []int64{}
	for i := 2; i <= mx; i++ {
		if factors[i] == 0 {
			for j := i; j <= mx; j += i {
				factors[j] |= 1 << len(p1p)
			}
			p1p = append(p1p, div(int64(i)))
		}
	}

	var n, q, l, r, x int
	var s []byte
	Fscan(in, &n, &q)
	a := make([]int, n)
	ors := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		ors[i] = factors[a[i]]
	}

	mt := make(mulST, 4*n)
	mt.init(a)
	ot := make(orST, 4*n)
	ot.init(ors)
	for ; q > 0; q-- {
		Fscan(in, &s, &l, &r)
		if s[0] == 'M' {
			Fscan(in, &x)
			mt.update(l, r, x)
			ot.update(l, r, factors[x])
		} else {
			ans := mt.query(l, r)
			ps := ot.query(l, r)
			for i, v := range p1p {
				if ps>>i&1 == 1 {
					ans = ans * v % mod
				}
			}
			Fprintln(out, ans)
		}
	}
}

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


package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var out *bufio.Writer = bufio.NewWriter(os.Stdout)
var mod int64 = int64(1e9 + 7)

func find(parent *[]int, x int) int {
	if (*parent)[x] == x {
		return x
	}
	(*parent)[x] = find(parent, (*parent)[x])
	return (*parent)[x]
}

func merge(parent *[]int, x, y int) {
	parx := find(parent, x)
	pary := find(parent, y)
	if parx != pary {
		(*parent)[pary] = parx
	}
}

func dsu(g *[][]int, parent *[]int, x int, vis *[]bool) {
	for _, i := range (*g)[x] {
		if !(*vis)[i] {
			(*vis)[i] = true
			merge(parent, x, i)
			dsu(g, parent, i, vis)
		}
	}
}
func main() {
	defer out.Flush()
	solve := func(Case int) {
		var n int
		fmt.Fscan(in, &n)
		a, b := make([]int, n), make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
			a[i]--
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
			b[i]--
		}
		g := make([][]int, n)
		for i := range a {
			g[a[i]] = append(g[a[i]], b[i])
			g[b[i]] = append(g[b[i]], a[i])
		}
		vis := make([]bool, n)
		parent := make([]int, n)
		for i := range a {
			parent[i] = i
		}
		for i := range a {
			if !vis[i] {
				vis[i] = true
				dsu(&g, &parent, i, &vis)
			}
		}
		ans := int64(0)
		for i := range a {
			if find(&parent, i) == i {
				ans++
			}
		}
		fmt.Fprintln(out, powMod(int64(2), ans, mod))

	}

	T := 1
	fmt.Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}
}

func powMod(x, y, mod int64) int64 {
	ans := int64(1)
	for y != 0 {
		if y&1 != 0 {
			ans = (ans * x) % mod
		}
		y = y >> 1
		x = (x * x) % mod
	}
	return ans
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func min(a []int) int {
	mn := int(1e9)
	for i := range a {
		if a[i] < mn {
			mn = a[i]
		}
	}
	return mn
}
func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func reverse(s []byte) []byte {
	a := []byte(s)
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return a
}

package main

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

type CF1534C struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1534C) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1534C) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1534C) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1534C) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1534C) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
 Run solve the problem CF1534C
 Date: 13/06/21
 User: pepradere
 URL: https://codeforces.com/contest/1534/problem/C
 Problem: CF1534C
**/
func (in *CF1534C) Run() {

	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		a := []int{}
		b := []int{}

		for i := 0; i < n; i++ {
			a = append(a, in.NextInt()-1)
		}

		for i := 0; i < n; i++ {
			b = append(b, in.NextInt()-1)
		}

		mady := []map[int]bool{}
		for i := 0; i < n; i++ {
			mady = append(mady, map[int]bool{})
		}

		for i := 0; i < n; i++ {
			mady[a[i]][b[i]] = true
			mady[b[i]][a[i]] = true
		}

		cola := []int{}
		vis := []bool{}
		for i := 0; i <= n; i++ {
			cola = append(cola, 0)
			vis = append(vis, false)
		}

		count := 0
		for i := 0; i < n; i++ {
			if !vis[i] {
				u := i
				vis[u] = true
				p, c := 0, 0
				cola[c] = u
				c++
				for ; p < c; {
					u = cola[p]
					p++
					for v, _ := range mady[u] {
						if !vis[v] {
							vis[v] = true
							cola[c] = v
							c++
						}

					}
				}
				count++
			}
		}

		const S = 1000000007
		ans := 1
		for i := 0; i < count; i++ {
			ans = ((ans%S) * 2) %S
		}

		//ans := int64(math.Pow(2.0, float64(count))) % 1000000007
		fmt.Println(ans)

	}
}

func NewCF1534C(r *bufio.Reader) *CF1534C {
	return &CF1534C{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1534C(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func modshift(p, m int) int {
	res := 1
	for i := 0; i < p; i++ {
		res = res << 1
		res = res % m
	}
	return res
}

func dfs(n int, p []int, v []bool) {
	t := n
	v[t] = true
	for p[t] != n {
		t = p[t]
		v[t] = true
	}
}

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

	mod := int(1e9 + 7)
	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var n, cnt int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		b := make([]int, n)
		p := make([]int, n+1)
		vis := make([]bool, n+1)

		for i := range a { fmt.Fscan(r, &a[i]) }
		for i := range b { fmt.Fscan(r, &b[i]) }
		for i := range a { p[a[i]] = b[i] }

		for _, v := range a {
			if !vis[v] {
				cnt++
				dfs(v, p, vis)
			}
		}

		fmt.Fprintln(w, modshift(cnt, mod))
	}

}

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pos := make([]int, n+1)
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			pos[v] = i
		}
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := 1
		vis := make([]bool, n)
		for i, s := range vis {
			if !s {
				ans = ans * 2 % (1e9 + 7)
				for ; !vis[i]; i = pos[b[i]] {
					vis[i] = true
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(r, &t)
	for k := 0; k < t; k++ {
		var n int
		fmt.Fscan(r, &n)
		arr := make([][]int, 2)
		arr[0] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &arr[0][j])
		}

		arr[1] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &arr[1][j])
		}
		solve1534C(arr)
	}
}
func solve1534C(arr [][]int) {
	n := len(arr[0])
	parent := make([]int, n)
	tmp := make([]int, n+1) //记录连通性的数组
	for i := 0; i < n; i++ {
		parent[i] = i
	}
	for i := 0; i < n; i++ {
		tmp[arr[0][i]] = i
	}
	for i := 0; i < n; i++ {
		pos := tmp[arr[1][i]]
		union1534C(pos, i, parent)
	}
	//计算集合的数量
	var setNums int
	for i := 0; i < n; i++ {
		if parent[i] == i {
			setNums++
		}
	}
	var mod int = 1e9 + 7
	var res int = 1
	for i := 0; i < setNums; i++ {
		res = ((res % mod) * 2) % mod
	}
	fmt.Println(res)
}
func find1534C(x int, parent []int) int {
	for x != parent[x] {
		parent[x] = parent[parent[x]]
		x = parent[x]
	}
	return x
}
func union1534C(x, y int, parent []int) {
	a := find1534C(x, parent)
	b := find1534C(y, parent)
	if a == b {
		return
	}
	parent[b] = a
}

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var out *bufio.Writer = bufio.NewWriter(os.Stdout)
var mod int = int(1e9 + 7)

func find(parent *[]int, x int) int {
	if (*parent)[x] == x {
		return x
	}
	(*parent)[x] = find(parent, (*parent)[x])
	return (*parent)[x]
}

func merge(parent *[]int, x, y int) {
	parx := find(parent, x)
	pary := find(parent, y)
	if parx != pary {
		(*parent)[pary] = parx
	}
}

func dsu(g *[][]int, parent *[]int, x int, vis *[]bool) {
	for _, i := range (*g)[x] {
		if !(*vis)[i] {
			(*vis)[i] = true
			merge(parent, x, i)
			dsu(g, parent, i, vis)
		}
	}
}
func main() {
	defer out.Flush()
	solve := func(Case int) {
		var n int
		fmt.Fscan(in, &n)
		a, b := make([]int, n), make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
			a[i]--
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
			b[i]--
		}
		g := make([][]int, n)
		for i := range a {
			g[a[i]] = append(g[a[i]], b[i])
			g[b[i]] = append(g[b[i]], a[i])
		}
		vis := make([]bool, n)
		parent := make([]int, n)
		for i := range a {
			parent[i] = i
		}
		for i := range a {
			if !vis[i] {
				vis[i] = true
				dsu(&g, &parent, i, &vis)
			}
		}
		ans := 0
		for i := range a {
			if find(&parent, i) == i {
				ans++
			}
		}
		fmt.Fprintln(out, powMod(2, ans, mod))

	}

	T := 1
	fmt.Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}
}

func powMod(x, y, mod int) int {
	ans := int(1)
	for y != 0 {
		if y&1 != 0 {
			ans = (ans * x) % mod
		}
		y = y >> 1
		x = (x * x) % mod
	}
	return ans
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func min(a []int) int {
	mn := int(1e9)
	for i := range a {
		if a[i] < mn {
			mn = a[i]
		}
	}
	return mn
}
func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func reverse(s []byte) []byte {
	a := []byte(s)
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return a
}

package main

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

type CF1534C struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1534C) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1534C) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1534C) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1534C) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1534C) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
 Run solve the problem CF1534C
 Date: 13/06/21
 User: pepradere
 URL: https://codeforces.com/contest/1534/problem/C
 Problem: CF1534C
**/
func (in *CF1534C) Run() {

	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		a := []int{}
		b := []int{}

		for i := 0; i < n; i++ {
			a = append(a, in.NextInt()-1)
		}

		for i := 0; i < n; i++ {
			b = append(b, in.NextInt()-1)
		}

		mady := []map[int]bool{}
		for i := 0; i < n; i++ {
			mady = append(mady, map[int]bool{})
		}

		for i := 0; i < n; i++ {
			mady[a[i]][b[i]] = true
			mady[b[i]][a[i]] = true
		}

		cola := []int{}
		vis := []bool{}
		for i := 0; i <= n; i++ {
			cola = append(cola, 0)
			vis = append(vis, false)
		}

		count := 0
		for i := 0; i < n; i++ {
			if !vis[i] {
				u := i
				vis[u] = true
				p, c := 0, 0
				cola[c] = u
				c++
				for ;p < c;{
					u = cola[p]
					p++
					for v , _ :=range mady[u] {
						if !vis[v]{
							vis[v] = true
							cola[c] = v
							c++
						}

					}
				}
				count++
			}
		}

		ans := int64(math.Pow(2.0, float64(count)))%1000000007
		fmt.Println(ans)

	}
}

func NewCF1534C(r *bufio.Reader) *CF1534C {
	return &CF1534C{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1534C(bufio.NewReader(os.Stdin)).Run()
}


package main

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

/*
type point struct {
	i int
	j int
}
*/

func is_corner(x, y, n int) bool {
	if x == 1 && y == 1 { return true }
	if x == 1 && y == n { return true }
	if x == n && y == 1 { return true }
	if x == n && y == n { return true }
	return false
}

func check_corner_reach(xc, yc, n, x, y int) bool {
	if (xc == x) || (yc == y) { return true }
	return false
}

func check_normal_reach(x, y int) bool {
	if x == 1 && y == 1 { return false }
	return true
}

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var l1, l2, l3, l4 int
		var r1, r2, r3, r4 int
		var lc, rc int
		var n int

		fmt.Fscan(r, &n, &l1, &r1, &l2, &r2, &l3, &r3, &l4, &r4)

		// fmt.Fprintln(w, "&&&& ", n, l1, r1, l2, r2, l3, r3, l4, r4) // ***

		if l1 == l2 || l1 == l3 {
			lc = l1
		} else if l2 == l1 || l2 == l3 {
			lc = l2
		} else {
			lc = l3
		}

		if r1 == r2 || r1 == r3 {
			rc = r1
		} else if r2 == r1 || r2 == r3 {
			rc = r2
		} else {
			rc = r3
		}

		// fmt.Fprintln(w, "%%%% ", lc, rc)

		can_reach := true
		if is_corner(lc, rc, n){
			// fmt.Fprintln(w, " &&&& Corner")
			can_reach = check_corner_reach(lc, rc, n, l4, r4)
		} else {
			l4 = abs(lc-l4)%2
			r4 = abs(rc-r4)%2
			can_reach = check_normal_reach(l4, r4)
		}

		if !can_reach {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
		}

	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=0; tt<T; tt++ {
		var n int
		scanf("%d\n", &n)
		p := make([][]int, 3)
		for i:=0; i<3; i++ {
			p[i] = make([]int, 2)
			for j:=0; j<2; j++ {
				scanf("%d ", &p[i][j])
			}
		}

		var x, y int
		scanf("\n%d %d\n", &x, &y)

		middle := -1
		isMiddle := func (m, o1, o2 int) bool {
			return (p[m][0] == p[o1][0] && p[m][1] == p[o2][1]) || (p[m][0] == p[o2][0] && p[m][1] == p[o1][1])
		}
		if isMiddle(0, 1, 2) {
			middle = 0
		} else if isMiddle(1, 0, 2) {
			middle = 1
		} else {
			middle = 2
		}

		isCorner := func (r, c int) bool {
			if (r == 1 && c == 1) || (r == 1 && c == n) || (r == n && c == 1) || (r == n && c == n) {
				return true
			}
			return false
		}

		if isCorner(p[middle][0], p[middle][1]) {
			if p[middle][0] == x || p[middle][1] == y {
				printf("%s\n", "YES")
				continue
			}
			printf("%s\n", "NO")
			continue
		}

		isYes := func(r, c int) bool {
			if (int(math.Abs(float64(x - r))) & 1) == 0 && (int(math.Abs(float64(y - c))) & 1) == 0 {
				return true
			}
			return false
		}
		if isYes(p[0][0], p[0][1]) || isYes(p[1][0], p[1][1]) || isYes(p[2][0], p[2][1]) {
			printf("%s\n", "YES")
		} else {
			printf("%s\n", "NO")
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var T int
	fmt.Scan(&T)
	for tt:=0; tt<T; tt++ {
		var n int
		fmt.Scan(&n)
		p := make([][]int, 3)
		for i:=0; i<3; i++ {
			p[i] = make([]int, 2)
			for j:=0; j<2; j++ {
				fmt.Scan(&p[i][j])
			}
		}

		var x, y int
		fmt.Scan(&x, &y)

		middle := -1
		isMiddle := func (m, o1, o2 int) bool {
			return (p[m][0] == p[o1][0] && p[m][1] == p[o2][1]) || (p[m][0] == p[o2][0] && p[m][1] == p[o1][1])
		}
		if isMiddle(0, 1, 2) {
			middle = 0
		} else if isMiddle(1, 0, 2) {
			middle = 1
		} else {
			middle = 2
		}

		isCorner := func (r, c int) bool {
			if (r == 1 && c == 1) || (r == 1 && c == n) || (r == n && c == 1) || (r == n && c == n) {
				return true
			}
			return false
		}

		if isCorner(p[middle][0], p[middle][1]) {
			if p[middle][0] == x || p[middle][1] == y {
				fmt.Println("YES")
				continue
			}
			fmt.Println("NO")
			continue
		}

		isYes := func(r, c int) bool {
			if int(math.Abs(float64(x - r))) & 1  > 0 && int(math.Abs(float64(y - c))) & 1 > 0 {
				return true
			}
			return false
		}
		if isYes(p[0][0], p[0][1]) || isYes(p[1][0], p[1][1]) || isYes(p[2][0], p[2][1]) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var T int
	fmt.Scan(&T)
	for tt:=0; tt<T; tt++ {
		var n int
		fmt.Scan(&n)
		p := make([][]int, 3)
		for i:=0; i<3; i++ {
			p[i] = make([]int, 2)
			for j:=0; j<2; j++ {
				fmt.Scan(&p[i][j])
			}
		}
		// fmt.Scan(&n, &p1[0], &p1[1], &p2[0], &p2[1], &p3[0], &p3[1])

		// fmt.Println(tt);

		var x, y int
		fmt.Scan(&x, &y)
		isYes := func(r, c int) bool {
			if int(math.Abs(float64(x - r))) % 2 == 0 && int(math.Abs(float64(y - c))) % 2 == 0 {
				// fmt.Println(r, c)
				return true
			}
			return false
		}

		middle := -1
		isMiddle := func (m, o1, o2 int) bool {
			if p[m][0] == p[o1][0] {
				return p[m][1] == p[o2][1]
			} else if p[m][0] == p[o2][0] {
				return p[m][1] == p[o2][1]
			}
			return false
		}
		if isMiddle(0, 1, 2) {
			middle = 0
		} else if isMiddle(1, 0, 2) {
			middle = 1
		} else {
			middle = 2
		}

		isCorner := func (r, c int) bool {
			if (r == 1 && c == 1) || (r == 1 && c == n) || (r == n && c == 1) || (r == n && c == n) {
				return true
			}
			return false
		}

		if isCorner(p[middle][0], p[middle][1]) {
			if p[middle][0] == x || p[middle][1] == y {
				fmt.Println("YES")
				continue
			}
			fmt.Println("NO")
			continue
		}

		if isYes(p[0][0], p[0][1]) || isYes(p[1][0], p[1][1]) || isYes(p[2][0], p[2][1]) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}