Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array $$$a$$$ with elements $$$a_1, a_2, \ldots, a_{2k-1}$$$, is the array $$$b$$$ with elements $$$b_1, b_2, \ldots, b_{k}$$$ such that $$$b_i$$$ is equal to the median of $$$a_1, a_2, \ldots, a_{2i-1}$$$ for all $$$i$$$. Omkar has found an array $$$b$$$ of size $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$-10^9 \leq b_i \leq 10^9$$$). Given this array $$$b$$$, Ray wants to test Omkar's claim and see if $$$b$$$ actually is an OmkArray of some array $$$a$$$. Can you help Ray?The median of a set of numbers $$$a_1, a_2, \ldots, a_{2i-1}$$$ is the number $$$c_{i}$$$ where $$$c_{1}, c_{2}, \ldots, c_{2i-1}$$$ represents $$$a_1, a_2, \ldots, a_{2i-1}$$$ sorted in nondecreasing order.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$b$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$-10^9 \leq b_i \leq 10^9$$$) — the elements of $$$b$$$. It is guaranteed the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output one line containing YES if there exists an array $$$a$$$ such that $$$b_i$$$ is the median of $$$a_1, a_2, \dots, a_{2i-1}$$$ for all $$$i$$$, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).

NoteIn the second case of the first sample, the array $$$[4]$$$ will generate an OmkArray of $$$[4]$$$, as the median of the first element is $$$4$$$.In the fourth case of the first sample, the array $$$[3, 2, 5]$$$ will generate an OmkArray of $$$[3, 3]$$$, as the median of $$$3$$$ is $$$3$$$ and the median of $$$2, 3, 5$$$ is $$$3$$$.In the fifth case of the first sample, the array $$$[2, 1, 0, 3, 4, 4, 3]$$$ will generate an OmkArray of $$$[2, 1, 2, 3]$$$ as   the median of $$$2$$$ is $$$2$$$  the median of $$$0, 1, 2$$$ is $$$1$$$  the median of $$$0, 1, 2, 3, 4$$$ is $$$2$$$  and the median of $$$0, 1, 2, 3, 3, 4, 4$$$ is $$$3$$$. In the second case of the second sample, the array $$$[1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5]$$$ will generate an OmkArray of $$$[1, 1, 3, 1, 0, -2, -1]$$$, as   the median of $$$1$$$ is $$$1$$$  the median of $$$0, 1, 4$$$ is $$$1$$$  the median of $$$0, 1, 3, 4, 5$$$ is $$$3$$$  the median of $$$-2, -2, 0, 1, 3, 4, 5$$$ is $$$1$$$  the median of $$$-4, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$0$$$  the median of $$$-4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$-2$$$  and the median of $$$-4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5$$$ is $$$-1$$$ For all cases where the answer is NO, it can be proven that it is impossible to find an array $$$a$$$ such that $$$b$$$ is the OmkArray of $$$a$$$.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

// github.com/EndlessCheng/codeforces-go
type node1 struct {
	lr       [2]*node1
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node1) cmp(b int) int {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

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

func (o *node1) maintain() { o.sz = o.dupCnt + o.lr[0].size() + o.lr[1].size() }

func (o *node1) rotate(d int) *node1 {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	o.maintain()
	x.maintain()
	return x
}

type treap1 struct {
	rd   uint
	root *node1
}

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

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

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

func (t *treap1) _delete(o *node1, key int) *node1 {
	if o == nil {
		return nil
	}
	if d := o.cmp(key); d >= 0 {
		o.lr[d] = t._delete(o.lr[d], key)
	} else {
		if o.dupCnt > 1 {
			o.dupCnt--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			d = 0
			if o.lr[0].priority > o.lr[1].priority {
				d = 1
			}
			o = o.rotate(d)
			o.lr[d] = t._delete(o.lr[d], key)
		}
	}
	o.maintain()
	return o
}

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

func newTreap1() *treap1 { return &treap1{rd: uint(time.Now().UnixNano())/2 + 1} }

func (t *treap1) lowerBound(key int) (lb *node1) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap1) prev(key int) (prev *node1) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap1) next(key int) (next *node1) {
	for o := t.root; o != nil; {
		if o.cmp(key) == 0 {
			next = o
			o = o.lr[0]
		} else {
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

// 有 k 个元素小于 o.key
func (t *treap1) kth(k int) (o *node1) {
	for o = t.root; o != nil; {
		if ls := o.lr[0].size(); k < ls {
			o = o.lr[0]
		} else {
			k -= o.dupCnt + ls
			if k < 0 {
				return
			}
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		l, r := -inf, inf
		t := newTreap1()
		t.put(b[0])
		for i := 1; i < n; i++ {
			v := b[i]
			if v < l || v > r {
				Fprintln(out, "NO")
				return
			}
			if v == b[i-1] {
				t.put(-inf)
				t.put(inf)
			} else if v < b[i-1] {
				if v == l {
					t.put(-inf)
					t.put(-inf)
				} else {
					t.put(v)
					t.put(-inf)
				}
			} else {
				if v == r {
					t.put(inf)
					t.put(inf)
				} else {
					t.put(v)
					t.put(inf)
				}
			}
			lo := t.kth(i - 1)
			if lo == nil {
				l = -inf
			} else {
				l = lo.key
			}
			ro := t.kth(i + 1)
			if ro == nil {
				r = inf
			} else {
				r = ro.key
			}
		}
		Fprintln(out, "YES")
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

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

// Tags:
// Fenwick Tree

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

	var t int
	fmt.Fscan(reader, &t)

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(reader, &n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &b[i])
		}

		c := make([]int, n)
		copy(c, b)
		sort.Ints(c)

		for i := range b {
			b[i] = sort.SearchInts(c, b[i]) + 1
		}

		cnt := make([]int, n+1)
		add(cnt, n, b[0])

		ok := true
		for i := 1; i < n; i++ {
			if b[i-1] > b[i] && query(cnt, b[i-1]-1)-query(cnt, b[i]) > 0 ||
				b[i-1] <= b[i] && query(cnt, b[i]-1)-query(cnt, b[i-1]) > 0 {
				ok = false
				break
			}
			add(cnt, n, b[i])
		}
		if ok {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func add(cnt []int, n, i int) {
	for ; i <= n; i += i & (-i) {
		cnt[i]++
	}
}

func query(cnt []int, i int) int {
	var out int
	for ; i > 0; i -= i & (-i) {
		out += cnt[i]
	}
	return out
}

package main

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

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

	var t int
	fmt.Fscan(reader, &t)

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(reader, &n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &b[i])
		}

		c := make([]int, n)
		copy(c, b)
		sort.Ints(c)

		for i := range b {
			b[i] = sort.SearchInts(c, b[i]) + 1
		}

		cnt := make([]int, n+1)
		for i := b[0]; i <= n; i += i & (-i) {
			cnt[i]++
		}

		ok := true
		for i := 1; i < n; i++ {
			if b[i-1] > b[i] && sum(cnt, b[i-1]-1)-sum(cnt, b[i]) > 0 ||
				b[i-1] <= b[i] && sum(cnt, b[i]-1)-sum(cnt, b[i-1]) > 0 {
				ok = false
				break
			}
			for j := b[i]; j <= n; j += j & (-j) {
				cnt[j]++
			}
		}
		if ok {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func sum(cnt []int, i int) int {
	var out int
	for ; i > 0; i -= i & (-i) {
		out += cnt[i]
	}
	return out
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}
func InitInts3D(n, m, k int, val int) [][][]int {
	res := make([][][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts2D(m, k, val)
	}
	return res
}

type StepState struct {
	step, dist int
}
type PriorityQueue []*StepState

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	if pq[i].step != pq[j].step {
		return pq[i].step < pq[j].step
	}
	return pq[i].dist < pq[j].dist
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
	item := x.(*StepState)
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type AvlNode struct {
	key    int
	height int
	size   int
	left   *AvlNode
	right  *AvlNode
}

func NewAvlNode(key int) *AvlNode {
	return &AvlNode{
		key:    key,
		height: 0,
		size:   1,
		left:   nil,
		right:  nil,
	}
}

type AvlTree struct {
	root *AvlNode
}

func NewAvlTree() *AvlTree {
	return &AvlTree{}
}

func (t *AvlTree) height(node *AvlNode) int {
	if node == nil {
		return -1
	}
	return node.height
}

func (t *AvlTree) size(node *AvlNode) int {
	if node == nil {
		return 0
	}
	return node.size
}

func (t *AvlTree) balanceFactor(node *AvlNode) int {
	return t.height(node.right) - t.height(node.left)
}

func (t *AvlTree) fixHeight(node *AvlNode) {
	hl := t.height(node.left)
	hr := t.height(node.right)
	newHeight := hl + 1
	if newHeight < hr+1 {
		newHeight = hr + 1
	}
	node.height = newHeight
}

func (t *AvlTree) fixSize(node *AvlNode) {
	node.size = t.size(node.left) + t.size(node.right) + 1
}

func (t *AvlTree) fixNode(node *AvlNode) {
	t.fixHeight(node)
	t.fixSize(node)
}

func (t *AvlTree) rotateRight(p *AvlNode) *AvlNode {
	q := p.left
	p.left = q.right
	q.right = p
	t.fixNode(p)
	t.fixNode(q)
	return q
}

func (t *AvlTree) rotateLeft(q *AvlNode) *AvlNode {
	p := q.right
	q.right = p.left
	p.left = q
	t.fixNode(q)
	t.fixNode(p)
	return p
}

func (t *AvlTree) balance(node *AvlNode) *AvlNode {
	t.fixNode(node)
	if t.balanceFactor(node) == 2 {
		if t.balanceFactor(node.right) < 0 {
			node.right = t.rotateRight(node.right)
		}
		return t.rotateLeft(node)
	}
	if t.balanceFactor(node) == -2 {
		if t.balanceFactor(node.left) > 0 {
			node.left = t.rotateLeft(node.left)
		}
		return t.rotateRight(node)
	}
	return node
}

func (t *AvlTree) insert(node *AvlNode, key int) *AvlNode {
	if node == nil {
		return NewAvlNode(key)
	}
	if key < node.key {
		node.left = t.insert(node.left, key)
	} else {
		node.right = t.insert(node.right, key)
	}
	tes := t.balance(node)
	return tes
}

func (t *AvlTree) Insert(key int) {
	t.root = t.insert(t.root, key)
}

func (t *AvlTree) findByOrder(node *AvlNode, order int) int {
	if node == nil {
		return -1
	}
	if order == t.size(node.left) {
		return node.key
	} else if order < t.size(node.left) {
		return t.findByOrder(node.left, order)
	} else {
		return t.findByOrder(node.right, order-t.size(node.left)-1)
	}
}

func (t *AvlTree) FindByOrder(order int) int {
	return t.findByOrder(t.root, order)
}

func (t *AvlTree) orderOfKey(node *AvlNode, key int) int {
	if node == nil {
		return 0
	} else if node.key == key {
		return t.size(node.left)
	} else if key < node.key {
		return t.orderOfKey(node.left, key)
	} else {
		return t.size(node.left) + 1 + t.orderOfKey(node.right, key)
	}
}

func (t *AvlTree) OrderOfKey(key int) int {
	return t.orderOfKey(t.root, key)
}

func (t *AvlTree) keyLessThan(node *AvlNode, key int, curLessMax int) int {
	if node == nil {
		return curLessMax
	}
	if node.key < key {
		return t.keyLessThan(node.right, key, node.key)
	} else {
		return t.keyLessThan(node.left, key, curLessMax)
	}
}

func (t *AvlTree) KeyLessThan(key int) int {
	return t.keyLessThan(t.root, key, -1)
}

func (t *AvlTree) findMin(node *AvlNode) *AvlNode {
	if node.left == nil {
		return node
	}
	return t.findMin(node.left)
}

func (t *AvlTree) removeMin(node *AvlNode) *AvlNode {
	if node.left == nil {
		return node.right
	}
	node.left = t.removeMin(node.left)
	return t.balance(node)
}

func (t *AvlTree) remove(p *AvlNode, key int) *AvlNode {
	if p == nil {
		return nil
	}
	if key < p.key {
		p.left = t.remove(p.left, key)
	} else if key > p.key {
		p.right = t.remove(p.right, key)
	} else {
		q := p.left
		r := p.right
		if r == nil {
			return q
		}
		minRight := t.findMin(r)
		minRight.right = t.removeMin(r)
		minRight.left = q
		return t.balance(minRight)
	}
	return t.balance(p)
}

func (t *AvlTree) Remove(key int) {
	t.root = t.remove(t.root, key)
}

const (
	INF = 1000000001
)

func process(N int, b []int) string {
	avl := NewAvlTree()
	avl.Insert(b[0])
	curSize := 1
	for i := 1; i < N; i++ {
		order := avl.OrderOfKey(b[i])
		// fmt.Println(b[i], order, avl.FindByOrder(order))
		if avl.FindByOrder(order) == b[i] {
			if order == curSize/2-1 {
				avl.Insert(-INF)
				avl.Insert(-INF)
			} else if order == curSize/2 {
				avl.Insert(-INF)
				avl.Insert(INF)
			} else if order == curSize/2+1 {
				avl.Insert(INF)
				avl.Insert(INF)
			} else {
				return "NO"
			}
		} else if order == curSize/2 {
			avl.Insert(b[i])
			avl.Insert(-INF)
		} else if order == curSize/2+1 {
			avl.Insert(b[i])
			avl.Insert(INF)
		} else {
			return "NO"
		}
		curSize += 2
	}
	return "YES"
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.NextInt()
		b := sc.NextInts(N)

		fmt.Fprintln(wr, process(N, b))
	}

}

package main

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

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

	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)

		b := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &b[j])
		}

		ans := solve(b, n)

		fmt.Println(ans)
		//b - 6 4 5

		//a - 6 4 -3 5 7
		//
	}
}

func solve(b []int, n int) string {

	mp := newIntIntTreeMap(func(a int, b int) bool {
		return a < b
	})

	if len(b) == 1 || len(b) == 2 {
		return "YES"
	}

	mp.Set(b[0], 0)
	mp.Set(b[1], 0)

	for i := 2; i < n; i++ {
		prev := b[i - 1]

		cur := b[i]

		if cur == prev {
			continue
		}

		if cur > prev {

			it := mp.UpperBound(prev)

			if it.Valid() && it.Key() < cur {
				return "NO"
			}

		} else {
			it := mp.LowerBound(prev)
			it.Prev()

			if it.node != nil && it.Key() > cur {
				return "NO"
			}
		}

		mp.Set(cur, 0)
	}

	return "YES"
}

type intIntTreeMap struct {
	endNode   *nodeIntIntTreeMap
	beginNode *nodeIntIntTreeMap
	count     int
	// Less returns a < b
	Less func(a int, b int) bool
}

type nodeIntIntTreeMap struct {
	right   *nodeIntIntTreeMap
	left    *nodeIntIntTreeMap
	parent  *nodeIntIntTreeMap
	isBlack bool
	key     int
	value   int
}

// New creates and returns new TreeMap.
// Parameter less is a function returning a < b.
func newIntIntTreeMap(less func(a int, b int) bool) *intIntTreeMap {
	endNode := &nodeIntIntTreeMap{isBlack: true}
	return &intIntTreeMap{beginNode: endNode, endNode: endNode, Less: less}
}

// Len returns total count of elements in a map.
// Complexity: O(1).
func (t *intIntTreeMap) Len() int { return t.count }

// Set sets the value and silently overrides previous value if it exists.
// Complexity: O(log N).
func (t *intIntTreeMap) Set(key int, value int) {
	parent := t.endNode
	current := parent.left
	less := true
	for current != nil {
		parent = current
		switch {
		case t.Less(key, current.key):
			current = current.left
			less = true
		case t.Less(current.key, key):
			current = current.right
			less = false
		default:
			current.value = value
			return
		}
	}
	x := &nodeIntIntTreeMap{parent: parent, value: value, key: key}
	if less {
		parent.left = x
	} else {
		parent.right = x
	}
	if t.beginNode.left != nil {
		t.beginNode = t.beginNode.left
	}
	t.insertFixup(x)
	t.count++
}

// Del deletes the value.
// Complexity: O(log N).
func (t *intIntTreeMap) Del(key int) {
	z := t.findNode(key)
	if z == nil {
		return
	}
	if t.beginNode == z {
		if z.right != nil {
			t.beginNode = z.right
		} else {
			t.beginNode = z.parent
		}
	}
	t.count--
	removeNodeIntIntTreeMap(t.endNode.left, z)
}

// Clear clears the map.
// Complexity: O(1).
func (t *intIntTreeMap) Clear() {
	t.count = 0
	t.beginNode = t.endNode
	t.endNode.left = nil
}

// Get retrieves a value from a map for specified key and reports if it exists.
// Complexity: O(log N).
func (t *intIntTreeMap) Get(id int) (int, bool) {
	node := t.findNode(id)
	if node == nil {
		node = t.endNode
	}
	return node.value, node != t.endNode
}

// Contains checks if key exists in a map.
// Complexity: O(log N)
func (t *intIntTreeMap) Contains(id int) bool { return t.findNode(id) != nil }

// Range returns a pair of iterators that you can use to go through all the keys in the range [from, to].
// More specifically it returns iterators pointing to lower bound and upper bound.
// Complexity: O(log N).
func (t *intIntTreeMap) Range(from, to int) (forwardIteratorIntIntTreeMap, forwardIteratorIntIntTreeMap) {
	return t.LowerBound(from), t.UpperBound(to)
}

// LowerBound returns an iterator pointing to the first element that is not less than the given key.
// Complexity: O(log N).
func (t *intIntTreeMap) LowerBound(key int) forwardIteratorIntIntTreeMap {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return forwardIteratorIntIntTreeMap{tree: t, node: t.endNode}
	}
	for {
		if t.Less(node.key, key) {
			if node.right != nil {
				node = node.right
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		}
	}
}

// UpperBound returns an iterator pointing to the first element that is greater than the given key.
// Complexity: O(log N).
func (t *intIntTreeMap) UpperBound(key int) forwardIteratorIntIntTreeMap {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return forwardIteratorIntIntTreeMap{tree: t, node: t.endNode}
	}
	for {
		if !t.Less(key, node.key) {
			if node.right != nil {
				node = node.right
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return forwardIteratorIntIntTreeMap{tree: t, node: result}
			}
		}
	}
}

// Iterator returns an iterator for tree map.
// It starts at the first element and goes to the one-past-the-end position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(1)
func (t *intIntTreeMap) Iterator() forwardIteratorIntIntTreeMap {
	return forwardIteratorIntIntTreeMap{tree: t, node: t.beginNode}
}

// Reverse returns a reverse iterator for tree map.
// It starts at the last element and goes to the one-before-the-start position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(log N)
func (t *intIntTreeMap) Reverse() reverseIteratorIntIntTreeMap {
	node := t.endNode.left
	if node != nil {
		node = mostRightIntIntTreeMap(node)
	}
	return reverseIteratorIntIntTreeMap{tree: t, node: node}
}

func (t *intIntTreeMap) findNode(id int) *nodeIntIntTreeMap {
	current := t.endNode.left
	for current != nil {
		switch {
		case t.Less(id, current.key):
			current = current.left
		case t.Less(current.key, id):
			current = current.right
		default:
			return current
		}
	}
	return nil
}

func mostLeftIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	for x.left != nil {
		x = x.left
	}
	return x
}

func mostRightIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	for x.right != nil {
		x = x.right
	}
	return x
}

func successorIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	if x.right != nil {
		return mostLeftIntIntTreeMap(x.right)
	}
	for x != x.parent.left {
		x = x.parent
	}
	return x.parent
}

func predecessorIntIntTreeMap(x *nodeIntIntTreeMap) *nodeIntIntTreeMap {
	if x.left != nil {
		return mostRightIntIntTreeMap(x.left)
	}
	for x.parent != nil && x != x.parent.right {
		x = x.parent
	}
	return x.parent
}

func rotateLeftIntIntTreeMap(x *nodeIntIntTreeMap) {
	y := x.right
	x.right = y.left
	if x.right != nil {
		x.right.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.left = x
	x.parent = y
}

func rotateRightIntIntTreeMap(x *nodeIntIntTreeMap) {
	y := x.left
	x.left = y.right
	if x.left != nil {
		x.left.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.right = x
	x.parent = y
}

func (t *intIntTreeMap) insertFixup(x *nodeIntIntTreeMap) {
	root := t.endNode.left
	x.isBlack = x == root
	for x != root && !x.parent.isBlack {
		if x.parent == x.parent.parent.left {
			y := x.parent.parent.right
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x != x.parent.left {
					x = x.parent
					rotateLeftIntIntTreeMap(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				rotateRightIntIntTreeMap(x)
				break
			}
		} else {
			y := x.parent.parent.left
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x == x.parent.left {
					x = x.parent
					rotateRightIntIntTreeMap(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				rotateLeftIntIntTreeMap(x)
				break
			}
		}
	}
}

// nolint: gocyclo
//noinspection GoNilness
func removeNodeIntIntTreeMap(root *nodeIntIntTreeMap, z *nodeIntIntTreeMap) {
	var y *nodeIntIntTreeMap
	if z.left == nil || z.right == nil {
		y = z
	} else {
		y = successorIntIntTreeMap(z)
	}
	var x *nodeIntIntTreeMap
	if y.left != nil {
		x = y.left
	} else {
		x = y.right
	}
	var w *nodeIntIntTreeMap
	if x != nil {
		x.parent = y.parent
	}
	if y == y.parent.left {
		y.parent.left = x
		if y != root {
			w = y.parent.right
		} else {
			root = x // w == nil
		}
	} else {
		y.parent.right = x
		w = y.parent.left
	}
	removedBlack := y.isBlack
	if y != z {
		y.parent = z.parent
		if z == z.parent.left {
			y.parent.left = y
		} else {
			y.parent.right = y
		}
		y.left = z.left
		y.left.parent = y
		y.right = z.right
		if y.right != nil {
			y.right.parent = y
		}
		y.isBlack = z.isBlack
		if root == z {
			root = y
		}
	}
	if removedBlack && root != nil {
		if x != nil {
			x.isBlack = true
		} else {
			for {
				if w != w.parent.left {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						rotateLeftIntIntTreeMap(w.parent)
						if root == w.left {
							root = w
						}
						w = w.left.right
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if x == root || !x.isBlack {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.right == nil || w.right.isBlack {
							w.left.isBlack = true
							w.isBlack = false
							rotateRightIntIntTreeMap(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.right.isBlack = true
						rotateLeftIntIntTreeMap(w.parent)
						break
					}
				} else {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						rotateRightIntIntTreeMap(w.parent)
						if root == w.right {
							root = w
						}
						w = w.right.left
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if !x.isBlack || x == root {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.left == nil || w.left.isBlack {
							w.right.isBlack = true
							w.isBlack = false
							rotateLeftIntIntTreeMap(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.left.isBlack = true
						rotateRightIntIntTreeMap(w.parent)
						break
					}
				}
			}
		}
	}
}

// ForwardIterator represents a position in a tree map.
// It is designed to iterate a map in a forward order.
// It can point to any position from the first element to the one-past-the-end element.
type forwardIteratorIntIntTreeMap struct {
	tree *intIntTreeMap
	node *nodeIntIntTreeMap
}

// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-past-the-end position.
func (i forwardIteratorIntIntTreeMap) Valid() bool { return i.node != i.tree.endNode }

// Next moves an iterator to the next element.
// It panics if goes out of bounds.
func (i *forwardIteratorIntIntTreeMap) Next() {
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
	i.node = successorIntIntTreeMap(i.node)
}

// Prev moves an iterator to the previous element.
// It panics if goes out of bounds.
func (i *forwardIteratorIntIntTreeMap) Prev() {
	i.node = predecessorIntIntTreeMap(i.node)
	if i.node == nil {
		//panic("out of bound iteration")
	}
}

// Key returns a key at an iterator's position
func (i forwardIteratorIntIntTreeMap) Key() int { return i.node.key }

// Value returns a value at an iterator's position
func (i forwardIteratorIntIntTreeMap) Value() int { return i.node.value }

// ReverseIterator represents a position in a tree map.
// It is designed to iterate a map in a reverse order.
// It can point to any position from the one-before-the-start element to the last element.
type reverseIteratorIntIntTreeMap struct {
	tree *intIntTreeMap
	node *nodeIntIntTreeMap
}

// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-before-the-start position.
func (i reverseIteratorIntIntTreeMap) Valid() bool { return i.node != nil }

// Next moves an iterator to the next element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorIntIntTreeMap) Next() {
	if i.node == nil {
		panic("out of bound iteration")
	}
	i.node = predecessorIntIntTreeMap(i.node)
}

// Prev moves an iterator to the previous element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorIntIntTreeMap) Prev() {
	if i.node != nil {
		i.node = successorIntIntTreeMap(i.node)
	} else {
		i.node = i.tree.beginNode
	}
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
}

// Key returns a key at an iterator's position
func (i reverseIteratorIntIntTreeMap) Key() int { return i.node.key }

// Value returns a value at an iterator's position
func (i reverseIteratorIntIntTreeMap) Value() int { return i.node.value }

package main

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

//Treap Implementation
type node1 struct {
	lr       [2]*node1
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node1) cmp(b int) int {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

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

func (o *node1) maintain() { o.sz = o.dupCnt + o.lr[0].size() + o.lr[1].size() }

func (o *node1) rotate(d int) *node1 {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	o.maintain()
	x.maintain()
	return x
}

type treap1 struct {
	rd   uint
	root *node1
}

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

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

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

func (t *treap1) _delete(o *node1, key int) *node1 {
	if o == nil {
		return nil
	}
	if d := o.cmp(key); d >= 0 {
		o.lr[d] = t._delete(o.lr[d], key)
	} else {
		if o.dupCnt > 1 {
			o.dupCnt--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			d = 0
			if o.lr[0].priority > o.lr[1].priority {
				d = 1
			}
			o = o.rotate(d)
			o.lr[d] = t._delete(o.lr[d], key)
		}
	}
	o.maintain()
	return o
}

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

func newTreap1() *treap1 { return &treap1{rd: uint(time.Now().UnixNano())/2 + 1} }

func (t *treap1) lowerBound(key int) (lb *node1) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap1) prev(key int) (prev *node1) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap1) next(key int) (next *node1) {
	for o := t.root; o != nil; {
		if o.cmp(key) == 0 {
			next = o
			o = o.lr[0]
		} else {
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap1) kth(k int) (o *node1) {
	for o = t.root; o != nil; {
		if ls := o.lr[0].size(); k < ls {
			o = o.lr[0]
		} else {
			k -= o.dupCnt + ls
			if k < 0 {
				return
			}
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	solve := func(Case int) {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		const inf int = 1e9
		t := newTreap1()
		t.put(a[0])
		ok := true
		for i := 1; i < n; i++ {
			v := a[i]
			prev := a[i-1]
			if v < prev {
				it := t.prev(prev)
				if it != nil && v < it.key {
					ok = false
					break
				}
			} else if v > prev {
				it := t.next(prev)
				if it != nil && v > it.key {
					ok = false
					break
				}
			}
			t.put(v)
		}
		if ok == true {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}

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

func max(x, y int64) int64 {
	if x < y {
		return y
	}
	return x
}

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

var _rbuf = bufio.NewReader(os.Stdin)
var _wbuf = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { _wbuf.Flush(); fmt.Fscan(_rbuf, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_wbuf, f, a...) }

func main() {
	defer _wbuf.Flush()

	var t int
	scan(&t)
	for ; t > 0; t-- {
		var n int
		scan(&n)
		b := make([]int, n)
		for i := range b {
			scan(&b[i])
		}
		if solve(n, b) {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func solve(n int, b []int) bool {
	c := make([]int, n)
	copy(c, b)
	sort.Ints(c)
	//c = unique(c)
	for i := range b {
		b[i] = sort.SearchInts(c, b[i]) + 1
	}

	m := len(c)
	bits := make([]int, m+1)
	add := func(i int) {
		for ; i <= m; i += i & (-i) {
			bits[i]++
		}
	}
	sum := func(i int) int {
		ret := 0
		for ; i > 0; i -= i & (-i) {
			ret += bits[i]
		}
		return ret
	}

	add(b[0])
	for i := 1; i < n; i++ {
		if b[i-1] > b[i] {
			if sum(b[i-1]-1)-sum(b[i]) > 0 {
				return false
			}
		} else if sum(b[i]-1)-sum(b[i-1]) > 0 {
			return false
		}
		add(b[i])
	}
	return true
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

// github.com/EndlessCheng/codeforces-go
type node struct {
	lr       [2]*node
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node) cmp(b int) int {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

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

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

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

type treap struct {
	rd   uint
	root *node
}

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

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

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

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

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

func newTreap() *treap { return &treap{rd: uint(time.Now().UnixNano())/2 + 1} }

func (t *treap) lowerBound(key int) (lb *node) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap) prev(key int) (prev *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap) next(key int) (next *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) == 0 {
			next = o
			o = o.lr[0]
		} else {
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

// 有 k 个元素小于 o.key
func (t *treap) kth(k int) (o *node) {
	for o = t.root; o != nil; {
		if ls := o.lr[0].size(); k < ls {
			o = o.lr[0]
		} else {
			k -= o.dupCnt + ls
			if k < 0 {
				return
			}
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		l, r := -inf, inf
		t := newTreap()
		t.put(b[0])
		for i := 1; i < n; i++ {
			v := b[i]
			if v < l || v > r {
				Fprintln(out, "NO")
				return
			}
			if v == b[i-1] {
				t.put(-inf)
				t.put(inf)
			} else if v < b[i-1] {
				if v == l {
					x := -inf
					o := t.prev(l)
					if o != nil {
						x = o.key
					}
					t.put(x)
					t.put(x)
				} else {
					t.put(v)
					t.put(l)
				}
			} else {
				if v == r {
					x := inf
					o := t.next(r)
					if o != nil {
						x = o.key
					}
					t.put(x)
					t.put(x)
				} else {
					t.put(v)
					t.put(r)
				}
			}

			//lo := t.lowerBound(v)
			//if lo == nil {
			//	t.put(v)
			//	t.put(inf)
			//} else if lo.key == v {
			//	if v == l {
			//		x := -inf
			//		o := t.prev(l)
			//		if o != nil {
			//			x = o.key
			//		}
			//		t.put(x)
			//		t.put(x)
			//	} else if v == r {
			//		x := inf
			//		o := t.next(r)
			//		if o != nil {
			//			x = o.key
			//		}
			//		t.put(x)
			//		t.put(x)
			//	} else {
			//		t.put(l)
			//		t.put(r)
			//	}
			//
			//} else {
			//	t.put(l)
			//	t.put(v)
			//}
			lo := t.kth(i - 1)
			if lo == nil {
				l = -inf
			} else {
				l = lo.key
			}
			ro := t.kth(i + 1)
			if ro == nil {
				r = inf
			} else {
				r = ro.key
			}
		}
		Fprintln(out, "YES")
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

// github.com/EndlessCheng/codeforces-go
type node struct {
	lr       [2]*node
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node) cmp(b int) int {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

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

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

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

type treap struct {
	rd   uint
	root *node
}

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

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

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

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

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

func newTreap() *treap { return &treap{rd: uint(time.Now().UnixNano())/2 + 1} }

func (t *treap) lowerBound(key int) (lb *node) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap) prev(key int) (prev *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap) next(key int) (next *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) == 0 {
			next = o
			o = o.lr[0]
		} else {
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

// 有 k 个元素小于 o.key
func (t *treap) kth(k int) (o *node) {
	for o = t.root; o != nil; {
		if ls := o.lr[0].size(); k < ls {
			o = o.lr[0]
		} else {
			k -= o.dupCnt + ls
			if k < 0 {
				return
			}
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		l, r := -inf, inf
		t := newTreap()
		t.put(b[0])
		for i := 1; i < n; i++ {
			v := b[i]
			if v < l || v > r {
				Fprintln(out, "NO")
				return
			}
			if v == b[i-1] {
				t.put(l)
				t.put(r)
			} else if v < b[i-1] {
				if v == l {
					x := -inf
					o := t.prev(l)
					if o != nil {
						x = o.key
					}
					t.put(x)
					t.put(x)
				} else {
					t.put(v)
					t.put(l)
				}
			} else {
				if v == r {
					x := inf
					o := t.next(r)
					if o != nil {
						x = o.key
					}
					t.put(x)
					t.put(x)
				} else {
					t.put(v)
					t.put(r)
				}
			}

			//lo := t.lowerBound(v)
			//if lo == nil {
			//	t.put(v)
			//	t.put(inf)
			//} else if lo.key == v {
			//	if v == l {
			//		x := -inf
			//		o := t.prev(l)
			//		if o != nil {
			//			x = o.key
			//		}
			//		t.put(x)
			//		t.put(x)
			//	} else if v == r {
			//		x := inf
			//		o := t.next(r)
			//		if o != nil {
			//			x = o.key
			//		}
			//		t.put(x)
			//		t.put(x)
			//	} else {
			//		t.put(l)
			//		t.put(r)
			//	}
			//
			//} else {
			//	t.put(l)
			//	t.put(v)
			//}
			lo := t.kth(i - 1)
			if lo == nil {
				l = -inf
			} else {
				l = lo.key
			}
			ro := t.kth(i + 1)
			if ro == nil {
				r = inf
			} else {
				r = ro.key
			}
		}
		Fprintln(out, "YES")
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

// github.com/EndlessCheng/codeforces-go
type node struct {
	lr       [2]*node
	priority uint
	key      int
	dupCnt   int
	sz       int
}

func (o *node) cmp(b int) int {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

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

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

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

type treap struct {
	rd   uint
	root *node
}

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

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

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

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

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

func newTreap() *treap { return &treap{rd: uint(time.Now().UnixNano())/2 + 1} }

func (t *treap) lowerBound(key int) (lb *node) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap) prev(key int) (prev *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func (t *treap) next(key int) (next *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) == 0 {
			next = o
			o = o.lr[0]
		} else {
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

// 有 k 个元素小于 o.key
func (t *treap) kth(k int) (o *node) {
	for o = t.root; o != nil; {
		if ls := o.lr[0].size(); k < ls {
			o = o.lr[0]
		} else {
			k -= o.dupCnt + ls
			if k < 0 {
				return
			}
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const inf int = 2e9
	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		if n <= 2 {
			Fprintln(out, "YES")
			return
		}
		l, r := -inf, inf
		//if b[0] < b[1] {
		//	l = b[0]
		//} else if b[0] > b[1] {
		//	r = b[0]
		//}
		t := newTreap()
		//t.put(-inf)
		//t.put(inf)
		t.put(b[0])
		//t.put(b[1])
		//l,r := -inf

		for i := 1; i < n; i++ {
			v := b[i]
			if v < l || v > r {
				Fprintln(out, "NO")
				return
			}

			lo := t.lowerBound(v)
			if lo == nil {
				t.put(v)
				t.put(inf)
			} else if lo.key == v {
				if v == l {
					x := -inf
					o := t.prev(l)
					if o != nil {
						x= o.key
					}
					t.put(x)
					t.put(x)
				} else if v == r {
					x := inf
					o := t.next(r)
					if o != nil {
						x= o.key
					}
					t.put(x)
					t.put(x)
				} else {
					t.put(l)
					t.put(r)
				}

			} else {
				t.put(l)
				t.put(v)
			}
			l = t.kth(i-1).key
				r=t.kth(i+1).key
		}

		//for i := 2; i < n; i++ {
		//	v := b[i]
		//	if v < l || v > r {
		//		Fprintln(out, "NO")
		//		return
		//	}
		//
		//	lo := t.lowerBound(v)
		//	if lo.key == v {
		//
		//		if
		//
		//		l = t.prev(v).key
		//		r = t.next(v).key
		//		continue
		//	}
		//
		//	r = lo.key
		//	l = t.prev(r).key
		//
		//	t.put(v)
		//	t.prev(l)
		//
		//}

		Fprintln(out, "YES")

	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}
func InitInts3D(n, m, k int, val int) [][][]int {
	res := make([][][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts2D(m, k, val)
	}
	return res
}

type StepState struct {
	step, dist int
}
type PriorityQueue []*StepState

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	if pq[i].step != pq[j].step {
		return pq[i].step < pq[j].step
	}
	return pq[i].dist < pq[j].dist
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
	item := x.(*StepState)
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

type AvlNode struct {
	key    int
	height int
	size   int
	left   *AvlNode
	right  *AvlNode
}

func NewAvlNode(key int) *AvlNode {
	return &AvlNode{
		key:    key,
		height: 0,
		size:   1,
		left:   nil,
		right:  nil,
	}
}

type AvlTree struct {
	root *AvlNode
}

func NewAvlTree() *AvlTree {
	return &AvlTree{}
}

func (t *AvlTree) height(node *AvlNode) int {
	if node == nil {
		return -1
	}
	return node.height
}

func (t *AvlTree) size(node *AvlNode) int {
	if node == nil {
		return 0
	}
	return node.size
}

func (t *AvlTree) balanceFactor(node *AvlNode) int {
	return t.height(node.right) - t.height(node.left)
}

func (t *AvlTree) fixHeight(node *AvlNode) {
	hl := t.height(node.left)
	hr := t.height(node.right)
	newHeight := hl + 1
	if newHeight < hr+1 {
		newHeight = hr + 1
	}
	node.height = newHeight
}

func (t *AvlTree) fixSize(node *AvlNode) {
	node.size = t.size(node.left) + t.size(node.right) + 1
}

func (t *AvlTree) fixNode(node *AvlNode) {
	t.fixHeight(node)
	t.fixSize(node)
}

func (t *AvlTree) rotateRight(p *AvlNode) *AvlNode {
	q := p.left
	p.left = q.right
	q.right = p
	t.fixNode(p)
	t.fixNode(q)
	return q
}

func (t *AvlTree) rotateLeft(q *AvlNode) *AvlNode {
	p := q.right
	q.right = p.left
	p.left = q
	t.fixNode(q)
	t.fixNode(p)
	return p
}

func (t *AvlTree) balance(node *AvlNode) *AvlNode {
	t.fixNode(node)
	if t.balanceFactor(node) == 2 {
		if t.balanceFactor(node.right) < 0 {
			node.right = t.rotateRight(node.right)
		}
		return t.rotateLeft(node)
	}
	if t.balanceFactor(node) == -2 {
		if t.balanceFactor(node.left) > 0 {
			node.left = t.rotateLeft(node.left)
		}
		return t.rotateRight(node)
	}
	return node
}

func (t *AvlTree) insert(node *AvlNode, key int) *AvlNode {
	if node == nil {
		return NewAvlNode(key)
	}
	if key < node.key {
		node.left = t.insert(node.left, key)
	} else {
		node.right = t.insert(node.right, key)
	}
	tes := t.balance(node)
	return tes
}

func (t *AvlTree) Insert(key int) {
	t.root = t.insert(t.root, key)
}

func (t *AvlTree) findByOrder(node *AvlNode, order int) int {
	if node == nil {
		return -1
	}
	if order == t.size(node.left) {
		return node.key
	} else if order < t.size(node.left) {
		return t.findByOrder(node.left, order)
	} else {
		return t.findByOrder(node.right, order-t.size(node.left)-1)
	}
}

func (t *AvlTree) FindByOrder(order int) int {
	return t.findByOrder(t.root, order)
}

func (t *AvlTree) orderOfKey(node *AvlNode, key int) int {
	if node == nil {
		return 0
	} else if node.key == key {
		return t.size(node.left)
	} else if key < node.key {
		return t.orderOfKey(node.left, key)
	} else {
		return t.size(node.left) + 1 + t.orderOfKey(node.right, key)
	}
}

func (t *AvlTree) OrderOfKey(key int) int {
	return t.orderOfKey(t.root, key)
}

func (t *AvlTree) keyLessThan(node *AvlNode, key int, curLessMax int) int {
	if node == nil {
		return curLessMax
	}
	if node.key < key {
		return t.keyLessThan(node.right, key, node.key)
	} else {
		return t.keyLessThan(node.left, key, curLessMax)
	}
}

func (t *AvlTree) KeyLessThan(key int) int {
	return t.keyLessThan(t.root, key, -1)
}

func (t *AvlTree) findMin(node *AvlNode) *AvlNode {
	if node.left == nil {
		return node
	}
	return t.findMin(node.left)
}

func (t *AvlTree) removeMin(node *AvlNode) *AvlNode {
	if node.left == nil {
		return node.right
	}
	node.left = t.removeMin(node.left)
	return t.balance(node)
}

func (t *AvlTree) remove(p *AvlNode, key int) *AvlNode {
	if p == nil {
		return nil
	}
	if key < p.key {
		p.left = t.remove(p.left, key)
	} else if key > p.key {
		p.right = t.remove(p.right, key)
	} else {
		q := p.left
		r := p.right
		if r == nil {
			return q
		}
		minRight := t.findMin(r)
		minRight.right = t.removeMin(r)
		minRight.left = q
		return t.balance(minRight)
	}
	return t.balance(p)
}

func (t *AvlTree) Remove(key int) {
	t.root = t.remove(t.root, key)
}

const (
	INF = 1000000000
)

func process(N int, b []int) string {
	avl := NewAvlTree()
	avl.Insert(b[0])
	curSize := 1
	for i := 1; i < N; i++ {
		order := avl.OrderOfKey(b[i])
		// fmt.Println(b[i], order, avl.FindByOrder(order))
		if avl.FindByOrder(order) == b[i] {
			if order == curSize/2-1 {
				avl.Insert(-INF)
				avl.Insert(-INF)
			} else if order == curSize/2 {
				avl.Insert(-INF)
				avl.Insert(INF)
			} else if order == curSize/2+1 {
				avl.Insert(INF)
				avl.Insert(INF)
			} else {
				return "NO"
			}
		} else if order == curSize/2 {
			avl.Insert(b[i])
			avl.Insert(-INF)
		} else if order == curSize/2+1 {
			avl.Insert(b[i])
			avl.Insert(INF)
		} else {
			return "NO"
		}
		curSize += 2
	}
	return "YES"
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.NextInt()
		b := sc.NextInts(N)

		fmt.Fprintln(wr, process(N, b))
	}

}


Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n = p1·p2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109 + 7 is the password to the secret data base. Now he wants to calculate this value.

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of primes in factorization of n.  The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).

Print one integer — the product of all divisors of n modulo 109 + 7.

NoteIn the first sample n = 2·3 = 6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1·2·3·6 = 36.In the second sample 2·3·2 = 12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1·2·3·4·6·12 = 1728.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF615D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	const mod = 1e9 + 7
	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
	}

	var k, p int
	e := [2e5]int64{}
	for Fscan(in, &k); k > 0; k-- {
		Fscan(in, &p)
		e[p]++
	}
	sq := true
	for _, c := range e {
		if c&1 > 0 {
			sq = false
			break
		}
	}
	var n, m int64 = 1, 1
	if sq {
		for i, c := range e {
			m = m * (c + 1) % (mod - 1)
			e[i] >>= 1
		}
	} else {
		ok := true
		for _, c := range e {
			c++
			if ok && c&1 == 0 {
				c >>= 1
				ok = false
			}
			m = m * c % (mod - 1)
		}
	}
	for p, c := range e {
		n = n * pow(int64(p), c*m%(mod-1)) % mod
	}
	Fprint(out, n)
}

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

package main

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

const (
	MOD = 1000000007
	PHI = MOD - 1
)

func pow(a, s int64) (r int64) {
	r = 1
	for s > 0 {
		if s%2 == 1 {
			r = r * a % MOD
		}
		a = a * a % MOD
		s /= 2
	}
	return
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	const N = 202000
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	var a [N]int
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscanf(buf, "%d", &x)
		a[x]++
	}
	var prod int64 = 1
	allEven := true
	usedOdd := false
	for i := 0; i < N; i++ {
		var cur int64 = int64(a[i]) + 1
		if a[i]%2 == 1 {
			allEven = false
			if !usedOdd {
				usedOdd = true
				cur = int64(a[i]+1) / 2
			}
		}
		prod = prod * cur % PHI
	}
	var res int64 = 1
	for i := 0; i < N; i++ {
		if a[i] == 0 {
			continue
		}
		var exp int64
		if allEven {
			exp = (int64(a[i]) / 2) * prod % PHI
		} else {
			exp = int64(a[i]) * prod % PHI
		}
		res = res * pow(int64(i), exp) % MOD
	}
	fmt.Println(res)
}

package main

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

const (
	MOD = 1000000007
	PHI = MOD - 1
)

func pow(a, s int64) (r int64) {
	r = 1
	for s > 0 {
		if s%2 == 1 {
			r = r * a % MOD
		}
		a = a * a % MOD
		s /= 2
	}
	return
}

func main() {
	const N = 202000
	var n int
	fmt.Scanf("%d", &n)
	var a [N]int
	for i := 0; i < n; i++ {
		var x int
		fmt.Scanf("%d", &x)
		a[x]++
	}
	var prod int64 = 1
	allEven := true
	usedOdd := false
	for i := 0; i < N; i++ {
		var cur int64 = int64(a[i]) + 1
		if a[i]%2 == 1 {
			allEven = false
			if !usedOdd {
				usedOdd = true
				cur = int64(a[i]+1) / 2
			}
		}
		prod = prod * cur % PHI
	}
	var res int64 = 1
	for i := 0; i < N; i++ {
		if a[i] == 0 {
			continue
		}
		var exp int64
		if allEven {
			exp = (int64(a[i]) / 2) * prod % PHI
		} else {
			exp = int64(a[i]) * prod % PHI
		}
		res = res * pow(int64(i), exp) % MOD
	}
	fmt.Println(res)
	writer.Flush()
}

func init() {
	// maybe defer writer.Flush() in main

	lineScanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	/*
		inputFile, err := os.Open("a.in")
		if err != nil {
			panic(err)
		}
		outputFile, err := os.Create("a.out")
		if err != nil {
			panic(err)
		}
		lineScanner = bufio.NewScanner(inputFile)
		writer = bufio.NewWriter(outputFile)
	*/

	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

var writer *bufio.Writer

func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}


President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.

The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President's desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.

Print the only number — the amount of President's deputies.

package main

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

var cs map[int]int = make(map[int]int)

func Init() {}
func Solve(io *FastIO) {
	n, m, c := io.NextInt(), io.NextInt(), byte(io.NextChar())
	var s, so string
	var char byte
	d := make(map[byte]bool)
	so = strings.Repeat(".", m)
	for i := 0; i < n; i++ {
		s = io.NextString()
		var sb byte = '.'
		for j := range s {
			char = s[j]
			if char == c {
				if sb != c && sb != '.' {
					d[sb] = true
				}
				if so[j] != c && so[j] != '.' {
					d[so[j]] = true
				}
			} else if char != c && char != '.' && (sb == c || so[j] == c) {
				d[char] = true
			}
			sb = char
		}
		so = s
	}
	o := 0
	for _ = range d {
		o++
	}
	io.Println(o)
}

type FastIO struct {
	//by megaspazz
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}
func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = res*10 + int(c-'0')
		c = io.NextChar()
	}
	return res * sgn
}
func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = int64(-1)
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = res*10 + int64(c-'0')
		c = io.NextChar()
	}
	return res * sgn
}
func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}
func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextInt()
	}
	return arr
}
func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}
func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextLong()
	}
	return arr
}
func (io *FastIO) NextString() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}
func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}
func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}
func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}
func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}
func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}
func IsLineBreakChar(c byte) bool {
	switch c {
	case 0, '\n', '\r':
		return true
	default:
		return false
	}
}
func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	color := map[byte]bool{}

	var n, m int
	var c byte
	room := [100][100]byte{}

	fmt.Fscanf(reader, "%d %d %c\n", &n, &m, &c)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%c", &room[i][j])
			if room[i][j] == c && i != 0 && room[i-1][j] != c && room[i-1][j] != '.' {
				color[room[i-1][j]] = true
			}
			if room[i][j] == c && j != 0 && room[i][j-1] != c && room[i][j-1] != '.' {
				color[room[i][j-1]] = true
			}
			if room[i][j] != c && i != 0 && room[i-1][j] == c && room[i][j] != '.' {
				color[room[i][j]] = true
			}
			if room[i][j] != c && j != 0 && room[i][j-1] == c && room[i][j] != '.' {
				color[room[i][j]] = true
			}
		}
		fmt.Fscanf(reader, "\n")
	}
	fmt.Printf("%d", len(color))
}

package main

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

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

	var n, m int
	s, _ := reader.ReadBytes('\n')
	pos := readInt(s, 0, &n)
	pos = readInt(s, pos+1, &m)
	P := s[pos+1]

	R := make([]string, n)

	for i := 0; i < n; i++ {
		R[i], _ = reader.ReadString('\n')
	}

	fmt.Println(solve(n, m, P, R))
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, P byte, R []string) int {
	mem := make(map[byte]bool)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if R[i][j] == P {
				for k := 0; k < 4; k++ {
					u, v := i+dd[k], j+dd[k+1]
					if u >= 0 && u < n && v >= 0 && v < m && R[u][v] != P && R[u][v] != '.' {
						mem[R[u][v]] = true
					}
				}
			}
		}
	}

	return len(mem)
}

package main

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

func main() {

	// creates scanner from standard input
	stdin := bufio.NewReader(os.Stdin)
	scanner := bufio.NewScanner(stdin)

	// scans first line to read height, weight, and president color
	scanner.Scan()
	firstLine := scanner.Text()
	reader := strings.NewReader(firstLine)

	// reads initial data from first line
	var width, height int
	var presidentColorString string
	_, _ = fmt.Fscan(reader, &height)
	_, _ = fmt.Fscan(reader, &width)
	_, _ = fmt.Fscan(reader, &presidentColorString)

	// some variables
	paddedHeight := height + 2
	paddedWidth := width + 2
	presidentColor := presidentColorString[0]
	empty := byte('.')
	null := byte(0)

	// add padding around room
	room := make([][]byte, paddedHeight)
	for idx := 0; idx < paddedHeight; idx++ {
		room[idx] = make([]byte, paddedWidth)
	}

	// iterate total input rows
	for rowIdx := 0; rowIdx < height; rowIdx++ {
		if !scanner.Scan() {
			break
		}

		// slice of row without padding
		row := room[rowIdx+1][1 : width+1]

		// read input into slice
		line := scanner.Bytes()
		for col, ch := range line {
			row[col] = ch
		}
	}

	adjacentToPresident := make(map[byte]interface{})
	for row := 1; row < height+1; row++ {
		for col := 1; col < width+1; col++ {
			if room[row][col] == empty || room[row][col] == null || room[row][col] == presidentColor {
				continue
			}

			if room[row-1][col] == presidentColor || room[row+1][col] == presidentColor ||
				room[row][col+1] == presidentColor || room[row][col-1] == presidentColor {
				adjacentToPresident[room[row][col]] = struct{}{}
			}
		}
	}

	fmt.Println(len(adjacentToPresident))

	// for _, row := range room {
	// 	fmt.Println(row)
	// }
	// fmt.Println(">", presidentColor)
}

package main

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

func solve() {
	n := readInt()
	_ = readInt()
	c := readString()[0]

	a := make([]string, n)
	seen := make(map[byte]bool)
	for i, _ := range a {
		a[i] = readString()
	}

	// We have seen the empty space and the president's desk
	seen['.'] = true
	seen[c] = true

	for i, ai := range a {
		for j, cur := range ai {
			if byte(cur) != c {
				continue
			}

			// This is a part of the president's desk.
			// See if anyone else is nearby.
			if i > 0 { // above
				seen[a[i-1][j]] = true
			}
			if i < len(a)-1 { // below
				seen[a[i+1][j]] = true
			}
			if j > 0 { // left
				seen[ai[j-1]] = true
			}
			if j < len(ai)-1 { // right
				seen[ai[j+1]] = true
			}
		}
	}
	fmt.Println(len(seen) - 2)
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

package main

import "fmt"

func mark(table [][]byte, c byte) {
	var n = len(table)
	var m = len(table[0])
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if table[i][j] == c {
				table[i][j] = '.'
			}
		}
	}
}

func main() {
	var n, m int
	var c byte
	_, _ = fmt.Scanf("%d %d %c\n", &n, &m, &c)
	var table = make([][]byte, n)
	for i := 0; i < n; i++ {
		table[i] = make([]byte, m)
		_, _ = fmt.Scanf("%s\n", &table[i])
	}
	var ans = 0

	//for i := 0; i < n; i++ {
	//    for j := 0; j < m; j++ {
	//        fmt.Printf("%c", table[i][j])
	//    }
	//    fmt.Println()
	//}
	//fmt.Println()

	var dr = []int{-1, 0, 1, 0}
	var dc = []int{0, 1, 0, -1}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if table[i][j] == c ||
				table[i][j] == '.' {
				continue
			}
			for k := 0; k < 4; k++ {
				var row = i + dr[k]
				var col = j + dc[k]
				if row >= 0 && row < n && col >= 0 && col < m {
					if table[row][col] == c {
						ans++
						mark(table, table[i][j])
					}
				}
			}
		}
	}
	fmt.Println(ans)
}

package main

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

type p2 struct {
	x, y int
}

func check(x, y int, pc rune, r *[][]rune, c *map[rune]bool) {
	if x < 0 || y < 0 || x >= len((*r)[0]) || y >= len(*r) {
		return
	}
	if (*r)[y][x] != '.' && (*r)[y][x] != pc {
		(*c)[(*r)[y][x]] = true
	}
}
func checkAround(p *p2, r *[][]rune, c *map[rune]bool) {
	check(p.x - 1, p.y, (*r)[p.y][p.x], r, c)
	check(p.x + 1, p.y, (*r)[p.y][p.x], r, c)
	check(p.x, p.y - 1, (*r)[p.y][p.x], r, c)
	check(p.x, p.y + 1, (*r)[p.y][p.x], r, c)
}
func main() {
	l, w, p := nextInt(), nextInt(), ([]rune(nextWord()))[0]
	r := make([][]rune, l)
	pt := make([]*p2, 0)
	for y := 0; y < l; y++ {
		r[y] = []rune(nextWord())
		for x := 0; x < w; x++ {
			if r[y][x] == p {
				pt = append(pt, &p2{x, y})
			}
		}
	}
	colors := make(map[rune]bool)
	for _, pp := range pt {
		checkAround(pp, &r, &colors)
	}
	printf("%d\n", len(colors))
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

import "fmt"

var dotASC = "."[0]

func main() {
	l, w, cs := 0, 0, "byte(0)"
	fmt.Scan(&l, &w, &cs)
	c := cs[0]
	o := make([]string, l)
	for i := 0; i < l; i++ {
		str := ""
		fmt.Scan(&str)
		o[i] = str
	}

	px, py := -1, -1
	for i := 0; i < l; i++ {
		for j := 0; j < w; j++ {
			if o[i][j] == c {
				py, px = i, j
				break
			}
		}
		if (px != -1) || (py != -1) {
			break
		}
	}

	pw, ph := 1, 1
	for i := px + 1; i < w; i++ {
		if o[py][i] == c {
			pw = i - px + 1
		}
	}

	for i := py + 1; i < l; i++ {
		if o[i][px] == c {
			ph = i - py + 1
		}
	}

	ex, ey, ansMap := px+pw-1, py+ph-1, map[byte]bool{}
	if px >= 1 {
		for i := py; i <= ey; i++ {
			ansMap[o[i][px-1]] = true
		}
	}
	if py >= 1 {
		for i := px; i <= ex; i++ {
			ansMap[o[py-1][i]] = true
		}
	}

	if ex < w-1 {
		for i := py; i <= ey; i++ {
			ansMap[o[i][ex+1]] = true
		}
	}
	if ey < l-1 {
		for i := px; i <= ex; i++ {
			ansMap[o[ey+1][i]] = true
		}
	}

	if ansMap[dotASC] {
		fmt.Println(len(ansMap) - 1)
	} else {
		fmt.Println(len(ansMap))
	}
}

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

package main

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

type Cell struct {
	I int
	J int
}

func hasNeighbor(n int, m int, i int, j int, room []string, c string) bool {
	neighbors := []Cell{
		{I: i - 1, J: j},
		{I: i, J: j - 1},
		{I: i, J: j + 1},
		{I: i + 1, J: j},
	}
	for _, cell := range neighbors {
		if 0 <= cell.I && cell.I < n && 0 <= cell.J && cell.J < m && string(room[cell.I][cell.J]) == c {
			return true
		}
	}
	return false
}

func Solve(n int, m int, c string, room []string) int {
	deputies := make(map[string]bool)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if string(room[i][j]) != c && string(room[i][j]) != "." && !deputies[string(room[i][j])] && hasNeighbor(n, m, i, j, room, c) {
				deputies[string(room[i][j])] = true
			}
		}
	}
	return len(deputies)
}

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

	var n int
	var m int
	var c string
	fmt.Fscanf(reader, "%d %d %s\n", &n, &m, &c)
	room := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &room[i])
	}
	fmt.Fprintf(writer, "%d\n", Solve(n, m, c, room))
}

package main

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

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

	var n, m int
	s, _ := reader.ReadBytes('\n')
	pos := readInt(s, 0, &n)
	pos = readInt(s, pos+1, &m)
	P := s[pos+1]

	R := make([]string, n)

	for i := 0; i < n; i++ {
		R[i], _ = reader.ReadString('\n')
	}

	fmt.Println(solve(n, m, P, R))
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, P byte, R []string) int {
	mem := make(map[byte]bool)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if R[i][j] == P {
				for k := 0; k < 4; k++ {
					u, v := i+dd[k], j+dd[k+1]
					if u >= 0 && u < n && v >= 0 && v < n && R[u][v] != P && R[u][v] != '.' {
						mem[R[u][v]] = true
					}
				}
			}
		}
	}

	return len(mem)
}

package main

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

func main() {

	var width, height int
	var presidentColorString string

	_, _ = fmt.Scan(&height)
	_, _ = fmt.Scan(&width)
	_, _ = fmt.Scan(&presidentColorString)

	paddedHeight := height + 2
	paddedWidth := width + 2
	presidentColor := presidentColorString[0]
	empty := byte('.')
	null := byte(0)

	// add padding around room
	room := make([][]byte, paddedHeight)
	for idx := 0; idx < paddedHeight; idx++ {
		room[idx] = make([]byte, paddedWidth)
	}

	scanner := bufio.NewScanner(os.Stdin)

	// iterate total input rows
	for rowIdx := 0; rowIdx < height; rowIdx++ {
		if !scanner.Scan() {
			break
		}

		// slice of row without padding
		row := room[rowIdx+1][1 : width+1]

		// read input into slice
		line := scanner.Bytes()
		for col, ch := range line {
			row[col] = ch
		}
	}

	adjacentToPresident := make(map[byte]interface{})
	for row := 1; row < height+1; row++ {
		for col := 1; col < width+1; col++ {
			if room[row][col] == empty || room[row][col] == null || room[row][col] == presidentColor {
				continue
			}

			if room[row-1][col] == presidentColor || room[row+1][col] == presidentColor ||
				room[row][col+1] == presidentColor || room[row][col-1] == presidentColor {
				adjacentToPresident[room[row][col]] = struct{}{}
			}
		}
	}

	fmt.Println(len(adjacentToPresident))

	for _, row := range room {
		fmt.Println(row)
	}

	fmt.Println(">", presidentColor)

}

package main

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

func main() {

	var width, height int
	var presidentColorString string

	_, _ = fmt.Scan(&height)
	_, _ = fmt.Scan(&width)
	_, _ = fmt.Scan(&presidentColorString)

	paddedHeight := height + 2
	paddedWidth := width + 2
	presidentColor := presidentColorString[0]
	empty := byte('.')

	// add padding around room
	room := make([][]byte, paddedHeight)
	for idx := 0; idx < paddedHeight; idx++ {
		room[idx] = make([]byte, paddedWidth)
	}

	scanner := bufio.NewScanner(os.Stdin)

	for row := 1; row < height+1; row++ {
		if !scanner.Scan() {
			break
		}

		line := scanner.Bytes()
		for col, ch := range line {
			room[row][col+1] = ch
		}
	}

	adjacentToPresident := make(map[byte]interface{})
	for row := 1; row < height+1; row++ {
		for col := 1; col < width+1; col++ {
			if room[row][col] == empty || room[row][col] == presidentColor {
				continue
			}

			if room[row-1][col] == presidentColor || room[row+1][col] == presidentColor ||
				room[row][col+1] == presidentColor || room[row][col-1] == presidentColor {
				adjacentToPresident[room[row][col]] = struct{}{}
			}
		}
	}

	fmt.Println(len(adjacentToPresident))

	// for _, row := range room {
	// 	fmt.Println(row)
	// }
	// fmt.Println(">", presidentColor)

}

package main

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

func main() {

	var width, height int
	var presidentColorString string

	_, _ = fmt.Scan(&height)
	_, _ = fmt.Scan(&width)
	_, _ = fmt.Scan(&presidentColorString)

	paddedHeight := height + 2
	paddedWidth := width + 2
	presidentColor := presidentColorString[0]
	empty := byte('.')
	null := byte(0)

	// add padding around room
	room := make([][]byte, paddedHeight)
	for idx := 0; idx < paddedHeight; idx++ {
		room[idx] = make([]byte, paddedWidth)
	}

	scanner := bufio.NewScanner(os.Stdin)

	for row := 1; row < height+1; row++ {
		if !scanner.Scan() {
			break
		}

		line := scanner.Bytes()
		for col, ch := range line {
			room[row][col+1] = ch
		}
	}

	adjacentToPresident := make(map[byte]interface{})
	for row := 1; row < height+1; row++ {
		for col := 1; col < width+1; col++ {
			if room[row][col] == empty || room[row][col] == null || room[row][col] == presidentColor {
				continue
			}

			if room[row-1][col] == presidentColor || room[row+1][col] == presidentColor ||
				room[row][col+1] == presidentColor || room[row][col-1] == presidentColor {
				adjacentToPresident[room[row][col]] = struct{}{}
			}
		}
	}

	fmt.Println(len(adjacentToPresident))

	// for _, row := range room {
	// 	fmt.Println(row)
	// }
	// fmt.Println(">", presidentColor)

}

package main

import "fmt"

func mark(table [][]byte, c byte) {
	var n = len(table)
	var m = len(table[0])
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if table[i][j] == c {
				table[i][j] = '.'
			}
		}
	}
}

func main() {
	var n, m int
	var c byte
	_, _ = fmt.Scanf("%d %d %c\n", &n, &m, &c)
	var table = make([][]byte, n)
	for i := 0; i < n; i++ {
		table[i] = make([]byte, m)
		_, _ = fmt.Scanf("%s", &table[i])
	}
	var ans = 0

	var dr = []int{-1, 0, 1, 0}
	var dc = []int{0, 1, 0, -1}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if table[i][j] == c ||
				table[i][j] == '.' {
				continue
			}
			for k := 0; k < 4; k++ {
				var row = i + dr[k]
				var col = j + dc[k]
				if row >= 0 && row < n && col >= 0 && col < m {
					if table[row][col] == c {
						ans++
						mark(table, table[i][j])
					}
				}
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const MaxN = 4096

var A [MaxN]int
var M [MaxN]int

func computeMaximums(node, level int) int {
	if level == 0 {
		return 0
	}
	var l = 2 * node
	var r = (2 * node) + 1
	M[l] = A[l] + computeMaximums(l, level-1)
	M[r] = A[r] + computeMaximums(r, level-1)
	if M[l] > M[r] {
		M[node] = M[l]
	} else {
		M[node] = M[r]
	}

	return M[node]
}

func solve(node, level, lit int) int {
	if level == 0 {
		return 0
	}
	var l = 2 * node
	var r = (2 * node) + 1
	var sum = 0
	sum += solve(l, level-1, M[l]-A[l]) + (lit - M[l])
	sum += solve(r, level-1, M[r]-A[r]) + (lit - M[r])
	return sum
}

func main() {
	var n, p int
	_, _ = fmt.Scanf("%d", &n)
	p = (1 << uint(n+1)) - 1
	for i := 2; i <= p; i++ {
		_, _ = fmt.Scanf("%d", &A[i])
	}

	var maxLit = computeMaximums(1, n)
	var ans = solve(1, n, maxLit)
	fmt.Printf("%d\n", ans)
}


You are given an array $$$a$$$ of length $$$2n$$$. Consider a partition of array $$$a$$$ into two subsequences $$$p$$$ and $$$q$$$ of length $$$n$$$ each (each element of array $$$a$$$ should be in exactly one subsequence: either in $$$p$$$ or in $$$q$$$).Let's sort $$$p$$$ in non-decreasing order, and $$$q$$$ in non-increasing order, we can denote the sorted versions by $$$x$$$ and $$$y$$$, respectively. Then the cost of a partition is defined as $$$f(p, q) = \sum_{i = 1}^n |x_i - y_i|$$$.Find the sum of $$$f(p, q)$$$ over all correct partitions of array $$$a$$$. Since the answer might be too big, print its remainder modulo $$$998244353$$$.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 150\,000$$$). The second line contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 10^9$$$) — elements of array $$$a$$$.

Print one integer — the answer to the problem, modulo $$$998244353$$$.

NoteTwo partitions of an array are considered different if the sets of indices of elements included in the subsequence $$$p$$$ are different.In the first example, there are two correct partitions of the array $$$a$$$:  $$$p = [1]$$$, $$$q = [4]$$$, then $$$x = [1]$$$, $$$y = [4]$$$, $$$f(p, q) = |1 - 4| = 3$$$;  $$$p = [4]$$$, $$$q = [1]$$$, then $$$x = [4]$$$, $$$y = [1]$$$, $$$f(p, q) = |4 - 1| = 3$$$. In the second example, there are six valid partitions of the array $$$a$$$:   $$$p = [2, 1]$$$, $$$q = [2, 1]$$$ (elements with indices $$$1$$$ and $$$2$$$ in the original array are selected in the subsequence $$$p$$$);  $$$p = [2, 2]$$$, $$$q = [1, 1]$$$;  $$$p = [2, 1]$$$, $$$q = [1, 2]$$$ (elements with indices $$$1$$$ and $$$4$$$ are selected in the subsequence $$$p$$$);  $$$p = [1, 2]$$$, $$$q = [2, 1]$$$;  $$$p = [1, 1]$$$, $$$q = [2, 2]$$$;  $$$p = [2, 1]$$$, $$$q = [2, 1]$$$ (elements with indices $$$3$$$ and $$$4$$$ are selected in the subsequence $$$p$$$).

package main

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

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	sort.Sort(sort.IntSlice(a))

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &b[i])
	}

	sort.Sort(sort.Reverse(sort.IntSlice(b)))

	sum := uint64(0)
	k := uint64(1)

	for i := 0; i < n; i++ {
		sum += uint64(abs(a[i] - b[i]))
		k = k * divide(uint64(2*n-i), uint64(n-i)) % mod

	}

	sum %= 998244353

	fmt.Fprint(out, (sum*k)%mod, "\n")
}

const mod = 998244353

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

func bin_pow(base, p uint64) uint64 {
	if p == 1 {
		return base
	}

	if p%2 == 0 {
		t := bin_pow(base, p/2)
		return t * t % mod
	} else {
		return bin_pow(base, p-1) * base % mod
	}
}

func inverse_element(x uint64) uint64 {
	return bin_pow(x, mod-2)
}

func divide(a, b uint64) uint64 {
	return a * inverse_element(b) % mod
}

package main

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

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

const mod = 998244353

func Bin(a int64, n int) int64 {
	var ans int64
	ans = 1
	for ; n > 0; n /= 2 {
		if n % 2 == 1 {
			ans = ans * a
			ans %= mod
		}
		a = a * a
		a %= mod
	}
	return ans
}

func inv(a int64) int64 {
	return Bin(a, mod - 2)
}

func solve() {
	var n int
	scanf("%d\n", &n)
	v := make([]int, 2 * n)
	for i := 0; i < 2 * n; i++ {
		scanf("%d ",&v[i])
	}
	sort.Ints(v)
	var res int64
	res = 0
	for i := 0; i < n; i++ {
		res += int64(v[n + i] - v[i])
		res %= mod
	}
	var n1 int64
	n1 = 1
	for i := 1; i <= n; i++ {
		n1 *= int64(i)
		n1 %= mod
	}
	var n2 int64
	n2 = 1
	for i := 1; i <= 2 * n; i++ {
		n2 *= int64(i)
		n2 %= mod
	}
	var ans int64
	ans = n2 * inv(n1) % mod * inv(n1) % mod
	ans = ans * res
	ans %= mod
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	solve()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1444B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}

	var n int64
	Fscan(in, &n)
	a := make([]int, 2*n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	s := int64(0)
	for _, v := range a[:n] {
		s -= int64(v)
	}
	for _, v := range a[n:] {
		s += int64(v)
	}
	f := int64(1)
	for i := int64(2); i <= n; i++ {
		f = f * i % mod
	}
	inv := pow(f, mod-2)
	for i := n + 1; i <= 2*n; i++ {
		f = f * i % mod
	}
	Fprint(out, s%mod*f%mod*inv%mod*inv%mod)
}

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

package main

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

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	sort.Sort(sort.IntSlice(a))

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &b[i])
	}

	sort.Sort(sort.Reverse(sort.IntSlice(b)))

	sum := 0
	k := 1

	for i := 0; i < n; i++ {
		sum += abs(a[i] - b[i])
		k = k * divide(2*n-i, n-i) % mod

	}

	sum %= 998244353

	fmt.Fprint(out, (sum*k)%mod, "\n")
}

const mod = 998244353

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

func bin_pow(base, p int) int {
	if p == 1 {
		return base
	}

	if p%2 == 0 {
		t := bin_pow(base, p/2)
		return t * t % mod
	} else {
		return bin_pow(base, p-1) * base % mod
	}
}

func inverse_element(x int) int {
	return bin_pow(x, mod-2)
}

func divide(a, b int) int {
	return a * inverse_element(b) % mod
}

package main

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

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	sort.Sort(sort.IntSlice(a))

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &b[i])
	}

	sort.Sort(sort.Reverse(sort.IntSlice(b)))

	sum := 0
	k := 1
	kk := 1

	for i := 0; i < n; i++ {
		sum += abs(a[i] - b[i])
		k = (k * (2*n - i)) % 998244353
		kk = (kk * (n - i)) % 998244353

		if k%kk == 0 {
			k /= kk
			kk = 1
		}
	}

	sum %= 998244353

	fmt.Fprint(out, sum*((k/kk)%998244353)%998244353, "\n")
}

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

package main

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

func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	sort.Sort(sort.IntSlice(a))

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &b[i])
	}

	sort.Sort(sort.Reverse(sort.IntSlice(b)))

	sum := 0
	k := 1
	kk := 1

	for i := 0; i < n; i++ {
		sum += abs(a[i] - b[i])
		k = (k * (2*n - i))

		if k%(n-i) == 0 {
			k /= n - i
		} else {
			kk = (kk * (n - i))
		}

		if k%kk == 0 {
			k /= kk
			kk = 1
		}
	}

	sum %= 998244353

	fmt.Fprint(out, (sum*((k/kk)%998244353))%998244353, "\n")
}

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

package main

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

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

const mod = 998244353

func Bin(a int64, n int) int64 {
	var ans int64
	ans = 1
	for ; n > 0; n /= 2 {
		if n % 2 == 1 {
			ans = ans * a
			ans %= mod
		}
		a = a * a
		a %= mod
	}
	return ans
}

func inv(a int64) int64 {
	return Bin(a, mod - 2)
}

func solve() {
	var n int
	scanf("%d\n", &n)
	v := make([]int, 2 * n)
	for i := 0; i < 2 * n; i++ {
		scanf("%d ",&v[i])
	}
	sort.Ints(v)
	var res int64
	res = 0
	for i := 0; i < n; i++ {
		res += int64(v[n + i] - v[i])
	}
	var n1 int64
	n1 = 1
	for i := 1; i <= n; i++ {
		n1 *= int64(i)
		n1 %= mod
	}
	var n2 int64
	n2 = 1
	for i := 1; i <= 2 * n; i++ {
		n2 *= int64(i)
		n2 %= mod
	}
	var ans int64
	ans = n2 * inv(n1) % mod * inv(n1) % mod
	ans = ans * res
	ans %= mod
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	solve()
}

package main

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

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

const mod = 998244353

func Bin(a int64, n int) int64 {
	var ans int64
	ans = 1
	for ; n > 0; n /= 2 {
		if n % 2 == 1 {
			ans = ans * a
			ans %= mod
		}
		a = a * a
		a %= mod
	}
	return ans
}

func inv(a int64) int64 {
	return Bin(a, mod - 2)
}

func solve() {
	var n int
	scanf("%d\n", &n)
	v := make([]int, 2 * n)
	for i := 0; i < 2 * n; i++ {
		scanf("%d ",&v[i])
	}
	sort.Ints(v)
	var res int64
	res = 0
	for i := 0; i < n; i++ {
		res += int64(v[n + i] - v[i])
	}
	var n1 int64
	n1 = 1
	for i := 1; i <= n; i++ {
		n1 *= int64(i)
		n1 %= mod
	}
	var n2 int64
	n2 = 1
	for i := 1; i <= 2 * n; i++ {
		n2 *= int64(i)
		n2 %= mod
	}
	var ans int64
	ans = n2 * inv(n1) % mod * inv(n1)
	ans = ans * res
	ans %= mod
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	solve()
}


One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume that the string contains the letters of some users of the R1 mail. Recovering letters is a tedious mostly manual work. So before you start this process, it was decided to estimate the difficulty of recovering. Namely, we need to calculate the number of different substrings of the saved string that form correct e-mail addresses.We assume that valid addresses are only the e-mail addresses which meet the following criteria:  the address should begin with a non-empty sequence of letters, numbers, characters '_', starting with a letter;  then must go character '@';  then must go a non-empty sequence of letters or numbers;  then must go character '.';  the address must end with a non-empty sequence of letters. You got lucky again and the job was entrusted to you! Please note that the substring is several consecutive characters in a string. Two substrings, one consisting of the characters of the string with numbers l1, l1 + 1, l1 + 2, ..., r1 and the other one consisting of the characters of the string with numbers l2, l2 + 1, l2 + 2, ..., r2, are considered distinct if l1 ≠ l2 or r1 ≠ r2.

The first and the only line contains the sequence of characters s1s2... sn (1 ≤ n ≤ 106) — the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'.

Print in a single line the number of substrings that are valid e-mail addresses.

NoteIn the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com.In the second test case note that the e-mail x@x.x is considered twice in the answer. Note that in this example the e-mail entries overlap inside the string.

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

var letters string = "abcdefghijklmnopqrstuvwxyz"
var digits string = "0123456789"
func isLetter(ch byte) bool {
  return strings.IndexByte(letters, ch) != -1
}
func isDigit(ch byte) bool {
  return strings.IndexByte(digits, ch) != -1
}

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

  line, _ := reader.ReadString('\n')
  line = strings.TrimSpace(line)
  n := len(line)
  count := int64(0)
  for pos := 0; pos < n; pos++ {
    if line[pos] != '@' {
      continue
    }
    left := int64(0)
    for i := pos-1; i >= 0; i-- {
      ch := line[i]
      if isLetter(ch) {
        left++
      } else if !isDigit(ch) && ch != '_' {
        break
      }
    }
    if left == 0 {
      continue
    }
    right := int64(0)
    for i := pos+1; i < n; i++ {
      ch := line[i]
      if !isLetter(ch) && !isDigit(ch) {
        if ch == '.' && i-pos > 1 {
          for i++; i < n && isLetter(line[i]); i++ {
            right++
          }
        }
        break
      }
    }
    count += left*right
  }
  writer.WriteString(fmt.Sprintf("%d\n", count))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

var letters string = "abcdefghijklmnopqrstuvwxyz"
var digits string = "0123456789"
func isLetter(ch byte) bool {
  return strings.IndexByte(letters, ch) != -1
}
func isDigit(ch byte) bool {
  return strings.IndexByte(digits, ch) != -1
}

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

  line, _ := reader.ReadString('\n')
  line = strings.TrimSpace(line)
  n := len(line)
  count := 0
  for pos := 0; pos < n; pos++ {
    if line[pos] != '@' {
      continue
    }
    left := 0
    for i := pos-1; i >= 0; i-- {
      ch := line[i]
      if isLetter(ch) {
        left++
      } else if !isDigit(ch) && ch != '_' {
        break
      }
    }
    if left == 0 {
      continue
    }
    right := 0
    for i := pos+1; i < n; i++ {
      ch := line[i]
      if !isLetter(ch) && !isDigit(ch) {
        if ch == '.' && i-pos > 1 {
          for i++; i < n && isLetter(line[i]); i++ {
            right++
          }
        }
        break
      }
    }
    count += left*right
  }
  writer.WriteString(fmt.Sprintf("%d\n", count))
}