package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n, x := ScanInt(), ScanInt64()
		a := make([]int64, n)
		c := make([]int64, n)
		var res int64

		for i := range a {
			a[i] = ScanInt64()
			c[i] = 1
		}

		for i := 0; a[i]%x == 0; i++ {
			a = append(a, a[i]/x)
			c = append(c, c[i]*x)
		}

		for i := range a {
			res += a[i] * c[i]
		}

		fmt.Println(res)
	}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(input, &n, &x)

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			pos := 0
			for {
				if y[pos].key%x == 0 {
					yy := Age{}
					yy.key = y[pos].key / x
					yy.cnt = x * y[pos].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		y = y[0:0]

		fmt.Println(ans)
	}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(input, &n, &x)

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			/*l := len(y)
			for i := 0; i < l; i++ {
				if y[i].key%x == 0 {
					yy := Age{}
					yy.key = y[i].key / x
					yy.cnt = x * y[i].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					l++
				} else {
					break
				}
			}*/

			pos := 0
			for {
				if y[pos].key%x == 0 {
					yy := Age{}
					yy.key = y[pos].key / x
					yy.cnt = x * y[pos].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(input, &n, &x)

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			l := len(y)
			for i := 0; i < l; i++ {
				if y[i].key%x == 0 {
					yy := Age{}
					yy.key = y[i].key / x
					yy.cnt = x * y[i].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					l++
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

/*
5 2
8 16 84 58 17
349
*/

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
	x   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n, &x)
		a := make([]int, n)

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

		fmt.Fprintln(out, solve3(n, x, a))

	}

}

func solve3(n int, x int, a []int) int64 {

	flag := false

	//把原本的加起來
	var SUM int64
	for m := 0; m < n; m++ {
		SUM += int64(a[m])
	}

	q := x
	for !flag {

		for i := 0; i < len(a); i++ {
			if a[i]%q == 0 {
				SUM += int64(a[i])
			} else {
				flag = true
				break
			}
		}

		q *= x

	}

	return SUM

}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

func solve2(n int, x int, a []int) int64 {

	sum := int64(0)
	index := 0

	for a[index]%x == 0 {

		for i := 0; i < x; i++ {
			a = append(a, a[index]/x)
		}

		index++

	}

	//printArr(a)

	for i := range a {
		sum += int64(a[i])
	}

	return sum
}

func solve(n int, x int, a []int) int64 {

	sum := int64(0)

	for i := range a {

		num := int64(a[i])
		sum += int64(a[i])

		fmt.Println(sum)

		for a[i]%x == 0 {
			sum += num
			a[i] /= x
			fmt.Println(sum)
		}

	}

	return sum
}

// https://codeforces.com/contest/1472/problem/E

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}
var (
    x int64 // min of stop
    m map[int]int
)

func solve() {
    var n int64
    fmt.Fscan(input, &n, &x)
    a := make([]int64, n)
    for i := 0; i < len(a); i++ {
        fmt.Fscan(input, &a[i])
    }

    fmt.Fprintln(output, calc(1, a))
}

// returns stop movie
func calc(pow int64, a []int64) int64 {
    if len(a) == 0 { return 0 }

    sum := int64(0)
    for i := 0; i < len(a); i++ {
        sum += a[i] * pow
    }

    anew := make([]int64, 0)
    for i := 0; i < len(a); i++ {
        if a[i] % x != 0 {
            for _, v := range anew {
                sum += v * pow * x
            }
            return sum
        }
        anew = append(anew, a[i] / x)
    }

    return sum + calc(pow * x, anew)
}

package main

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

/*

 */

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

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

	for ; t > 0; t-- {
		var n, x int64
		scanf("%d %d\n", &n, &x)

		type pair struct {
			num int64
			cnt int64
		}

		a := make([]pair, n)

		for i := range a {
			var num int64
			scanf("%d", &num)

			a[i] = pair{num, 1}
		}
		scanf("\n")

		for i := 0; i < len(a); i++ {
			p := a[i]

			if p.num%x == 0 {
				a = append(a, pair{p.num / x, x * p.cnt})
			} else {
				break
			}
		}

		sum := int64(0)
		for _, p := range a {
			sum += p.num * p.cnt
		}

		fmt.Fprintln(writer, sum)
	}
}

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 divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	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"
	"fmt"
	"os"
)

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1e6), 1e6)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

// [12] x=2
// => 12, 6, 6, 3 (stop), 3, 3, 3
// => 36

// [4, 6, 8, 2] x=2
// => 4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1 ...
func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n, x := sint64(), sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, x, a))
	}
}

func sol(n, x int64, a []int64) int64 {
	deg := make([]int64, n)
	for i := range a {
		tmp := a[i]
		d := int64(0)
		for tmp > 0 {
			if tmp%x != 0 {
				break
			}
			tmp /= x
			d++
		}
		deg[i] = d
	}
	minDeg := min(deg)
	ret := sum(a) * (minDeg + 1)
	for i := range a {
		if deg[i] > minDeg {
			ret += a[i]
		} else {
			break
		}
	}
	return ret
}

func min(deg []int64) int64 {
	ret := deg[0]
	for _, d := range deg {
		if d < ret {
			ret = d
		}
	}
	return ret
}

func sum(a []int64) int64 {
	ret := int64(0)
	for _, num := range a {
		ret += num
	}
	return ret
}

package main

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

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

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		s := int64(0)
		Fscan(in, &n, &x)
		a := make([]int, n)
		c := make([]int8, n)
		mi := 0
		for i := range a {
			Fscan(in, &a[i])
			v := a[i]
			for s += int64(v); v%x == 0; v /= x {
				c[i]++
			}
			if c[i] < c[mi] {
				mi = i
			}
		}
		s *= int64(c[mi] + 1)
		for _, v := range a[:mi] {
			s += int64(v)
		}
		Fprintln(out, s)
	}
}

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

package main

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

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

	var t, n int
	var x int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &x)
		a := make([]int64, n)

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

		multiplier := int64(1)
		for i := 0; i < n; i++ {
			if a[i]%x != 0 {
				break
			}
			sum += a[i] * multiplier
			a[i] = a[i] / x
			if i == n-1 {
				i = -1
				multiplier *= x
			}
		}

		fmt.Println(sum)
	}
}

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

func main() {
var input io.Reader = bufio.NewReader(os.Stdin)

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

for o:=0;o<t;o++{
var n, x int
fmt.Fscan(input, &n)
fmt.Fscan(input, &x)

ARRAY := make([]int, n)

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

b := 7

var SUM int64
for m :=0; m< n; m++{
SUM = SUM + int64(ARRAY[m])
}

q := x
for l :=0 ;b > 6;{
l = l +1
M :=0
for i :=0; i<len(ARRAY); i++{
if ARRAY[i] % q == 0{
SUM = SUM + int64(ARRAY[i])
M = M + 1
}else{
b = 5
break
}
}
if M == n{
q = q * x
M = 0
}else{
b = 5
break
}

}

fmt.Println(SUM)

}
}

package main

import (

"bufio"

"fmt"

"io"

"os"

)

func main() {

var input io.Reader = bufio.NewReader(os.Stdin)

var t int

fmt.Fscan(input, &t)

for o:=0;o<t;o++{

var n, x int

fmt.Fscan(input, &n)

fmt.Fscan(input, &x)

ARRAY := make([]int, n)

for i:=0; i<n;i++{

fmt.Fscan(input, &ARRAY[i])}

b := 7

var SUM int64

for m :=0; m< n; m++{

SUM = SUM + int64(ARRAY[m])
}

q := x

for l :=0 ;b > 6;{
l = l +1
M :=0

for i :=0; i<len(ARRAY); i++{

if ARRAY[i] % q == 0{

SUM = SUM + int64(ARRAY[i])

M = M + 1

}

}

if M == n{

q = q * x

}else{

b = 5

break

}

}

fmt.Println(SUM)

}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(input, &n, &x)

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		ff := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				if yy.key%x != 0 {
					ff = false
				}

				ans += int64(yy.key * yy.cnt)

				if ff {
					y = append(y, yy)
				}
			} else {
				ok = false
				break
			}
		}

		if ok {
			for {

				if len(y) == 0 {
					break
				}

				if y[0].key%x == 0 {
					yy := Age{}
					yy.key = y[0].key / x
					yy.cnt = x * y[0].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
	x   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		var a []int
		flag := false
		sum := int64(0)
		fmt.Fscan(in, &n, &x)

		//輸入資料
		for i := 0; i < n; i++ {
			temp := 0
			fmt.Fscan(in, &temp)

			sum += int64(temp)

			if temp%x != 0 {
				flag = true
			}

			if !flag {
				for i := 0; i < x; i++ {
					a = append(a, temp/x)
				}
			}

		}

		var b []int
		var t []int

		//擴展資料
		for !flag {

			b = t

			for i := range a {

				sum += int64(a[i])

				if a[i]%x != 0 {
					flag = true
				}

				if !flag {
					for j := 0; j < x; j++ {
						b = append(b, a[i]/x)
					}
				}

			}

			a = b

		}

		//最後面
		for i := range b {
			sum += int64(b[i])
		}

		fmt.Fprintln(out, sum)

	}

}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

func solve2(n int, x int, a []int) int64 {

	sum := int64(0)
	index := 0

	for a[index]%x == 0 {

		for i := 0; i < x; i++ {
			a = append(a, a[index]/x)
		}

		index++

	}

	//printArr(a)

	for i := range a {
		sum += int64(a[i])
	}

	return sum
}

func solve(n int, x int, a []int) int64 {

	sum := int64(0)

	for i := range a {

		num := int64(a[i])
		sum += int64(a[i])

		fmt.Println(sum)

		for a[i]%x == 0 {
			sum += num
			a[i] /= x
			fmt.Println(sum)
		}

	}

	return sum
}

// https://codeforces.com/contest/1472/problem/E

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}
var (
    x int64 // min of stop
    m map[int]int
)

func solve() {
    var n int64
    fmt.Fscan(input, &n, &x)
    a := make([]int64, n)
    for i := 0; i < len(a); i++ {
        fmt.Fscan(input, &a[i])
    }

    fmt.Fprintln(output, calc(1, a))
}

// returns stop movie
func calc(pow int64, a []int64) int64 {
    if len(a) == 0 { return 0 }

    sum := int64(0)
    for i := 0; i < len(a); i++ {
        sum += a[i] * pow
    }

    anew := make([]int64, 0)
    for i := 0; i < len(a); i++ {
        if a[i] % x != 0 { break }
        anew = append(anew, a[i] / x)
    }

    return sum + calc(pow * x, anew)
}


package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var outbuff = make([]byte, 20)

func read_int() (x int64) {
	var ch byte
	for ch < '0' || ch > '9' {
		ch, _ = in.ReadByte()
	}
	x = int64(ch - '0')
	for {
		ch, _ = in.ReadByte()
		if ch >= '0' && ch <= '9' {
			x = x*10 + int64(ch-'0')
		} else {
			break
		}
	}
	return
}

func write_int(x int64, end byte) {
	// if x == 0 {
	// 	out.WriteByte('0')
	// } else {
	// 	i := 0
	// 	for ; x > 0; i++ {
	// 		outbuff[i] = byte(x%10 + '0')
	// 		x /= 10
	// 	}
	// 	for i--; i >= 0; i-- {
	// 		out.WriteByte(outbuff[i])
	// 	}
	// }
	out.WriteString(strconv.FormatInt(x, 10))
	out.WriteByte(end)
	out.Flush()
}

func main() {
	n := read_int()
	a := make([]int64, n)
	m := make([]map[int64]bool, 40)
	for i := range a {
		a[i] = read_int()
	}
	var sx, sy int64
	for _, v := range a {
		sx ^= v
	}
	for lg := 39; lg >= 0; lg-- {
		m[lg] = make(map[int64]bool)
		m[lg][0] = true
	}
	var mx int64
	for i := n - 1; i >= -1; i-- {
		var z int64
		for lg := uint64(39); lg < 40; lg-- {
			if (1<<lg)&sx == 0 {
				z += 1 << lg
				if !m[lg][z] {
					z -= 1 << lg
				}
			} else {
				if !m[lg][z] {
					z += 1 << lg
				}
			}
		}
		if z^sx > mx {
			mx = z ^ sx
		}
		if i >= 0 {
			sx ^= a[i]
			sy ^= a[i]
			for lg := uint64(39); lg < 40; lg-- {
				m[lg][(sy>>lg)<<lg] = true
			}
		}
	}
	write_int(mx, '\n')
}

package main

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

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

func read_int() (x int64) {
	var ch byte
	for ch < '0' || ch > '9' {
		ch, _ = in.ReadByte()
	}
	x = int64(ch - '0')
	for {
		ch, _ = in.ReadByte()
		if ch >= '0' && ch <= '9' {
			x = x*10 + int64(ch-'0')
		} else {
			break
		}
	}
	return
}

func write_int(x int64, end byte) {
	out.WriteString(strconv.FormatInt(x, 10))
	out.WriteByte(end)
	out.Flush()
}

func main() {
	n := read_int()
	a := make([]int64, n)
	m := make([]map[int64]bool, 40)
	for i := range a {
		a[i] = read_int()
	}
	var sx, sy int64
	for _, v := range a {
		sx ^= v
	}
	for lg := 39; lg >= 0; lg-- {
		m[lg] = make(map[int64]bool)
		m[lg][0] = true
	}
	var mx int64
	for i := n - 1; i >= -1; i-- {
		var z int64
		for lgi := 39; lgi >= 0; lgi-- {
			lg := uint64(lgi)
			if (1<<lg)&sx == 0 {
				z += 1 << lg
				if !m[lg][z] {
					z -= 1 << lg
				}
			} else {
				if !m[lg][z] {
					z += 1 << lg
				}
			}
		}
		if z^sx > mx {
			mx = z ^ sx
		}
		if i >= 0 {
			sx ^= a[i]
			sy ^= a[i]
			for lgi := 39; lgi >= 0; lgi-- {
				lg := uint64(lgi)
				m[lg][(sy>>lg)<<lg] = true
			}
		}
	}
	write_int(mx, '\n')
}

package main

import (
	"bufio"
	"os"

//	"strconv"
)

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var outbuff = make([]byte, 20)

func read_int() (x int64) {
	var ch byte
	for ch < '0' || ch > '9' {
		ch, _ = in.ReadByte()
	}
	x = int64(ch - '0')
	for {
		ch, _ = in.ReadByte()
		if ch >= '0' && ch <= '9' {
			x = x*10 + int64(ch-'0')
		} else {
			break
		}
	}
	return
}

func write_int(x int64, end byte) {
	if x == 0 {
		out.WriteByte('0')
	} else {
		i := 0
		for ; x > 0; i++ {
			outbuff[i] = byte(x%10 + '0')
			x /= 10
		}
		for i--; i >= 0; i-- {
			out.WriteByte(outbuff[i])
		}
	}
	//out.WriteString(strconv.FormatInt(x, 10))
	out.WriteByte(end)
	out.Flush()
}

func main() {
	n := read_int()
	a := make([]int64, n)
	m := make([]map[int64]bool, 40)
	for i := range a {
		a[i] = read_int()
	}
	var sx, sy int64
	for _, v := range a {
		sx ^= v
	}
	for lg := 39; lg >= 0; lg-- {
		m[lg] = make(map[int64]bool)
		m[lg][0] = true
	}
	var mx int64
	for i := n - 1; i >= -1; i-- {
		var z int64
		for lg := uint64(39); lg < 40; lg-- {
			if (1<<lg)&sx == 0 {
				z += 1 << lg
				if !m[lg][z] {
					z -= 1 << lg
				}
			} else {
				if !m[lg][z] {
					z += 1 << lg
				}
			}
		}
		if z^sx > mx {
			mx = z ^ sx
		}
		if i >= 0 {
			sx ^= a[i]
			sy ^= a[i]
			for lg := uint64(39); lg < 40; lg-- {
				m[lg][(sy>>lg)<<lg] = true
			}
		}
	}
	write_int(mx, '\n')
}


package main

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

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		n := 0
		Fscan(in,&n)
		aa := make([]int,n)
		a:= make([]int,2*n)
		b:= make([]int,2*n)

		fl := true
		for i:=0;i<2*n;i++{
			Fscan(in,&a[i])
			a[i]--
			if aa[a[i]]<2{
				b[2*a[i]+aa[a[i]]]=i
			} else {
				fl=false
			}
			aa[a[i]]++
		}

		if !fl{
			Fprintln(out,-1)
			continue
		}

		oth:= make([]int,2*n)
		for i:=0;i<n;i++{
			oth[b[2*i]]=b[2*i+1]
			oth[b[2*i+1]]=b[2*i]
		}

		// Fprintln(out,a)
		// Fprintln(out,oth)
		rvt := make([]bool,n)
		vis := make([]bool,n)

		run1 := func(start int){
			if start+n == oth[start]{
				return
			}
			// Fprintln(out,"st",start)
			f:=0
			cur := start+n
			vis[cur%n]=true
			aa := make([][] int,2)
			aa[f]=append(aa[f],start)
			aa[f]=append(aa[f],cur)
			// Fprintln(out,cur)
			for a[start]!=a[cur]{
				if (cur < n && oth[cur] < n) {
					f=0
				} else if (cur >= n && oth[cur] >= n) {
					f=1
				}
				cur=oth[cur]
				vis[cur%n]=true
				aa[f]=append(aa[f],cur)
				// Fprintln(out,cur)
				if cur<n{
					cur+=n
				} else {
					cur-=n
				}
				vis[cur%n]=true
				aa[f]=append(aa[f],cur)
				// Fprintln(out,cur)
			}

			if len(aa[0]) < len(aa[1]){
				f=0
			} else {
				f=1
			}
			// Fprintln(out,"mark",aa[0],aa[1],vis)
			// Fprintln(out,aa[f])
			for _,v:=range(aa[f]){
				rvt[v%n]=true
			}
			// out.Flush()
		}

		for _,v:=range(b){
			if v < n && !vis[v]{
				run1(v)
			}
		}

		cnt:=0
		for i:=0;i<n;i++{
			if rvt[i]{
				cnt++
			}
		}
		Fprintln(out,cnt)
		for i:=0;i<n;i++{
			if rvt[i]{
				Fprint(out,i+1," ")
			}
		}
		Fprintln(out,"")
	}
}

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

package main

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

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

	type edge struct {
		t, v int
	}

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		f := make([]int, n+1)
		posa, posb := make([][]int, n+1), make([][]int, n+1)
		for i := 1; i <= n; i++ {
			a := io.nextInt()
			f[a]++
			posa[a] = append(posa[a], i)
		}
		for i := 1; i <= n; i++ {
			b := io.nextInt()
			f[b]++
			posb[b] = append(posb[b], i)
		}
		ok := true
		for i := 1; i <= n; i++ {
			if f[i] != 2 {
				ok = false
			}
		}
		ans := 0
		col := make([]int, n+1)
		if ok {
			g := make([][]edge, n+1)
			for i := 1; i <= n; i++ {
				var sw []int
				if len(posa[i]) == 2 {
					sw = posa[i]
				}
				if len(posb[i]) == 2 {
					sw = posb[i]
				}
				if sw != nil {
					u, v := sw[0], sw[1]
					g[u] = append(g[u], edge{1, v})
					g[v] = append(g[v], edge{1, u})
				} else {
					u, v := posa[i][0], posb[i][0]
					g[u] = append(g[u], edge{0, v})
					g[v] = append(g[v], edge{0, u})
				}
			}

			for i := 1; i <= n; i++ {
				if col[i] != 0 {
					continue
				}
				var vis []int
				mk := 0
				var dfs func(u, p, c int)
				dfs = func(u, p, c int) {
					vis = append(vis, u)
					col[u] = c
					if c == 1 {
						mk++
					}
					for _, e := range g[u] {
						if e.v == p {
							continue
						}
						if e.t == 0 && col[e.v] == 3-c || e.t == 1 && col[e.v] == c {
							ok = false
						} else if col[e.v] == 0 {
							if e.t == 0 {
								dfs(e.v, u, c)
							} else {
								dfs(e.v, u, 3-c)
							}
						}
					}
				}
				dfs(i, i, 1)
				tot := len(vis)
				if mk > tot-mk {
					for _, j := range vis {
						col[j] = 3 - col[j]
					}
					mk = tot - mk
				}
				ans += mk
			}
		}

		if !ok {
			io.println(-1)
		} else {
			io.println(ans)
			for i := 1; i <= n; i++ {
				if col[i] == 1 {
					io.print(i, " ")
				}
			}
			io.println()
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p []byte
}

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p)
	for f.p[0] <= ' ' {
		f.Read(f.p)
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p)
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}


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


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