Vasya has a sequence $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number $$$6$$$ $$$(\dots 00000000110_2)$$$ into $$$3$$$ $$$(\dots 00000000011_2)$$$, $$$12$$$ $$$(\dots 000000001100_2)$$$, $$$1026$$$ $$$(\dots 10000000010_2)$$$ and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence.Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to $$$0$$$.For the given sequence $$$a_1, a_2, \ldots, a_n$$$ Vasya'd like to calculate number of integer pairs $$$(l, r)$$$ such that $$$1 \le l \le r \le n$$$ and sequence $$$a_l, a_{l + 1}, \dots, a_r$$$ is good.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — length of the sequence. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$) — the sequence $$$a$$$.

Print one integer — the number of pairs $$$(l, r)$$$ such that $$$1 \le l \le r \le n$$$ and the sequence $$$a_l, a_{l + 1}, \dots, a_r$$$ is good.

NoteIn the first example pairs $$$(2, 3)$$$ and $$$(1, 3)$$$ are valid. Pair $$$(2, 3)$$$ is valid since $$$a_2 = 7 \rightarrow 11$$$, $$$a_3 = 14 \rightarrow 11$$$ and $$$11 \oplus 11 = 0$$$, where $$$\oplus$$$ — bitwise exclusive or. Pair $$$(1, 3)$$$ is valid since $$$a_1 = 6 \rightarrow 3$$$, $$$a_2 = 7 \rightarrow 13$$$, $$$a_3 = 14 \rightarrow 14$$$ and $$$3 \oplus 13 \oplus 14 = 0$$$.In the second example pairs $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 4)$$$ and $$$(1, 4)$$$ are valid.

package main

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

func ones(x int64) int64 {
	var ret int64
	for ;x != 0; x, ret = x >> 1, ret + x & 1 {}
	return ret
}

func main() {

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int64, n + 1)
	sum := make([]int64, n + 1)
	var odd, even int64 = 0, 1
	for i := 1;i <= n;i ++ {
		fmt.Fscanf(reader, "%d ", &a[i])
		a[i] = ones(a[i])
		sum[i] = sum[i - 1] + a[i]
		if sum[i] % 2 == 1 {
			odd ++
		} else {
			even ++
		}
	}
	ans = odd * (odd - 1) / 2 + even * (even - 1) / 2
	for i := 1;i <= n;i ++ {
		var max int64 = -1
		for j := 0;j <= 61 && i + j <= n;j ++ {
			segSum := sum[j + i] - sum[i - 1]
			if max < a[i + j] {
				max = a[i + j]
			}
			if segSum % 2 == 0 && max * 2 > segSum {
				ans --
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [65][65][65][2]int

func main() {

	for i := 0;i <= 64;i ++ {
		for j := i;j <= 64;j += 2 {
			for k := 0;k <= 64;k ++ {
				minl, maxr := 65, -1
				for l := i;l <= j;l += 2 {
					if k + l <= 64 && k + l >= maxr {
						maxr = k + l
					} else if k + l > 64 && 128 - k - l >= maxr{
						maxr = 128 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [65][65]int64{}, [65][65]int64{}
	for i := 0;i < n;i ++ {
		var x int64
		if i < n - 1 {
			fmt.Fscanf(reader, "%d ", &x)
		} else {
			fmt.Fscanf(reader, "%d", &x)
		}
		xone := ones(x)
		for j := 0;j <= 64;j ++ {
			for k := j;k <= 64;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpCur[xone][xone] ++

		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 64;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 64;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [65][65][65][2]int

func main() {

	for i := 0;i <= 64;i ++ {
		for j := i;j <= 64;j += 2 {
			for k := 0;k <= 64;k ++ {
				minl, maxr := 65, -1
				for l := i;l <= j;l += 2 {
					if k + l <= 64 && k + l > maxr {
						maxr = k + l
					} else if k + l > 64 && 128 - k - l > maxr{
						maxr = 128 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [65][65]int64{}, [65][65]int64{}
	for i := 0;i < n;i ++ {
		dpPrev[0][0] ++
		var x int64
		if i < n - 1 {
			fmt.Fscanf(reader, "%d ", &x)
		} else {
			fmt.Fscanf(reader, "%d", &x)
		}
		xone := ones(x)
		for j := 0;j <= 64;j ++ {
			for k := j;k <= 64;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 64;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 64;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [62][62][62][2]int

func main() {

	for i := 0;i <= 61;i ++ {
		for j := i;j <= 61;j += 2 {
			for k := 0;k <= 61;k ++ {
				minl, maxr := 61, 0
				for l := i;l <= j;l += 2 {
					if k + l <= 61 && k + l > maxr {
						maxr = k + l
					} else if k + l > 61 && 122 - k - l > maxr{
						maxr = 122 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [62][62]int64{}, [62][62]int64{}
	for i := 0;i < n;i ++ {
		dpPrev[0][0] ++
		var x int64
		if i < n - 1 {
			fmt.Fscanf(reader, "%d ", &x)
		} else {
			fmt.Fscanf(reader, "%d", &x)
		}
		xone := ones(x)
		for j := 0;j <= 61;j ++ {
			for k := j;k <= 61;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 61;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 61;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [61][61][61][2]int

func main() {

	for i := 0;i <= 60;i ++ {
		for j := i;j <= 60;j += 2 {
			for k := 0;k <= 60;k ++ {
				minl, maxr := 60, 0
				for l := i;l <= j;l += 2 {
					if k + l <= 60 && k + l > maxr {
						maxr = k + l
					} else if k + l > 60 && 120 - k - l > maxr{
						maxr = 120 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [61][61]int64{}, [61][61]int64{}
	for i := 0;i < n;i ++ {
		dpPrev[0][0] ++
		var x int64
		if i < n - 1 {
			fmt.Fscanf(reader, "%d ", &x)
		} else {
			fmt.Fscanf(reader, "%d", &x)
		}
		xone := ones(x)
		for j := 0;j <= 60;j ++ {
			for k := j;k <= 60;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 60;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 60;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [65][65][65][2]int

func main() {

	for i := 0;i <= 64;i ++ {
		for j := i;j <= 64;j += 2 {
			for k := 0;k <= 64;k ++ {
				minl, maxr := 65, -1
				for l := i;l <= j;l += 2 {
					if k + l <= 64 && k + l >= maxr {
						maxr = k + l
					} else if k + l > 64 && 128 - k - l >= maxr{
						maxr = 128 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [65][65]int64{}, [65][65]int64{}
	for i := 0;i < n;i ++ {
		var x int64
		fmt.Fscanf(reader, "%d ", &x)
		xone := ones(x)
		for j := 0;j <= 64;j ++ {
			for k := j;k <= 64;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpCur[xone][xone] ++

		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 64;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 64;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}

package main

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

func ones(x int64) int {
	ret := 0
	for ;x != 0; x, ret = x >> 1, ret + int(x & 1) {}
	return ret
}

var bounds [65][65][65][2]int

func main() {

	for i := 0;i <= 64;i ++ {
		for j := i;j <= 64;j += 2 {
			for k := 0;k <= 64;k ++ {
				minl, maxr := 64, 0
				for l := i;l <= j;l += 2 {
					if k + l <= 64 && k + l >= maxr {
						maxr = k + l
					} else if k + l > 64 && 128 - k - l >= maxr{
						maxr = 128 - k - l
					}
					if l > k && minl > l - k {
						minl = l - k
					} else if l <= k && minl > k - l {
						minl = k - l
					}
				}
				bounds[i][j][k][0] = minl
				bounds[i][j][k][1] = maxr
			}
		}
	}

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	var ans int64
	fmt.Fscanf(reader, "%d\n", &n)
	dpPrev, dpCur := [65][65]int64{}, [65][65]int64{}
	for i := 0;i < n;i ++ {
		var x int64
		fmt.Fscanf(reader, "%d ", &x)
		xone := ones(x)
		for j := 0;j <= 64;j ++ {
			for k := j;k <= 64;k += 2 {
				l, r := bounds[j][k][xone][0], bounds[j][k][xone][1]
				dpCur[l][r] += dpPrev[j][k]
			}
		}
		dpCur[xone][xone] ++

		dpPrev, dpCur = dpCur, dpPrev
		for j := 0;j <= 64;j ++ {
			ans += dpPrev[0][j]
			for k := 0;k <= 64;k ++ {
				dpCur[j][k] = 0
			}
		}
	}
	fmt.Printf("%d", ans)
}


Alice has a cake, and she is going to cut it. She will perform the following operation $$$n-1$$$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $$$w\ge 2$$$ and cut it into two smaller pieces of weight $$$\lfloor\frac{w}{2}\rfloor$$$ and $$$\lceil\frac{w}{2}\rceil$$$ ($$$\lfloor x \rfloor$$$ and $$$\lceil x \rceil$$$ denote floor and ceiling functions, respectively).After cutting the cake in $$$n$$$ pieces, she will line up these $$$n$$$ pieces on a table in an arbitrary order. Let $$$a_i$$$ be the weight of the $$$i$$$-th piece in the line.You are given the array $$$a$$$. Determine whether there exists an initial weight and sequence of operations which results in $$$a$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single line: print YES if the array $$$a$$$ could have resulted from Alice's operations, otherwise print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES

NoteIn the first test case, it's possible to get the array $$$a$$$ by performing $$$0$$$ operations on a cake with weight $$$327$$$.In the second test case, it's not possible to get the array $$$a$$$.In the third test case, it's possible to get the array $$$a$$$ by performing $$$1$$$ operation on a cake with weight $$$1\,970\,429\,473$$$:   Cut it in half, so that the weights are $$$[985\,214\,736, 985\,214\,737]$$$.  Note that the starting weight can be greater than $$$10^9$$$.In the fourth test case, it's possible to get the array $$$a$$$ by performing $$$2$$$ operations on a cake with weight $$$6$$$:  Cut it in half, so that the weights are $$$[3,3]$$$.  Cut one of the two pieces with weight $$$3$$$, so that the new weights are $$$[1, 2, 3]$$$ which is equivalent to $$$[2, 3, 1]$$$ up to reordering.

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

/*

 */

type IntHeap []int64

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] >= h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	*h = append(*h, x.(int64))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := &IntHeap{}
	heap.Init(a)

	sum := int64(0)

	for i := 0; i < n; i++ {
		var num int64
		scanf("%d", &num)

		heap.Push(a, num)
		sum += num
	}
	scanf("\n")

	b := &IntHeap{sum}
	heap.Init(b)

	for a.Len() > 0 {
		maxA := (*a)[0]
		maxB := heap.Pop(b).(int64)

		if maxB > maxA {
			halfMaxNum := maxB / 2
			heap.Push(b, halfMaxNum)
			heap.Push(b, maxB-halfMaxNum)
		} else if maxB < maxA {
			fmt.Fprintln(writer, "NO")
			return
		} else {
			heap.Pop(a)
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int64) int64 {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int {
//	return abs(a) / gcd(a, b) * abs(b)
//}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
//func digitAt(num, pos int) int {
//	if num == 0 {
//		return 0
//	}
//
//	num = abs(num)
//
//	lenNum := int(math.Log10(float64(num))) + 1
//	if pos < 0 || pos >= lenNum {
//		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
//		panic(err)
//	}
//
//	rem := num % int(math.Pow10(pos+1))
//	return rem / int(math.Pow10(pos))
//}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

/*
https://codeforces.com/contest/1654/problem/C
*/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		sum := int64(0)
		mp := make(map[int64]int)
		// arr := make([]int, N)
		min_val := int64(1000000000)
		for i:=0; i<N; i++ {
			v, _ := strconv.Atoi(line_arr[i])
			sum+=int64(v)
			//arr[i] = v
			_, ok := mp[int64(v)]
			if ok {
				mp[int64(v)] = mp[int64(v)] + 1
			} else {
				mp[int64(v)] = 1
			}
			if min_val > int64(v) {
				min_val = int64(v)
			}
		}

		//fmt.Printf("%v\n", arr)
		//fmt.Printf("%v\n", sum)
		//fmt.Printf("%v\n", mp)

		if N == 1 {
			fmt.Println("YES")
			continue
		}

		var q []int64
		q = append(q, sum)
		q_ind := 0
		num_div := 0
		for q_ind < len(q) && num_div < N {
			//fmt.Printf("queue: %v\n", q)
			//fmt.Printf("q_ind: %d\n", q_ind)
			t := q[q_ind]
			q_ind++
			c, ok := mp[t]
			if ok {
				if c == 1 {
					delete(mp, t)
				} else {
					mp[t] = c - 1
				}
			} else {
				f, c := splitNum(t)
				if f < min_val {
					break
				}
				q = append(q, f, c)
				num_div++
			}
			//fmt.Printf("num_div: %d\n", num_div)
			//fmt.Printf("map -- %v\n", mp)
		}

		if len(mp) > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

func splitNum(v int64) (int64, int64) {
	if v %2 == 0 {
		x := v/2
		return x, x
	}
	f := int64(math.Floor(float64(v)/2))
	c := int64(math.Ceil(float64(v)/2))
	return f, c
}

package main

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

// 1 2 3 4
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		sum := int64(0)
		count := map[int64]int{}
		for i := 0; i < n; i++ {
			num := int64(0)
			fmt.Fscan(in, &num)
			sum += num
			count[num]++
		}
		list := []int64{sum}
		if n == 1 {
			fmt.Fprintln(out, "YES")
			continue
		}
		for len(list) > 0 && len(list) < n {
			var temp []int64
			for _, i := range list {
				var a, b int64
				if i&1 == 1 {
					a, b = i/2, i/2+1
				} else {
					a, b = i/2, i/2
				}
				if count[a] > 0 {
					count[a]--
				} else {
					temp = append(temp, a)
				}
				if count[b] > 0 {
					count[b]--
				} else {
					temp = append(temp, b)
				}
			}
			list = temp
		}
		if len(list) == 0 {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

// YES
//NO
//YES
//YES
//NO
//YES
//NO
//YES
//YES
//YES
//YES
//NO
//NO
//YES

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"os"
	"sort"
)

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

// This example demonstrates an integer heap built using the heap interface.

// An IntHeap is a min-heap of ints.
type IntHeap []int64

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int64))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

// // This example inserts several ints into an IntHeap, checks the minimum,
// // and removes them in order of priority.
// func main() {
// 	h := &IntHeap{2, 1, 5}
// 	heap.Init(h)
// 	heap.Push(h, 3)
// 	fmt.Printf("minimum: %d\n", (*h)[0])
// 	for h.Len() > 0 {
// 		fmt.Printf("%d ", heap.Pop(h))
// 	}
// }

func main() {
	_r, _w := os.Stdin, os.Stdout
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var solve func()

	/*-------------------------------Todo-----------------------------------------*/

	solve = func() {
		var n int
		var total int64
		Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			total += int64(a[i])
		}
		sort.Ints(a)
		p := n - 1
		h := &IntHeap{total}
		heap.Init(h)
		for h.Len() > 0 && p >= 0 {
			maxV := heap.Pop(h).(int64)
			if maxV > int64(a[p]) {
				maxV1 := maxV / 2
				maxV2 := (maxV + 1) / 2
				heap.Push(h, maxV1)
				heap.Push(h, maxV2)
			} else if maxV == int64(a[p]) {
				p--
			} else {
				Fprintln(out, "NO")
				return
			}
		}
		if p == -1 && h.Len() == 0 {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "NO")
		}
	}

	/*----------------------------------------------------------------------------*/
	var T int = 1
	Fscan(in, &T)
	for i := 0; i < T; i++ {
		solve()
	}
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type any = interface{}

type myHeap []int64

func (h *myHeap) Less(i, j int) bool {
	return (*h)[i] < (*h)[j]
}

func (h *myHeap) Swap(i, j int) {
	(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *myHeap) Len() int {
	return len(*h)
}

func (h *myHeap) Pop() (v any) {
	*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
	return
}

func (h *myHeap) Push(v any) {
	*h = append(*h, v.(int64))
}

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	var hp myHeap
	mp := map[int64]int{}
	sum := int64(0)
	for i := 0; i < n; i++ {
		var item int64
		fmt.Fscan(in, &item)
		mp[item]++
		sum += item
	}
	heap.Push(&hp, sum)
	for n != 0 && hp.Len() != 0 {
		p := heap.Pop(&hp).(int64)
		if mp[p] != 0 {
			mp[p]--
			n--
			continue
		}
		if p == 1 {
			fmt.Fprintln(out, "No")
			return
		}
		heap.Push(&hp, p/2)
		heap.Push(&hp, (p+1)/2)
	}
	if n == 0 {
		fmt.Fprintln(out, "Yes")
	} else {
		fmt.Fprintln(out, "No")
	}
}

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}
func sum(a, b int64) int64 {
	return (b + a) * (b - a + 1) / 2
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

	var T, n, v int64
	for Fscan(in, &T); T > 0; T-- {
		c := map[int64]int{}
		s := int64(0)
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &v)
			c[v]++
			s += v
		}
		var f func(int64) bool
		f = func(x int64) bool {
			if c[x] > 0 {
				c[x]--
				return true
			}
			return x > 1 && f(x/2) && f((x+1)/2)
		}
		if f(s) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"sort"
)

/*
2
7
5 5 5 6 10 10 40
6
5 5 6 10 10 40
---
YES
NO
=======
1
10
7 1 1 1 3 1 3 3 2 3
======
1
10
1 2 4 7 1 1 1 1 7 2
---
YES
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int64, n)

	numToCnt := make(map[int64]int)

	sum := int64(0)

	for i := range a {
		scanf("%d", &a[i])

		numToCnt[a[i]]++

		sum += a[i]
	}
	scanf("\n")

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	maxIdx := n - 1

	q := list.New()
	q.PushBack(sum)

	for q.Len() > 0 && len(numToCnt) > 0 {
		front := q.Remove(q.Front()).(int64)

		for maxIdx >= 0 && numToCnt[a[maxIdx]] == 0 {
			maxIdx--
		}

		if maxIdx >= 0 && front < a[maxIdx] && front > 2 {
			fmt.Fprintln(writer, "NO")
			return
		}

		if numToCnt[front] > 0 {
			numToCnt[front]--

			if numToCnt[front] == 0 {
				delete(numToCnt, front)
			}
		} else if front == 1 || front < a[0] {
			fmt.Fprintln(writer, "NO")
			return
		} else {
			lft := front / 2
			rht := lft + front%2

			q.PushBack(rht)
			q.PushBack(lft)
		}

	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return nums[minNumIdx]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int64) int64 {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int {
//	return abs(a) / gcd(a, b) * abs(b)
//}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
//func digitAt(num, pos int) int {
//	if num == 0 {
//		return 0
//	}
//
//	num = abs(num)
//
//	lenNum := int(math.Log10(float64(num))) + 1
//	if pos < 0 || pos >= lenNum {
//		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
//		panic(err)
//	}
//
//	rem := num % int(math.Pow10(pos+1))
//	return rem / int(math.Pow10(pos))
//}

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"sort"
)

/*
2
7
5 5 5 6 10 10 40
6
5 5 6 10 10 40
---
YES
NO
=======
1
10
7 1 1 1 3 1 3 3 2 3
======
1
10
1 2 4 7 1 1 1 1 7 2
---
YES
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int64, n)

	numToCnt := make(map[int64]int)

	sum := int64(0)

	for i := range a {
		scanf("%d", &a[i])

		numToCnt[a[i]]++

		sum += a[i]
	}
	scanf("\n")

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	maxIdx := n - 1

	q := list.New()
	q.PushBack(sum)

	for q.Len() > 0 && len(numToCnt) > 0 {
		front := q.Remove(q.Front()).(int64)

		if numToCnt[front] > 0 {
			numToCnt[front]--

			if numToCnt[front] == 0 {
				delete(numToCnt, front)
			}
		} else if front == 1 || front < a[0] {
			fmt.Fprintln(writer, "NO")
			return
		} else {
			lft := front / 2
			rht := lft + front%2

			q.PushBack(rht)
			q.PushBack(lft)

			for maxIdx >= 0 && numToCnt[a[maxIdx]] == 0 {
				maxIdx--
			}

			if maxIdx >= 0 && (lft < a[maxIdx] && rht < a[maxIdx] && front < a[maxIdx]) {
				fmt.Fprintln(writer, "NO")
				return
			}
		}

	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return nums[minNumIdx]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int64) int64 {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int {
//	return abs(a) / gcd(a, b) * abs(b)
//}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
//func digitAt(num, pos int) int {
//	if num == 0 {
//		return 0
//	}
//
//	num = abs(num)
//
//	lenNum := int(math.Log10(float64(num))) + 1
//	if pos < 0 || pos >= lenNum {
//		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
//		panic(err)
//	}
//
//	rem := num % int(math.Pow10(pos+1))
//	return rem / int(math.Pow10(pos))
//}

/*
https://codeforces.com/contest/1654/problem/C
*/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		sum := 0
		mp := make(map[int]int)
		arr := make([]int, N)
		min_val := 1000000000
		for i:=0; i<N; i++ {
			v, _ := strconv.Atoi(line_arr[i])
			sum+=v
			arr[i] = v
			_, ok := mp[v]
			if ok {
				mp[v] = mp[v] + 1
			} else {
				mp[v] = 1
			}
			if min_val > v {
				min_val = v
			}
		}

		//fmt.Printf("%v\n", arr)
		//fmt.Printf("%v\n", sum)
		//fmt.Printf("%v\n", mp)

		if N == 1 {
			fmt.Println("YES")
			continue
		}

		var q []int
		q = append(q, sum)
		q_ind := 0
		num_div := 0
		for q_ind < len(q) {
			fmt.Printf("queue: %v\n", q)
			fmt.Printf("q_ind: %d\n", q_ind)
			t := q[q_ind]
			q_ind++
			c, ok := mp[t]
			if ok {
				if c == 1 {
					delete(mp, t)
				} else {
					mp[t] = c - 1
				}
			} else {
				f, c := splitNum(t)
				if f < min_val {
					break
				}
				q = append(q, f, c)
				num_div++
			}
			fmt.Printf("num_div: %d\n", num_div)
			fmt.Printf("map -- %v\n", mp)
		}

		if len(mp) > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

func splitNum(v int) (int, int) {
	if v %2 == 0 {
		x := v/2
		return x, x
	}
	f := int(math.Floor(float64(v)/2))
	c := int(math.Ceil(float64(v)/2))
	return f, c
}

/*
https://codeforces.com/contest/1654/problem/C
*/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		sum := 0
		mp := make(map[int]int)
		arr := make([]int, N)
		min_val := 1000000000
		for i:=0; i<N; i++ {
			v, _ := strconv.Atoi(line_arr[i])
			sum+=v
			arr[i] = v
			_, ok := mp[v]
			if ok {
				mp[v] = mp[v] + 1
			} else {
				mp[v] = 1
			}
			if min_val > v {
				min_val = v
			}
		}

		//fmt.Printf("%v\n", arr)
		//fmt.Printf("%v\n", sum)
		//fmt.Printf("%v\n", mp)

		if N == 1 {
			fmt.Println("YES")
			continue
		}

		var q []int
		q = append(q, sum)
		q_ind := 0
		num_div := 0
		for q_ind < len(q) {
			//fmt.Printf("queue: %v\n", q)
			//fmt.Printf("q_ind: %d\n", q_ind)
			t := q[q_ind]
			q_ind++
			c, ok := mp[t]
			if ok {
				if c == 1 {
					delete(mp, t)
				} else {
					mp[t] = c - 1
				}
			} else {
				f, c := splitNum(t)
				if f < min_val {
					break
				}
				q = append(q, f, c)
				num_div++
			}
			//fmt.Printf("num_div: %d\n", num_div)
			//fmt.Printf("map -- %v\n", mp)
		}

		if len(mp) > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

func splitNum(v int) (int, int) {
	if v %2 == 0 {
		x := v/2
		return x, x
	}
	f := int(math.Floor(float64(v)/2))
	c := int(math.Ceil(float64(v)/2))
	return f, c
}

/*
https://codeforces.com/contest/1654/problem/C
*/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		sum := 0
		mp := make(map[int]int)
		arr := make([]int, N)
		min_val := 1000000000
		for i:=0; i<N; i++ {
			v, _ := strconv.Atoi(line_arr[i])
			sum+=v
			arr[i] = v
			_, ok := mp[v]
			if ok {
				mp[v] = mp[v] + 1
			} else {
				mp[v] = 1
			}
			if min_val > v {
				min_val = v
			}
		}

		//fmt.Printf("%v\n", arr)
		//fmt.Printf("%v\n", sum)
		//fmt.Printf("%v\n", mp)

		if N == 1 {
			fmt.Println("YES")
			continue
		}

		var q []int
		q = append(q, sum)
		q_ind := 0
		num_div := 0
		for q_ind < len(q) {
			//fmt.Printf("queue: %v\n", q)
			//fmt.Printf("q_ind: %d\n", q_ind)
			t := q[q_ind]
			q_ind++
			c, ok := mp[t]
			if ok {
				if c == 1 {
					delete(mp, t)
				} else {
					mp[t] = c - 1
				}
			} else {
				f, c := splitNum(t)
				if f < min_val {
					break
				}
				q = append(q, f, c)
				num_div++
			}
			//fmt.Printf("num_div: %d\n", num_div)
			//fmt.Printf("map -- %v\n", mp)
		}

		if len(mp) > 0 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
	}
}

func splitNum(v int) (int, int) {
	f := int(math.Floor(float64(v)/2))
	c := int(math.Ceil(float64(v)/2))
	return f, c
}

package main

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

// 1 2 3 4
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		sum := 0
		count := map[int]int{}
		for i := 0; i < n; i++ {
			num := 0
			fmt.Fscan(in, &num)
			sum += num
			count[num]++
		}
		list := []int{sum}
		if n == 1 {
			fmt.Fprintln(out, "YES")
			continue
		}
		for len(list) > 0 && len(list) < n {
			var temp []int
			for _, i := range list {
				var a, b int
				if i&1 == 1 {
					a, b = i/2, i/2+1
				} else {
					a, b = i/2, i/2
				}
				if count[a] > 0 {
					count[a]--
				} else {
					temp = append(temp, a)
				}
				if count[b] > 0 {
					count[b]--
				} else {
					temp = append(temp, b)
				}
			}
			list = temp
		}
		if len(list) == 0 {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

// YES
//NO
//YES
//YES
//NO
//YES
//NO
//YES
//YES
//YES
//YES
//NO
//NO
//YES


Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.The exam consists of $$$n$$$ problems that can be solved in $$$T$$$ minutes. Thus, the exam begins at time $$$0$$$ and ends at time $$$T$$$. Petya can leave the exam at any integer time from $$$0$$$ to $$$T$$$, inclusive.All problems are divided into two types:   easy problems — Petya takes exactly $$$a$$$ minutes to solve any easy problem;  hard problems — Petya takes exactly $$$b$$$ minutes ($$$b > a$$$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $$$x$$$, then it will be solved at time $$$x+a$$$. Similarly, if at a time $$$x$$$ Petya starts to solve a hard problem, then it will be solved at time $$$x+b$$$.For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $$$t_i$$$ ($$$0 \le t_i \le T$$$) at which it will become mandatory (required). If Petya leaves the exam at time $$$s$$$ and there is such a problem $$$i$$$ that $$$t_i \le s$$$ and he didn't solve it, then he will receive $$$0$$$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $$$t_i \le s$$$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $$$s$$$ Petya can have both "mandatory" and "non-mandatory" problems solved.For example, if $$$n=2$$$, $$$T=5$$$, $$$a=2$$$, $$$b=3$$$, the first problem is hard and $$$t_1=3$$$ and the second problem is easy and $$$t_2=2$$$. Then:  if he leaves at time $$$s=0$$$, then he will receive $$$0$$$ points since he will not have time to solve any problems;  if he leaves at time $$$s=1$$$, he will receive $$$0$$$ points since he will not have time to solve any problems;  if he leaves at time $$$s=2$$$, then he can get a $$$1$$$ point by solving the problem with the number $$$2$$$ (it must be solved in the range from $$$0$$$ to $$$2$$$);  if he leaves at time $$$s=3$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them;  if he leaves at time $$$s=4$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them;  if he leaves at time $$$s=5$$$, then he can get $$$2$$$ points by solving all problems. Thus, the answer to this test is $$$2$$$.Help Petya to determine the maximal number of points that he can receive, before leaving the exam.

The first line contains the integer $$$m$$$ ($$$1 \le m \le 10^4$$$) — the number of test cases in the test. The next lines contain a description of $$$m$$$ test cases.  The first line of each test case contains four integers $$$n, T, a, b$$$ ($$$2 \le n \le 2\cdot10^5$$$, $$$1 \le T \le 10^9$$$, $$$1 \le a < b \le 10^9$$$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $$$n$$$ numbers $$$0$$$ or $$$1$$$, separated by single space: the $$$i$$$-th number means the type of the $$$i$$$-th problem. A value of $$$0$$$ means that the problem is easy, and a value of $$$1$$$ that the problem is hard. The third line of each test case contains $$$n$$$ integers $$$t_i$$$ ($$$0 \le t_i \le T$$$), where the $$$i$$$-th number means the time at which the $$$i$$$-th problem will become mandatory. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2\cdot10^5$$$.

Print the answers to $$$m$$$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam.

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

func (a ByTime) Len() int           { return len(a) }
func (a ByTime) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByTime) Less(i, j int) bool { return a[i].time < a[j].time }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, T, a, b := 0, 0, 0, 0
		fmt.Fscanf(in, "%d %d %d %d\n", &n, &T, &a, &b)
		easy := 0
		hard := 0
		diff := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &diff[i])
			if diff[i] == 0 {
				easy++
			} else {
				hard++
			}
		}
		fmt.Fscanf(in, "\n")
		time := 0
		nodes := make([]node, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &time)
			nodes[i] = node{diff: diff[i], time: time}
		}
		fmt.Fscanf(in, "\n")
		sort.Sort(ByTime(nodes))
		ans := 0
		need := int64(0)
		num := 0
		//fmt.Println("debug", nodes)
		for len(nodes) > 0 {
			curTime := nodes[0].time
			if int64(curTime-1) >= need {
				ans = max(ans, num)
				remaining := int64(curTime-1) - need
				tmp := 0
				if remaining > 0 {
					if int64(easy)*int64(a) >= remaining {
						tmp += int(remaining) / a
					} else {
						tmp += easy
						remaining -= int64(easy) * int64(a)
						if int64(hard)*int64(b) >= remaining {
							tmp += int(remaining) / b
						} else {
							tmp += hard
						}
					}
					ans = max(ans, num+tmp)
				}
			}
			curCost := int64(0)
			for len(nodes) > 0 && nodes[0].time == curTime {
				curNode := nodes[0]
				nodes = nodes[1:]
				if curNode.diff == 0 {
					curCost += int64(a)
					easy--
				} else {
					curCost += int64(b)
					hard--
				}
				num++
			}
			need += curCost
		}
		if int64(T) >= need {
			ans = max(ans, num)
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}


Tokitsukaze has a permutation $$$p$$$ of length $$$n$$$. Recall that a permutation $$$p$$$ of length $$$n$$$ is a sequence $$$p_1, p_2, \ldots, p_n$$$ consisting of $$$n$$$ distinct integers, each of which from $$$1$$$ to $$$n$$$ ($$$1 \leq p_i \leq n$$$).She wants to know how many different indices tuples $$$[a,b,c,d]$$$ ($$$1 \leq a < b < c < d \leq n$$$) in this permutation satisfy the following two inequalities:  $$$p_a < p_c$$$ and $$$p_b > p_d$$$. Note that two tuples $$$[a_1,b_1,c_1,d_1]$$$ and $$$[a_2,b_2,c_2,d_2]$$$ are considered to be different if $$$a_1 \ne a_2$$$ or $$$b_1 \ne b_2$$$ or $$$c_1 \ne c_2$$$ or $$$d_1 \ne d_2$$$.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$4 \leq n \leq 5000$$$) — the length of permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$) — the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.

For each test case, print a single integer — the number of different $$$[a,b,c,d]$$$ tuples.

package main

import (
	"bufio"
	. "fmt"
	"os"
	// "strings"
	// "container/heap"
	// "bytes"
	// "math"
	// "strconv"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

var n,m int
var k int
var turn int
const MOD int64 = 1e9 + 7
var s,t string
type pair struct{node,weight int}
// calcMaxMatchLength:=func(s string)[]int{
// 	match:=make([]int,len(s))
// 	for i,c:=1,0;i<len(s);i++{
// 		v:=s[i]
// 		for c>0 && s[c]!=v{
// 			c=match[c-1]
// 		}
// 		if s[c]==v{
// 			c++
// 		}
// 	}
// 	return match
// }
// kmpSearch:=func(text,pattern string)(pos []int){
// 	match:=calcMaxMatchLength(pattern)

// }
func solve(){
	Fscan(r, &n)
	a := make([]int, n)
	l := make([][]int, n)
	for i := range a {
		Fscan(r, &a[i])
		l[i] = make([]int, n+1)
		s:=0
		for j, v := range a[:i] {
			if v < a[i] {
				s++
			}
			l[i][j+1] = s
		}
	}
	ans := int64(0)
	for i := 1; i < n-2; i++ {
		for j, c := n-2, 0; j > i; j-- {
			if a[i] > a[j+1] {
				c++
			}
			ans += int64(l[j][i] * c)
		}
	}
	Fprintln(w, ans)

	return
}

func main(){
	defer w.Flush()
	// Fscan(r,&n,&m)
	// Fscan(r,&n)
	Fscan(r,&turn)
	// Fscan(r,&s)
	// Fscan(r,&k)

	for i:=0;i<turn;i++{
		solve()
	}

	// solve()
}

/* ===========================PART 1: Math Func ============================  */
func max(x, y int) int {if x > y {return x};return y}
func abs(x int64) int64 {if x > 0 {return x};return -x}
func min(x, y int) int {if x < y {return x};return y}
func memset(a []int, v int) {
    if len(a) == 0 {return}
    a[0] = v
    for bp := 1; bp < len(a); bp *= 2 {
        copy(a[bp:], a[:bp])
    }
}

/* ===========================PART 2: Heap func ============================  */
// type hp struct{ sort.IntSlice }
// func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
// func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
// func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// /* ===========================PART 3: String func ============================  */
// func reverse(a []byte){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 4: Array func ============================  */
// func reverse(a []int){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 5: Hash func ============================  */
// func containKey(m map[int]int, key int) bool {
// 	if _, ok := m[key]; ok {
// 		return true
// 	}
// 	return false
// }

package main

import (
	"bufio"
	. "fmt"
	"os"
	// "strings"
	// "container/heap"
	// "bytes"
	// "math"
	// "strconv"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

var n,m int
var k int
var turn int
const MOD int64 = 1e9 + 7
var s,t string
type pair struct{node,weight int}
// calcMaxMatchLength:=func(s string)[]int{
// 	match:=make([]int,len(s))
// 	for i,c:=1,0;i<len(s);i++{
// 		v:=s[i]
// 		for c>0 && s[c]!=v{
// 			c=match[c-1]
// 		}
// 		if s[c]==v{
// 			c++
// 		}
// 	}
// 	return match
// }
// kmpSearch:=func(text,pattern string)(pos []int){
// 	match:=calcMaxMatchLength(pattern)

// }
func solve(){
	Fscan(r, &n)
	a := make([]int, n)
	l := make([][]int, n)
	for i := range a {
		Fscan(r, &a[i])
		l[i] = make([]int, n+1)
		for j, v := range a[:i] {
			l[i][j+1] = l[i][j]
			if v < a[i] {
				l[i][j+1]++
			}
		}
	}
	ans := int64(0)
	for i := 1; i < n-2; i++ {
		for j, c := n-2, 0; j > i; j-- {
			if a[i] > a[j+1] {
				c++
			}
			ans += int64(l[j][i] * c)
		}
	}
	Fprintln(w, ans)

	return
}

func main(){
	defer w.Flush()
	// Fscan(r,&n,&m)
	// Fscan(r,&n)
	Fscan(r,&turn)
	// Fscan(r,&s)
	// Fscan(r,&k)

	for i:=0;i<turn;i++{
		solve()
	}

	// solve()
}

/* ===========================PART 1: Math Func ============================  */
func max(x, y int) int {if x > y {return x};return y}
func abs(x int64) int64 {if x > 0 {return x};return -x}
func min(x, y int) int {if x < y {return x};return y}
func memset(a []int, v int) {
    if len(a) == 0 {return}
    a[0] = v
    for bp := 1; bp < len(a); bp *= 2 {
        copy(a[bp:], a[:bp])
    }
}

/* ===========================PART 2: Heap func ============================  */
// type hp struct{ sort.IntSlice }
// func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
// func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
// func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// /* ===========================PART 3: String func ============================  */
// func reverse(a []byte){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 4: Array func ============================  */
// func reverse(a []int){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 5: Hash func ============================  */
// func containKey(m map[int]int, key int) bool {
// 	if _, ok := m[key]; ok {
// 		return true
// 	}
// 	return false
// }

package main

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

var T, N int16
var P [5005]int16
var SB, SA [][]int16

func solve() int64 {
	SB = make([][]int16, N)
	SA = make([][]int16, N)
	for i := int16(0); i<N; i++ {
		SB[i] = make([]int16, N + 1)
		SA[i] = make([]int16, N + 1)
	}

	for i := int16(1); i < N; i++ {
		for j := int16(1); j<=N; j++ {
			SB[i][j] = SB[i-1][j]
		}
		for a := P[i-1] + 1; a <= N; a++ {
			SB[i][a]++
		}
	}

	for i := int16(N-2); i >= 0; i-- {
		for j := int16(1); j<=N; j++ {
			SA[i][j] = SA[i+1][j]
		}
		for a := P[i+1] + 1; a <= N; a++ {
			SA[i][a]++
		}
	}

	var res int64
	for i := int16(0); i < N; i++ {
		a := P[i]
		for j := i + 1; j < N; j++ {
			b := P[j]

			//fmt.Println(i, j, a, b, SB[i][b], SA[j][a])

			res += int64(SB[i][b]) * int64(SA[j][a])
		}
	}

	return res
}

func main(){
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	for T > 0 {
		fmt.Fscan(in,  &N)
		for i:=int16(0); i<N; i++ {
			fmt.Fscan(in, &P[i])
		}
		fmt.Println(solve())
		T--
	}
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ;t > 0;t-- {
		var n int64
		fmt.Fscan(in,&n)
		arr := make([]int64,n+1)
		f := make([]int64,n+1)
		for i := int64(1);i <= n;i++ {
			fmt.Fscan(in,&arr[i])
		}
		for i := int64(1);i <= n;i++ {
			for j := i+1;j <= n;j++ {
				if arr[i] > arr[j] {
					f[i]++
				}
			}
		}
		sum := make([]int64,n+1)
		//fmt.Println(f)
		res := int64(0)
		for i := int64(1);i <= n;i++ {
			for j := int64(1);j < i;j++  {
				if arr[j] > arr[i] {
					f[j] --
				}
			}
			sum[0] = 0
			for k := int64(1);k <= n;k++ {
				sum[k] = sum[k-1]+f[k]
			}
			for j := int64(1);j < i;j++ {
				if arr[j] < arr[i] {
					res += sum[i-1] - sum[j]
				}
			}

		}
		fmt.Fprintln(out,res)
	}
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		l := make([][]int, n)
		for i := range a {
			Fscan(in, &a[i])
			l[i] = make([]int, n+1)
			for j, v := range a[:i] {
				l[i][j+1] = l[i][j]
				if v < a[i] {
					l[i][j+1]++
				}
			}
		}
		ans := int64(0)
		for i := 1; i < n-2; i++ {
			for j, c := n-2, 0; j > i; j-- {
				if a[i] > a[j+1] {
					c++
				}
				ans += int64(l[j][i] * c)
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		l := make([][]int, n)
		r := make([][]int, n)
		for i := range a {
			Fscan(in, &a[i])
			l[i] = make([]int, n+1)
			for j, v := range a[:i] {
				l[i][j+1] = l[i][j]
				if v < a[i] {
					l[i][j+1]++
				}
			}
			r[i] = make([]int, n+1)
		}
		for i := n - 2; i > 0; i-- {
			for j := n - 1; j > i; j-- {
				r[i][j] = r[i][j+1]
				if a[i] > a[j] {
					r[i][j]++
				}
			}
		}
		ans := int64(0)
		for i := 1; i < n-2; i++ {
			for j := i + 1; j < n-1; j++ {
				ans += int64(l[j][i] * r[i][j+1])
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			arr[i] = v
		}

		var tot int64
		g := make([]int64, n)
		for i := n - 1; i >= 0; i-- {
			var c int64
			for j := i - 1; j >= 0; j-- {
				if arr[j] < arr[i] {
					tot += c
				}
				c += g[j]
				if arr[j] > arr[i] {
					g[j]++
				}
			}
		}

		fmt.Fprintln(out, tot)
	}
}

package main

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

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

	var t, n int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		Fscan(in, &n)

		A := make([]int64, n+10)
		cnt := make([]int64, n+10)

		for i := 1; i <= n; i++ {
			Fscan(in, &A[i])
		}

		var ans int64

		for b := 1; b <= n; b++ {
			var v int64
			for c := n; c > b; c-- {
				ans += cnt[c] * v
				if A[c] < A[b] {
					v++
				}
			}

			a := b
			for c := b + 1; c < n; c++ {
				if A[a] < A[c] {
					cnt[c]++
				}

			}

		}

		Fprintln(out, ans)

	}

}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

func update(x int, k int, f []int) {
	for x < len(f) {
		f[x] += k
		x += x & -x
	}
}

func get(x int, f []int) Int {
	res := Int(0)
	for x > 0 {
		res += Int(f[x])
		x -= x & -x
	}
	return res
}

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

	var t, n int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		Fscan(in, &n)

		A := make([]Int, n+10)
		fb := make([]Int, n+10)
		preFb := make([]Int, n+10)

		for i := 1; i <= n; i++ {
			Fscan(in, &A[i])
		}

		for i := 1; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				if A[j] < A[i] {
					fb[i]++
				}
			}
		}

		ans := L0
		for c := 1; c <= n; c++ {
			for b := 1; b < c; b++ {
				if A[b] > A[c] {
					fb[b]--
				}
			}
			preFb[0] = 0
			for i := 1; i < c; i++ {
				preFb[i] = preFb[i-1] + fb[i]

			}
			for a := 1; a < c; a++ {
				if A[a] < A[c] {
					ans += preFb[c-1] - preFb[a]
				}
			}
		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]int {
	index := make([][]int, h)
	data := make([]int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); P := gis(N); for i:=0;i<N;i++ { P[i]-- }
		ansarr := make([]int,N*N)
		for b:=1;b<=N-3;b++ {
			pb := P[b]
			numg := 0
			if P[N-1] < pb { numg++ }
			for c:=N-2;c>b;c-- {
				id := N*b+c
				ansarr[id] = numg
				if P[c] < pb { numg++ }
			}
		}
		for c:= N-2;c>=2;c-- {
			pc := P[c]
			numg := 0
			if P[0] < pc { numg++ }
			for b:=1;b<c;b++ {
				id := N*b+c
				ansarr[id] *= numg
				if P[b] < pc { numg++ }
			}
		}
		ans := int64(0); for _,a := range ansarr { ans += int64(a) }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	p := make([]int, n)
	// cnt[i][j]前i个位置中，小于j的个数
	cnt := make([][]int64, n)

	for i := range p {
		Fscan(in, &p[i])
		p[i]--
		cnt[i] = make([]int64, n)
		for j := range cnt[i] {
			cnt[i][j] = 0
			if i > 0 {
				cnt[i][j] = cnt[i-1][j]
			}

			if p[i] < j {
				cnt[i][j]++
			}
		}
	}
	var ans int64 = 0
	for b := 1; b < n-2; b++ {
		for c := b + 1; c < n-1; c++ {
			ans += cnt[b-1][p[c]] * (cnt[n-1][p[b]] - cnt[c][p[b]])
		}
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

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

type Int int64

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

/*
* fB[i] is number of pb>pd pairs in [i+1, n]
 */

func solve() {
	var n int
	fmt.Fscan(in, &n)
	A := make([]int, n+1)
	fb := make([]int, n+1)
	pre := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &A[i])
	}
	for b := 1; b <= n; b++ {
		for d := b + 1; d <= n; d++ {
			if A[b] > A[d] {
				fb[b]++
			}
		}
	}
	var ans Int
	for c := 1; c <= n; c++ {
		for b := 1; b < c; b++ {
			if A[b] > A[c] {
				fb[b]--
			}
		}
		pre[0] = 0
		for i := 1; i < c; i++ {
			pre[i] = pre[i-1] + fb[i]
		}
		for a := 1; a < c; a++ {
			if A[a] < A[c] {
				ans += (Int)(pre[c-1] - pre[a])
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
}
func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for tt := 0; tt < t; tt++ {
		solve()
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	for t := io.ReadInt(); t > 0; t-- {
		n := io.ReadInt()
		m := io.ReadIntSlice(n)

		suf := make([][]int16, len(m))
		for i := range m {
			suf[i] = make([]int16, len(m))
			for j := len(m) - 1; j > i; j-- {
				if m[i] > m[j] {
					suf[i][j]++
				}
				if j != len(m)-1 {
					suf[i][j] += suf[i][j+1]
				}
			}
		}

		pref := make([][]int16, len(m))
		for i := range m {
			pref[i] = make([]int16, len(m))
			for j := 0; j < i; j++ {
				if m[j] > m[i] {
					pref[i][j]++
				}
				if j != 0 {
					pref[i][j] += pref[i][j-1]
				}
			}
		}

		//for i := range suf {
		//	fmt.Println(suf[i])
		//}

		//for i := range pref {
		//	fmt.Println(pref[i])
		//}

		var answer int64
		var current int64

		for a := 0; a < len(m)-3; a++ {
			for c := a + 2; c < len(m)-1; c++ {
				current += int64(suf[c-1][c+1])
				current -= int64(pref[c][c-2])
				current += int64(pref[c][a])
				//fmt.Println(a, c, current, suf[c-1][c+1], pref[c][c-2]+pref[c][a])
				if m[a] < m[c] {
					answer += current
				}
			}
			current = 0
		}

		fmt.Println(answer)
	}
}

package main

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

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	var arr1 []int
	var arr2 []int

	count := func(_arr []int, v int) int {
		ret := 0
		for ; v > 0; v -= v & -v {
			ret += _arr[v]
		}
		return ret
	}

	add := func(_arr []int, v, n int) {
		for ; v <= n; v += v & -v {
			_arr[v] += 1
		}
	}
	sol := func() {
		n := myio.ReadInt()
		var sum int64 = 0
		ar := make([]int, n)
		for i := 0; i < n; i++ {
			ar[i] = myio.ReadInt()
		}
		arr1 = make([]int, n+1)
		for b := 0; b < n; b++ {
			arr2 = make([]int, n+1)
			for c := n - 1; c > b; c-- {
				nc := count(arr1, ar[c]-1)
				nb := count(arr2, ar[b]-1)
				sum += (int64)(nc * nb)
				// fmt.Printf("(b=%d,c=%d, nc=%d,nb=%d) sum=%d\n", b, c, nc, nb, sum)
				add(arr2, ar[c], n)
			}
			add(arr1, ar[b], n)
		}
		// fmt.Println("----")
		myio.Println(sum)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
	// "strings"
	// "container/heap"
	// "bytes"
	// "math"
	// "strconv"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

var n,m int
var k int
var turn int
const MOD int64 = 1e9 + 7
var s,t string
type pair struct{node,weight int}
// calcMaxMatchLength:=func(s string)[]int{
// 	match:=make([]int,len(s))
// 	for i,c:=1,0;i<len(s);i++{
// 		v:=s[i]
// 		for c>0 && s[c]!=v{
// 			c=match[c-1]
// 		}
// 		if s[c]==v{
// 			c++
// 		}
// 	}
// 	return match
// }
// kmpSearch:=func(text,pattern string)(pos []int){
// 	match:=calcMaxMatchLength(pattern)

// }
func solve(){
	a := make([]int, n)
	l := make([][]int, n)
	for i := range a {
		Fscan(r, &a[i])
		l[i] = make([]int, n+1)
		for j, v := range a[:i] {
			l[i][j+1] = l[i][j]
			if v < a[i] {
				l[i][j+1]++
			}
		}
	}
	ans := int64(0)
	for i := 1; i < n-2; i++ {
		for j, c := n-2, 0; j > i; j-- {
			if a[i] > a[j+1] {
				c++
			}
			ans += int64(l[j][i] * c)
		}
	}
	Fprintln(w, ans)

	return
}

func main(){
	defer w.Flush()
	// Fscan(r,&n,&m)
	// Fscan(r,&n)
	Fscan(r,&turn)
	// Fscan(r,&s)
	// Fscan(r,&k)

	for i:=0;i<turn;i++{
		solve()
	}

	// solve()
}

/* ===========================PART 1: Math Func ============================  */
func max(x, y int) int {if x > y {return x};return y}
func abs(x int64) int64 {if x > 0 {return x};return -x}
func min(x, y int) int {if x < y {return x};return y}
func memset(a []int, v int) {
    if len(a) == 0 {return}
    a[0] = v
    for bp := 1; bp < len(a); bp *= 2 {
        copy(a[bp:], a[:bp])
    }
}

/* ===========================PART 2: Heap func ============================  */
// type hp struct{ sort.IntSlice }
// func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
// func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
// func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// /* ===========================PART 3: String func ============================  */
// func reverse(a []byte){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 4: Array func ============================  */
// func reverse(a []int){
// 	for i,n :=0,len(a);i<n/2;i++{
// 		a[i],a[n-1-i]=a[n-1-i],a[i]
// 	}
// }

// /* ===========================PART 5: Hash func ============================  */
// func containKey(m map[int]int, key int) bool {
// 	if _, ok := m[key]; ok {
// 		return true
// 	}
// 	return false
// }

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ;t > 0;t-- {
		var n int
		fmt.Fscan(in,&n)
		arr := make([]int,n+1)
		f := make([]int,n+1)
		for i := 1;i <= n;i++ {
			fmt.Fscan(in,&arr[i])
		}
		for i := 1;i <= n;i++ {
			for j := i+1;j <= n;j++ {
				if arr[i] > arr[j] {
					f[i]++
				}
			}
		}
		sum := make([]int,n+1)
		//fmt.Println(f)
		res := 0
		for i := 1;i <= n;i++ {
			for j := 1;j < i;j++  {
				if arr[j] > arr[i] {
					f[j] --
				}
			}
			sum[0] = 0
			for k := 1;k <= n;k++ {
				sum[k] = sum[k-1]+f[k]
			}
			for j := 1;j < i;j++ {
				if arr[j] < arr[i] {
					res += sum[i-1] - sum[j]
				}
			}

		}
		fmt.Fprintln(out,res)
	}
}

package main

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

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

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			arr[i] = v
		}

		var tot int
		g := make([]int, n)
		for i := n - 1; i >= 0; i-- {
			var c int
			for j := i - 1; j >= 0; j-- {
				if arr[j] < arr[i] {
					tot += c
				}
				c += g[j]
				if arr[j] > arr[i] {
					g[j]++
				}
			}
		}

		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); P := gis(N); for i:=0;i<N;i++ { P[i]-- }
		ansarr := make([]int,N*N)
		for b:=1;b<=N-3;b++ {
			pb := P[b]
			numg := 0
			if P[N-1] < pb { numg++ }
			for c:=N-2;c>b;c-- {
				id := N*b+c
				ansarr[id] = numg
				if P[c] < pb { numg++ }
			}
		}
		for c:= N-2;c>=2;c-- {
			pc := P[c]
			numg := 0
			if P[0] < pc { numg++ }
			for b:=1;b<c;b++ {
				id := N*b+c
				ansarr[id] *= numg
				if P[b] < pc { numg++ }
			}
		}
		ans := sumarr(ansarr)
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	p := make([]int, n)
	// cnt[i][j]前i个位置中，小于j的个数
	cnt := make([][]int, n)

	for i := range p {
		Fscan(in, &p[i])
		p[i]--
		cnt[i] = make([]int, n)
		for j := range cnt[i] {
			cnt[i][j] = 0
			if i > 0 {
				cnt[i][j] = cnt[i-1][j]
			}

			if p[i] < j {
				cnt[i][j]++
			}
		}
	}
	ans := 0
	for b := 1; b < n-2; b++ {
		for c := b + 1; c < n-1; c++ {
			ans += cnt[b-1][p[c]] * (cnt[n-1][p[b]] - cnt[c][p[b]])
		}
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

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

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

/*
* fB[i] is number of pb>pd pairs in [i+1, n]
 */

func solve() {
	var n int
	fmt.Fscan(in, &n)
	A := make([]int, n+1)
	fb := make([]int, n+1)
	pre := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &A[i])
	}
	for b := 1; b <= n; b++ {
		for d := b + 1; d <= n; d++ {
			if A[b] > A[d] {
				fb[b]++
			}
		}
	}
	ans := 0
	for c := 1; c <= n; c++ {
		for b := 1; b < c; b++ {
			if A[b] > A[c] {
				fb[b]--
			}
		}
		pre[0] = 0
		for i := 1; i < c; i++ {
			pre[i] = pre[i-1] + fb[i]
		}
		for a := 1; a < c; a++ {
			if A[a] < A[c] {
				ans += (pre[c-1] - pre[a])
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
}
func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for tt := 0; tt < t; tt++ {
		solve()
	}
}