Monocarp has got an array $$$a$$$ consisting of $$$n$$$ integers. Let's denote $$$k$$$ as the mathematic mean of these elements (note that it's possible that $$$k$$$ is not an integer). The mathematic mean of an array of $$$n$$$ elements is the sum of elements divided by the number of these elements (i. e. sum divided by $$$n$$$).Monocarp wants to delete exactly two elements from $$$a$$$ so that the mathematic mean of the remaining $$$(n - 2)$$$ elements is still equal to $$$k$$$.Your task is to calculate the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array. The second line contains a sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{9}$$$), where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

Print one integer — the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$).

NoteIn the first example, any pair of elements can be removed since all of them are equal.In the second example, there is no way to delete two elements so the mathematic mean doesn't change.In the third example, it is possible to delete the elements on positions $$$1$$$ and $$$3$$$, or the elements on positions $$$4$$$ and $$$5$$$.

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReaderSize(os.Stdin, 55000)
	w = bufio.NewWriterSize(os.Stdout, 55000)
	cnt uint64
	n uint64
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]uint64, n)
		var sum uint64
		mp := make(map[uint64]uint64)
		for i := uint64(0); i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ans := uint64(0)
		if  2 * sum % n != 0 {
			fmt.Println(0)
			continue
		}
		x := 2 * sum / n
		for _, num := range arr {
			mp[num]--
			if mp[x-num] > 0 {
				ans += mp[x-num]
			}
		}
		fmt.Println(ans)
	}
	w.Flush()
}

package main

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

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

func solve() {
	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	fmt.Fscanf(in, "\n")
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
	out.Flush()
}

package main

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

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

	T int64

	n   int64
	a   []int64
	cnt map[int64]int64
)

func main() {
	_, _ = fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(r, &n)
		a = make([]int64, n)
		cnt = make(map[int64]int64)

		var sum int64
		for i := range a {
			_, _ = fmt.Fscan(r, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		var ans int64
		for _, i := range a {
			cnt[i]--
			ans += cnt[sum-i]
		}

		_, _ = fmt.Fprintln(w, ans)
	}
	_ = w.Flush()
}

package main

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

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

	T int64

	n   int64
	a   []int64
	cnt map[int64]int64
)

func main() {
	_, _ = fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(r, &n)
		a = make([]int64, n)
		cnt = make(map[int64]int64)

		var sum int64
		for i := range a {
			_, _ = fmt.Fscan(r, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		var ans int64
		for _, i := range a {
			ans += cnt[sum-i]
			if i == sum - i {
				ans--
			}
		}

		_, _ = fmt.Fprintln(w, ans / 2)
	}
	_ = w.Flush()
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n uint64
	fmt.Fscan(input, &n)
	a := make([]uint64, n)
    sum := uint64(0)
    digs := map[uint64]int{}
    for i := uint64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x
    // sum / n == sum' / (n - 2)
    // sum - sum' = ?

    cnt := int64(0)
    for _, k := range a {
        digs[k]--
        cnt += int64(digs[x - k])
        assert(digs[x - k] >= 0)
    }
    fmt.Println(cnt)
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

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

		ARR := make([]int64, n)
		var total int64
		for i := 0; int64(i) < n; i++ {
			fmt.Fscan(input, &ARR[i])
			total += ARR[i]

		}
		var q int64
		q = 2 * total
		if q%n != 0 {
			fmt.Println(0)
		} else {
			var remove int64
			remove = q / n
			NN := make([]int64, 0)
			for i := 0; int64(i) < n; i++ {
				if ARR[i] <= remove {
					NN = append(NN, ARR[i])
				}
			}

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

			N := make([]int64, 0)
			pp := len(NN)

			COUNT := make([]int64, 0)
			var c int64 = 1
			begin := NN[0]
			for i := 1; i < pp; i++ {
				if NN[i] == begin {
					c++
				} else {
					N = append(N, begin)
					COUNT = append(COUNT, c)
					c = 1
					begin = NN[i]
				}
			}
			N = append(N, begin)
			COUNT = append(COUNT, c)

			p := len(N)
			var count int64 = 0
			last := p - 1
			for ii := 0; ii < p; ii++ {
				if N[ii]+N[ii] == remove {
					count += COUNT[ii] * (COUNT[ii] - 1) / 2
				} else {
					for j := last; j > ii; j-- {
						if N[ii]+N[j] < remove {
							break
						} else {
							if N[ii]+N[j] > remove {
								last = j
							} else {
								count += COUNT[ii] * COUNT[j]
							}
						}
					}
				}
			}
			fmt.Println(count)

		}

	}

}

package main

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

/*
7
5
9 6 5 2 7
3
0 0 0
4
0 0 0 0
5
1 2 3 4 5
5
2 2 2 2 2
5
2 1 2 1 2
6
1 2 3 4 5 3
---
0 sum=29, mean=29/5
3
6
2 sum=15, mean=15/5 -> 9/3
10 sum=10, mean=10/5 -> 6/3 sumToRemove =
0 sum=8, mean=8/5
3 sum = 16, mean = 18/6 ->  12/4
=======
1
6
3 3 6 3 6 6
---
sum=27, mean=27/6 -> 18/4

1
20
690934752 62748807 583444698 170238861 61388204 692295355 56461303 697222256 161041875 592641684 265603541 488080018 748709359 4974200 5745298 747938261 506342974 247340585 63086398 690597161
*/

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

	sum := int64(0)

	a := make([]int, n)

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

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

	mean := float64(sum) / float64(n)

	sumToRemove := mean * 2

	isInteger := (sumToRemove - math.Trunc(sumToRemove)) < 1e-6

	if !isInteger {
		fmt.Fprintln(writer, 0)
	} else {
		sumToRemoveInt := int(sumToRemove)

		pairCnt := int64(0)

		numToCnt := make(map[int]int)

		for _, num := range a {
			numToCnt[num]++
		}

		for lftNum, lftCnt := range numToCnt {
			rgtNum := sumToRemoveInt - lftNum
			rgtNumCnt := numToCnt[rgtNum]

			if rgtNumCnt != 0 {
				if lftNum != rgtNum {
					pairCnt += int64(lftCnt) * int64(rgtNumCnt)
				} else {
					pairCnt += int64(rgtNumCnt) * int64(rgtNumCnt-1)
				}
			}
		}

		fmt.Fprintln(writer, pairCnt/2)
	}
}

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

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

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

	return nums[idx], idx
}

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

// not my own submission
// original submission: https://codeforces.com/contest/1598/submission/131503323

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
    var n uint64
    fmt.Fscan(input, &n)
    a := make([]uint64, n)
    sum := uint64(0)
    digs := map[uint64]int{}
    for i := uint64(0); i < n; i++ {
        fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
    }

    if 2 * sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2 * sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x
    // sum / n == sum' / (n - 2)
    // sum - sum' = ?
    cnt := uint64(0)
    for _, k := range a {
        digs[k]--
        cnt += uint64(digs[x - k])
        assert(digs[x - k] >= 0)
    }
    fmt.Println(cnt)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1598C(_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)
		s := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			s += int64(a[i])
		}
		s *= 2
		if s%int64(n) > 0 {
			Fprintln(out, 0)
			continue
		}
		s /= int64(n)
		ans := int64(0)
		c := map[int]int{}
		for _, v := range a {
			ans += int64(c[int(s)-v])
			c[v]++
		}
		Fprintln(out, ans)
	}
}

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

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

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int64 {
	// sum/n == mean
	// (sum - d)/n-2 == mean
	var sum int64

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

	if sum * 2 % int64(n) != 0 {
		return 0
	}

	target := (sum * 2) / int64(n)

	mp := make(map[int64][]int)

	for i := 0; i < n; i++ {
		mp[int64(a[i])] = append(mp[int64(a[i])], i)
	}

	var ans int64

	for i := 0; i < n; i++ {
		el := a[i]

		if int64(el) > target {
			continue
		}

		rem := target - int64(el)

		if v, ok := mp[rem]; ok {

			idx := sort.Search(len(v), func(k int) bool {
				return v[k] > i
			})

			if idx < len(v) {
				ans += int64(len(v)  - idx)
			}
		}
	}

	return ans

}

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, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int64()
		a := make([]int64, n)
		for i := range a {
			a[i] = _int64()
		}
		printf("%d\n", sol(n, a))
	}
}

func sol(n int64, a []int64) int64 {
	sum := int64(0)
	for i := range a {
		sum += a[i]
	}
	target := sum * 2 / n
	if target*n != sum*2 {
		return 0
	}
	ret := int64(0)
	freq := map[int64]int64{}
	for i := range a {
		freq[a[i]]++
	}
	for i, f := range freq {
		if i*2 == target {
			ret += f * (f - 1)
		} else {
			ret += f * freq[target-i]
		}
	}
	return ret / 2
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int64
		fmt.Fscanf(r, "%d\n", &n)

		arr := make([]int64, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscanf(r, "%d", &arr[i])
		}
		fmt.Fscanf(r, "\n")

		sum := int64(0)
		c := make(map[int64]int64)
		for i := int64(0); i < n; i++ {
			c[arr[i]]++
			sum += arr[i]
		}

		//	sum / n = sum - a - x / n - 2

		//	(n - 2) * sum = sum * n - a * n - x * n
		//	((n - 2) * sum - sum * n + a * n) / -n = x

		v := make([]int64, 0)
		for k, _ := range c {
			v = append(v, k)
		}
		sort.Slice(v, func(i, j int) bool {
			return v[i] < v[j]
		})

		res := int64(0)
		for _, a := range v {
			s := -(sum*(n-2) - sum*n + a*n)
			if s%n == 0 && a < s/n {
				res += c[a] * c[s/n]
			} else if s%n == 0 && a == s/n {
				//fmt.Println(a, c[a]*(c[a]-1))
				res += (c[a] * (c[a] - 1)) / 2
			}
		}

		fmt.Println(res)
	}
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReaderSize(os.Stdin, 55000)
	w = bufio.NewWriterSize(os.Stdout, 55000)
	cnt uint64
	n uint64
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]uint64, n)
		var sum uint64
		mp := make(map[uint64]int)
		for i := uint64(0); i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ans := 0
		if  2 * sum % n == 0 {
			x := 2 * sum / n
			for _, num := range arr {
				mp[num]--
				if mp[x-num] > 0 {
					ans += mp[x-num]
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReaderSize(os.Stdin, 55000)
	w = bufio.NewWriterSize(os.Stdout, 55000)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ans := 0
		if  2 * sum % n == 0 {
			x := 2 * sum / n
			for _, num := range arr {
				mp[num]--
				if mp[x-num] > 0 {
					ans += mp[x-num]
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ans := 0
		if  (2 * sum) % n == 0{
			x := 2*(sum/n)
			for _, num := range arr {
				mp[num]--
				if mp[x-num] > 0 {
					ans += mp[x-num]
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ans := 0
		if  2 *sum % n == 0{
			x := 2*sum/n
			for _, num := range arr {
				mp[num]--
				ans += mp[x-num]
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]float64, n)
		sum := 0.0
		mp := make(map[float64]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		var ave float64 = sum/float64(n)
		ans := 0
		if  ave*2 == float64(int(sum / float64(n) * 2)){
			for _, num := range arr {
				if cnt := mp[2*ave-num]; cnt > 0 {
					mp[num]--
					ans += mp[2*ave-num]
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
"bufio"
"fmt"
"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]float64, n)
		sum := 0.0
		mp := make(map[float64]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		var ave float64 = sum/float64(n)
		ans := 0
		if  ave*2 == float64(int(sum / float64(n) * 2)){
			for _, num := range arr {
				mp[num]--
				if cnt := mp[2*ave-num]; cnt > 0 {
					ans += cnt
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ave := sum / n
		ans := 0
		if ave * n  == sum {
			for _, num := range arr {
				mp[num]--
				if cnt := mp[2*ave-num]; cnt > 0 {
					ans += cnt
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		ave := sum / n
		ans := 0
		if ave *n  == sum {
			for _, num := range arr {
				cnt := mp[2*ave-num]
				if cnt > 0 && mp[2*ave-num] == num {
					ans += cnt-1
					mp[2*ave-num]--
					mp[num]--
				} else if cnt > 0 {
					ans += cnt
					mp[2*ave-num]--
					mp[num]--
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var(
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	cnt int
	n int
)

func main() {
	fmt.Fscan(os.Stdin, &cnt)
	for ; cnt > 0; cnt-- {
		fmt.Fscan(r, &n)
		arr := make([]int, n)
		sum := 0
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
			sum += arr[i]
			mp[arr[i]]++
		}
		fmt.Println(arr, mp)
		ave := sum / n
		ans := 0
		if ave *n  == sum {
			for _, num := range arr {
				cnt := mp[2*ave-num]
				if cnt > 0 && mp[2*ave-num] == num {
					ans += cnt-1
					mp[2*ave-num]--
					mp[num]--
				} else if cnt > 0 {
					ans += cnt
					mp[2*ave-num]--
					mp[num]--
				}
			}
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

package main

import (
	"fmt"
	"os"
)

func main() {
	cnt := 0
	fmt.Fscan(os.Stdin, &cnt)
	for i := 0; i < cnt; i++ {
		//IO
		var n float64
		fmt.Fscan(os.Stdin, &n)
		arr := make([]float64, int(n))
		var sum float64
		for i := 0; i < int(n); i++ {
			fmt.Fscan(os.Stdin, &arr[i])
			sum += arr[i]
		}
		//Algorithms
		var averagefloat float64 = sum / n
		if float64(int(averagefloat * 2)) != averagefloat *2 {
			fmt.Println(0)
		} else {
			//01 bag problem
			need := averagefloat*2
			//dp数组含义表示第i背包容量前n个物品在存放几件时候的方案数
			dp := make([][3]float64, int(need)+1)

			//初始化dp
			dp[0][0] = 1
			//遍历dp
			for i := 0; i < len(arr); i++ {
				for j := len(dp)-1; j >= 0; j-- {
					//dp状态转移：1、选择当前物品,方案数 2、不选择当前物品方案数  dp[i] = 1+2
					if j >= int(arr[i]) {
						dp[j][1] += dp[j-int(arr[i])][0]
						dp[j][2] += dp[j-int(arr[i])][1]
					} else {
						break
					}
				}
			}
			fmt.Println(dp[len(dp)-1][2], arr)
		}
	}
}

package main

import (
	"fmt"
	"os"
)

func main() {
	cnt := 0
	fmt.Fscan(os.Stdin, &cnt)
	for i := 0; i < cnt; i++ {
		//IO
		var n float64
		fmt.Fscan(os.Stdin, &n)
		arr := make([]float64, int(n))
		var sum float64
		for i := 0; i < int(n); i++ {
			fmt.Fscan(os.Stdin, &arr[i])
			sum += arr[i]
		}
		//Algorithms
		var averagefloat float64 = sum / n
		if float64(int(averagefloat * 2)) != averagefloat *2 {
			fmt.Println(0)
		} else {
			//01 bag problem
			need := averagefloat*2
			//dp数组含义表示第i背包容量前n个物品在存放几件时候的方案数
			dp := make([][3]float64, int(need)+1)

			//初始化dp
			dp[0][0] = 1
			//遍历dp
			for i := 0; i < len(arr); i++ {
				for j := len(dp)-1; j >= 0; j-- {
					//dp状态转移：1、选择当前物品,方案数 2、不选择当前物品方案数  dp[i] = 1+2
					if j >= int(arr[i]) {
						dp[j][1] += dp[j-int(arr[i])][0]
						dp[j][2] += dp[j-int(arr[i])][1]
					} else {
						break
					}
				}
			}
			fmt.Println(dp[len(dp)-1][2])
		}
	}
}

package main

import (
	"fmt"
	"os"
)

func main() {
	cnt := 0
	fmt.Fscan(os.Stdin, &cnt)
	for i := 0; i < cnt; i++ {
		//IO
		var n float64
		fmt.Fscan(os.Stdin, &n)
		arr := make([]float64, int(n))
		var sum float64
		for i := 0; i < int(n); i++ {
			fmt.Fscan(os.Stdin, &arr[i])
			sum += arr[i]
		}
		//Algorithms
		var averagefloat float64 = sum / n
		if float64(int(averagefloat * 2)) != averagefloat *2 {
			fmt.Println(0)
		} else {
			//01 bag problem
			need := averagefloat*2
			//dp数组含义表示第i背包容量前n个物品在存放几件时候的方案数
			dp := make([][3]float64, int(need)+1)

			//初始化dp
			dp[0][0] = 1
			//遍历dp
			for i := 0; i < len(arr); i++ {
				for j := len(dp)-1; j >= 0; j-- {
					//dp状态转移：1、选择当前物品,方案数 2、不选择当前物品方案数  dp[i] = 1+2
					if j >= int(arr[i]) {
						dp[j][1] += dp[j-int(arr[i])][0]
						dp[j][2] += dp[j-int(arr[i])][1]
					}
				}
			}
			fmt.Println(dp[len(dp)-1][2])
		}
	}
}

package main

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

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

func solve() {
	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	//fmt.Fscanf(in, "\n")
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
	out.Flush()
}

package main

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

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

func solve() {

	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	fmt.Fscanf(in, "\n")
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
	out.Flush()
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

func solve() {

	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
	out.Flush()
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%v\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

func solve() {

	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
	out.Flush()
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%v\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

func solve() {

	var n int
	_, err := fmt.Fscanf(in, "%v\n", &n)
	if err != nil {
		fmt.Println(out, err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Fprintln(out, err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Fprintln(out, "0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Fprintln(out, ans)
}

func main() {
	var t = 0
	fmt.Fscanf(in, "%v\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	var n int
	_, err := fmt.Fscanf(in,"%v\n", &n)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Println(err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Println("0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Println(ans)
}

func main() {
	var t = 0
	fmt.Scanf("%v\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	var n int
	_, err := fmt.Fscanf(in, "%d", &n)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Println(err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Println("0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Println(ans)
}

func main() {
	var t = 0
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Println(err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Println("0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Println(ans)
}

func main() {
	var t = 0
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	var n int
	_, err := fmt.Fscan(in, &n)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		if i == n-1 {
			_, err = fmt.Fscanf(in, "%v\n", &a[i])
		} else {
			_, err = fmt.Fscan(in, &a[i])
		}
		if err != nil {
			fmt.Println(err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Println("0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Println(ans)
}

func main() {
	var t = 0
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	var n int
	_, err := fmt.Fscan(in, &n)
	if err != nil {
		fmt.Println(err)
		return
	}
	a := make([]int, n)
	s := int64(0)
	for i := 0; i < n; i++ {
		_, err = fmt.Fscan(in, &a[i])
		if err != nil {
			fmt.Println(err)
			return
		}
		s += int64(a[i] * 2)
	}
	if s%int64(n) > 0 {
		fmt.Println("0")
		return
	}
	y := s / int64(n)
	ans := int64(0)
	m := make(map[int64]int64)
	for i := 0; i < n; i++ {
		x := y - int64(a[i])
		ans += m[x]
		m[int64(a[i])]++
	}
	fmt.Println(ans)
}

func main() {
	var t = 0
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

	T int

	n   int
	a   []int
	cnt map[int]int
)

func main() {
	_, _ = fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(r, &n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := range a {
			_, _ = fmt.Fscan(r, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			ans += cnt[sum-i]
			if i == sum - i {
				ans--
			}
		}

		_, _ = fmt.Fprintln(w, ans / 2)
	}
	_ = w.Flush()
}

package main

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

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

	T int

	n   int
	a   []int
	cnt map[int]int
)

func main() {
	_, _ = fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(r, &n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := range a {
			_, _ = fmt.Fscan(r, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			ans += cnt[sum-i]
			if i == sum - i {
				ans--
			}
		}

		_, _ = fmt.Fprintln(w, ans)
	}
	_ = w.Flush()
}

package main

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

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

const (
	MaxN int = 2e5 + 5
)

var (
	T, n int
	a    []int
	cnt  map[int]int

)

func main() {
	_, _ = fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(r, &n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := range a {
			_, _ = fmt.Fscan(r, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			cnt[i]--
			ans += cnt[sum-i]
		}

		_, _ = fmt.Fprintln(w, ans)
	}
	_ = w.Flush()
}

package main

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

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

const (
	MaxN int = 2e5
)

var (
	T, n int
	a    []int
	cnt  map[int]int

)

func main() {
	_, _ = fmt.Fscan(reader, &T)
	for ; T > 0; T-- {
		_, _ = fmt.Fscan(reader, &n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := range a {
			_, _ = fmt.Fscan(reader, &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			cnt[i]--
			ans += cnt[sum-i]
		}

		_, _ = fmt.Fprintln(writer, ans)
	}
	_ = writer.Flush()
}

package main

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

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

const (
	MaxN int = 2e5
)

var (
	T, n int
	a    []int
	cnt  map[int]int
)

func main() {
	scanf("%d", &T)
	for ; T > 0; T-- {
		scanf("%d", &n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := range a {
			scanf("%d", &a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			cnt[i]--
			ans += cnt[sum-i]
		}

		printf("%d", ans)
	}
}

package main

import (
	"fmt"
)

const (
	MaxN int = 2e5
)

var (
	T, n int
	a    []int
	cnt  map[int]int
)

func main() {
	_, _ = fmt.Scan(&T)
	for ; T > 0; T-- {
		_, _ = fmt.Scan(&n)
		a = make([]int, n)
		cnt = make(map[int]int)

		sum := 0
		for i := 0; i < n; i++ {
			_, _ = fmt.Scan(&a[i])
			sum += a[i]
			a[i] *= n
			cnt[a[i]]++
		}
		sum <<= 1

		ans := 0
		for _, i := range a {
			ans += cnt[sum-i]
		}

		fmt.Println(ans)
	}
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n uint64
	fmt.Fscan(input, &n)
	a := make([]uint64, n)
    sum := uint64(0)
    digs := map[uint64]int{}
    for i := uint64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x
    // sum / n == sum' / (n - 2)
    // sum - sum' = ?

    cnt := 0
    for _, k := range a {
        digs[k]--
        cnt += digs[x - k]
        assert(digs[x - k] >= 0)
    }
    fmt.Println(cnt)
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n uint64
	fmt.Fscan(input, &n)
	a := make([]uint64, n)
    sum := uint64(0)
    digs := map[uint64]int{}
    for i := uint64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x

    cnt := 0
    for _, k := range a {
        digs[k]--
        cnt += digs[x - k]
    }
    fmt.Println(cnt)
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

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

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x

    cnt := 0
    for _, k := range a {
        digs[k]--
        cnt += digs[x - k]
    }
    fmt.Println(cnt)
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n int64
	fmt.Fscan(input, &n)
	a := make([]int64, n)
    sum := int64(0)
    digs := map[int64]int{}
    used := map[int64]bool{}
    for i := int64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x

    cnt := 0
    for k, v := range digs {
        k2 := x - k
        if digs[k2] == 0 || used[k] || used[k2] {continue}
        used[k] = true
        used[k2] = true
        if k2 != k {
            cnt += digs[k2] * v
        } else {
            if v > 1 {
                cnt += v * (v - 1) / 2
            }
        }
    }
    fmt.Println(cnt)
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n int64
	fmt.Fscan(input, &n)
	a := make([]int64, n)
    sum := int64(0)
    digs := map[int64]int{}
    used := map[int64]bool{}
    for i := int64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]
    // a[i] + a[j] == 2x

    cnt := 0
    for k, v := range digs {
        if used[k] {continue}
        used[k] = true
        used[x - k] = true
        p := digs[x - k]
        if x - k != k {
           cnt += p * v
        } else {
            if p > 1 {
                cnt += p * (p - 1) / 2
            }
        }
    }
    fmt.Println(cnt)
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n int64
	fmt.Fscan(input, &n)
	a := make([]int64, n)
    sum := int64(0)
    digs := map[int64]int{}
    used := map[int64]bool{}
    for i := int64(0); i < n; i++ {
		fmt.Fscan(input, &a[i])
        sum += a[i]
        digs[a[i]]++
	}

    if 2*sum % n != 0 {
        fmt.Println(0)
        return
    }
    x := 2*sum / n // 2x
    // a[i] - x == x - a[j]
    // a[i] == 2x - a[j]

    cnt := 0
    for k, _ := range digs {
        if used[k] || used[x - k] {continue}
        used[k] = true
        used[x - k] = true
        p := digs[x - k]
        if x - k != k {
           cnt += p
        } else {
            if p > 1 {
                cnt += p * (p - 1) / 2
            }
        }
    }
    fmt.Println(cnt)
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

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

		ARR := make([]int64, n)
		var total int64
		for i := 0; int64(i) < n; i++ {
			fmt.Fscan(input, &ARR[i])
			total += ARR[i]

		}
		var q int64
		q = 2 * total
		if q%n != 0 {
			fmt.Println(0)
		} else {
			var remove int64
			remove = q / n
			NN := make([]int64, 0)
			for i := 0; int64(i) < n; i++ {
				if ARR[i] <= remove {
					NN = append(NN, ARR[i])
				}
			}

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

			N := make([]int64, 0)
			pp := len(NN)

			COUNT := make([]int, 0)
			c := 1
			begin := NN[0]
			for i := 1; i < pp; i++ {
				if NN[i] == begin {
					c++
				} else {
					N = append(N, begin)
					COUNT = append(COUNT, c)
					c = 1
					begin = NN[i]
				}
			}
			N = append(N, begin)
			COUNT = append(COUNT, c)

			p := len(N)
			count := 0
			last := p - 1
			for ii := 0; ii < p; ii++ {
				if N[ii]+N[ii] == remove {
					count += COUNT[ii] * (COUNT[ii] - 1) / 2
				} else {
					for j := last; j > ii; j-- {
						if N[ii]+N[j] < remove {
							break
						} else {
							if N[ii]+N[j] > remove {
								last = j
							} else {
								count += COUNT[ii] * COUNT[j]
							}
						}
					}
				}
			}
			fmt.Println(count)

		}

	}

}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

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

		ARR := make([]int64, n)
		var total int64
		for i := 0; int64(i) < n; i++ {
			fmt.Fscan(input, &ARR[i])
			total += ARR[i]

		}
		var q int64
		q = 2 * total
		if q%n != 0 {
			fmt.Println(0)
		} else {
			var remove int64
			remove = q / n
			NN := make([]int64, 0)
			for i := 0; int64(i) < n; i++ {
				if ARR[i] <= remove {
					NN = append(NN, ARR[i])
				}
			}

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

			N := make([]int64, 0)
			pp := len(NN)

			COUNT := make([]int, 0)
			c := 1
			begin := NN[0]
			for i := 1; i < pp; i++ {
				if NN[i] == begin {
					c++
				} else {
					N = append(N, begin)
					COUNT = append(COUNT, c)
					c = 1
					begin = NN[i]
				}
			}
			N = append(N, begin)
			COUNT = append(COUNT, c)

			p := len(N)
			count := 0
			last := p - 1
			for ii := 0; ii < p; ii++ {
				if N[ii]+N[ii] == remove {
					count += COUNT[ii] * (COUNT[ii] - 1) / 2
				} else {
					for j := last; j > ii; j-- {
						if N[ii]+N[j] < remove {
							break
						} else {
							if N[ii]+N[j] > remove {
								last = j - 1
							} else {
								count += COUNT[ii] * COUNT[j]
							}
						}
					}
				}
			}
			fmt.Println(count)

		}

	}

}

package main

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

/*
7
5
9 6 5 2 7
3
0 0 0
4
0 0 0 0
5
1 2 3 4 5
5
2 2 2 2 2
5
2 1 2 1 2
6
1 2 3 4 5 3
---
0 sum=29, mean=29/5
3
6
2 sum=15, mean=15/5 -> 9/3
10 sum=10, mean=10/5 -> 6/3 sumToRemove =
0 sum=8, mean=8/5
3 sum = 16, mean = 18/6 ->  12/4
=======
1
6
3 3 6 3 6 6
---
sum=27, mean=27/6 -> 18/4

1
20
690934752 62748807 583444698 170238861 61388204 692295355 56461303 697222256 161041875 592641684 265603541 488080018 748709359 4974200 5745298 747938261 506342974 247340585 63086398 690597161
*/

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

	sum := 0

	a := make([]int, n)

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

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

	mean := float64(sum) / float64(n)

	sumToRemove := mean * 2

	isInteger := (sumToRemove - math.Trunc(sumToRemove)) < 1e-6

	if !isInteger {
		fmt.Fprintln(writer, 0)
	} else {
		sumToRemoveInt := int(sumToRemove)

		pairCnt := int64(0)

		numToCnt := make(map[int]int)

		for _, num := range a {
			numToCnt[num]++
		}

		for lftNum, lftCnt := range numToCnt {
			rgtNum := sumToRemoveInt - lftNum
			rgtNumCnt := numToCnt[rgtNum]

			if rgtNumCnt != 0 {
				if lftNum != rgtNum {
					pairCnt += int64(lftCnt) * int64(rgtNumCnt)
				} else {
					pairCnt += int64(rgtNumCnt) * int64(rgtNumCnt-1)
				}
			}
		}

		fmt.Fprintln(writer, pairCnt/2)
	}
}

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

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

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

	return nums[idx], idx
}

//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"
	"math"
	"os"
)

/*
7
5
9 6 5 2 7
3
0 0 0
4
0 0 0 0
5
1 2 3 4 5
5
2 2 2 2 2
5
2 1 2 1 2
6
1 2 3 4 5 3
---
0 sum=29, mean=29/5
3
6
2 sum=15, mean=15/5 -> 9/3
10 sum=10, mean=10/5 -> 6/3 sumToRemove =
0 sum=8, mean=8/5
3 sum = 16, mean = 18/6 ->  12/4
*/

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

	sum := 0

	a := make([]int, n)

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

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

	mean := float64(sum) / float64(n)

	sumToRemove := mean * 2

	isInteger := (sumToRemove - math.Trunc(sumToRemove)) < 1e-6

	if !isInteger {
		fmt.Fprintln(writer, 0)
	} else {
		sumToRemoveInt := int(sumToRemove)

		pairCnt := int64(0)

		numToCnt := make(map[int]int)

		for _, num := range a {
			numToCnt[num]++
		}

		for lftNum, lftCnt := range numToCnt {
			rgtNum := sumToRemoveInt - lftNum
			rgtNumCnt := numToCnt[rgtNum]

			if rgtNumCnt != 0 {
				if lftNum != rgtNum {
					pairCnt += int64(lftCnt) * int64(rgtNumCnt)
				} else {
					pairCnt += int64(rgtNumCnt) * int64(rgtNumCnt-1)
				}
			}
		}

		fmt.Fprintln(writer, pairCnt/2)
	}
}

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

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

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

	return nums[idx], idx
}

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


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, numbered from $$$1$$$ to $$$n$$$. You can perform the following operation no more than $$$3n$$$ times:  choose three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 \le i, j \le n$$$; $$$0 \le x \le 10^9$$$);  assign $$$a_i := a_i - x \cdot i$$$, $$$a_j := a_j + x \cdot i$$$. After each operation, all elements of the array should be non-negative.Can you find a sequence of no more than $$$3n$$$ operations after which all elements of the array are equal?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^4$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^5$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

For each test case print the answer to it as follows:   if there is no suitable sequence of operations, print $$$-1$$$;  otherwise, print one integer $$$k$$$ ($$$0 \le k \le 3n$$$) — the number of operations in the sequence. Then print $$$k$$$ lines, the $$$m$$$-th of which should contain three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 \le i, j \le n$$$; $$$0 \le x \le 10^9$$$) for the $$$m$$$-th operation.  If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize $$$k$$$.

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

func Max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func solve() {
	n := nextInt()
	arr := make([]int, n)
	sum := 0
	for i := range arr {
		arr[i] = nextInt()
		sum += arr[i]
	}
	if sum%n != 0 {
		printf("-1\n")
		return
	}

	sum /= n
	res := [][3]int{}

	for i := 1; i < n; i++ {
		res = append(res, [3]int{1, i + 1, (i + 1 - arr[i]%(i+1)) % (i + 1)})
		arr[0] -= (i + 1 - arr[i]%(i+1)) % (i + 1)
		arr[i] += (i + 1 - arr[i]%(i+1)) % (i + 1)
		res = append(res, [3]int{i + 1, 1, arr[i] / (i + 1)})
		arr[0] += arr[i]
		arr[i] = 0
	}

	for i := 1; i < n; i++ {
		res = append(res, [3]int{1, i + 1, sum})
	}
	printf("%d\n", len(res))
	for _, v := range res {
		printf("%d %d %d\n", v[0], v[1], v[2])
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// out = bufio.NewWriterSize(file2, 8192)
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		s := 0
		Fscan(in, &n)
		a := make([]int, n+1)
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
			s += a[i]
		}
		if s%n > 0 {
			Fprintln(out, -1)
			continue
		}
		Fprintln(out, 3*n-3)
		for i := 2; i <= n; i++ {
			Fprintln(out, 1, i, (i-a[i]%i)%i)
			Fprintln(out, i, 1, (a[i]-1)/i+1)
		}
		for i := 2; i <= n; i++ {
			Fprintln(out, 1, i, s/n)
		}
	}
}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

func Max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func solve() {
	n := nextInt()
	arr := make([]int, n)
	sum := 0
	for i := range arr {
		arr[i] = nextInt()
		sum += arr[i]
	}
	if sum%n != 0 {
		printf("-1\n")
		return
	}

	sum /= n

	for i := range arr {
		arr[i] -= sum
	}

	res := [][3]int{}

	for i := 1; i < n; i++ {
		res = append(res, [3]int{1, i + 1, (i + 1 - arr[i]%(i+1)) % (i + 1)})
		arr[0] -= (i + 1 - arr[i]%(i+1)) % (i + 1)
		arr[i] += (i + 1 - arr[i]%(i+1)) % (i + 1)
		res = append(res, [3]int{i + 1, 1, arr[i] / (i + 1)})
		arr[0] += arr[i]
		arr[i] = 0
	}

	for i := 1; i < n; i++ {
		res = append(res, [3]int{1, i + 1, sum})
	}
	printf("%d\n", len(res))
	for _, v := range res {
		printf("%d %d %d\n", v[0], v[1], v[2])
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// out = bufio.NewWriterSize(file2, 8192)
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}


You have a string $$$s$$$ and a chip, which you can place onto any character of this string. After placing the chip, you move it to the right several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i + 1$$$. Of course, moving the chip to the right is impossible if it is already in the last position.After moving the chip to the right, you move it to the left several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i - 1$$$. Of course, moving the chip to the left is impossible if it is already in the first position.When you place a chip or move it, you write down the character where the chip ends up after your action. For example, if $$$s$$$ is abcdef, you place the chip onto the $$$3$$$-rd character, move it to the right $$$2$$$ times and then move it to the left $$$3$$$ times, you write down the string cdedcb.You are given two strings $$$s$$$ and $$$t$$$. Your task is to determine whether it's possible to perform the described operations with $$$s$$$ so that you write down the string $$$t$$$ as a result.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains the string $$$s$$$ ($$$1 \le |s| \le 500$$$), the second line contains the string $$$t$$$ ($$$1 \le |t| \le 2 \cdot |s| - 1$$$). Both strings consist of lowercase English characters. It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$500$$$.

For each test case, print "YES" if you can obtain the string $$$t$$$ by performing the process mentioned in the statement with the string $$$s$$$, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

6
abcdef
cdedcb
aaa
aaaaa
aab
baaa
ab
b
abcdef
abcdef
ba
baa

NoteConsider the examples.The first test case is described in the statement.In the second test case, you can place the chip on the $$$1$$$-st position, move it twice to the right, and then move it twice to the left.In the fourth test case, you can place the chip on the $$$2$$$-nd position, and then don't move it at all.In the fifth test case, you can place the chip on the $$$1$$$-st position, move it $$$5$$$ times to the right, and then finish the process.

package main

import (
	"fmt"
)

const left = "left"
const right = "right"

type Tree struct {
	left  *Tree
	value string
	until string
	side  string
	right *Tree
}

func main() {
	var n int
	var t, s string
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		ok := false
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		for i := 0; i < len(s); i++ {
			if ok {
				break
			}
			if s[i] == t[0] {
				st := ""
				for j := i; j < len(s); j++ {
					if ok {
						break
					}
					st += string(s[j])
					str := st
					if str == t {
						fmt.Println("Yes")
						ok = true
						break
					}
					if j == 0 {
						continue
					}
					for e := j - 1; e >= 0; e-- {
						str += string(s[e])
						if len(str) > len(t) {
							break
						}
						if str == t {
							fmt.Println("Yes")
							ok = true
							break
						}
					}
				}
			}
		}

		if !ok {
			fmt.Println("No")
		}
	}
}

package main

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

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

	var T int
	var txt, tar []byte
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &txt, &tar)
		n := len(tar)
		halfLen := make([]int, n)
		for i, mid, r := 0, 0, 0; i < n; i++ {
			hl := 1
			if i < r {
				hl = min(halfLen[mid*2-i], r-i)
			}
			for ; i >= hl && i+hl < n && tar[i-hl] == tar[i+hl]; hl++ {
			}
			if i+hl > r {
				mid, r = i, i+hl
			}
			halfLen[i] = hl
		}
		isP := func(l, r int) bool { return halfLen[(l+r)/2]*2 > r-l+1 }

		maxMatchPrefix := func(text, pattern []byte) int {
			match := make([]int, len(pattern))
			for i, c := 1, 0; i < len(pattern); i++ {
				v := pattern[i]
				for c > 0 && pattern[c] != v {
					c = match[c-1]
				}
				if pattern[c] == v {
					c++
				}
				match[i] = c
			}
			lenP := len(pattern)
			c, mx := 0, 0
			for _, v := range text {
				for c > 0 && pattern[c] != v {
					c = match[c-1]
				}
				if pattern[c] == v {
					if c++; c > mx {
						mx = c
					}
				}
				if c == lenP {
					return lenP
				}
			}
			return mx
		}

		preMax := maxMatchPrefix(txt, tar)
		for i := 0; i < n/2; i++ {
			tar[i], tar[n-1-i] = tar[n-1-i], tar[i]
		}
		sufMax := maxMatchPrefix(txt, tar)
		for i := 0; i < n; i += 2 {
			if isP(0, i) && n-i/2 <= sufMax || isP(n-1-i, n-1) && n-i/2 <= preMax {
				Fprintln(out, "YES")
				continue o
			}
		}
		Fprintln(out, "NO")
	}
}

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

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

package main

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

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

	var T int
	var txt, tar []byte
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &txt, &tar)
		n := len(tar)
		halfLen := make([]int, n)
		for i, mid, r := 0, 0, 0; i < n; i++ {
			hl := 1
			if i < r {
				hl = min(halfLen[mid*2-i], r-i)
			}
			for ; i >= hl && i+hl < n && tar[i-hl] == tar[i+hl]; hl++ {
			}
			if i+hl > r {
				mid, r = i, i+hl
			}
			halfLen[i] = hl
		}
		isP := func(l, r int) bool { return halfLen[(l+r)/2]*2 > r-l+1 }

		rev := append([]byte(nil), tar...)
		for i := 0; i < n/2; i++ {
			rev[i], rev[n-1-i] = rev[n-1-i], rev[i]
		}
		for i := 0; i < n; i += 2 {
			if isP(0, i) && bytes.Contains(txt, rev[:n-i/2]) || isP(n-1-i, n-1) && bytes.Contains(txt, tar[:n-i/2]) {
				Fprintln(out, "YES")
				continue o
			}
		}
		Fprintln(out, "NO")
	}
}

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

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

package main

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

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

	var T int
	var txt, tar []byte
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &txt, &tar)
		n := len(tar)
		t := append(make([]byte, 0, n*2+3), '^')
		for _, c := range tar {
			t = append(t, '#', c)
		}
		t = append(t, '#', '$')
		halfLen := make([]int, len(t)-2)
		halfLen[1] = 1
		for i, mid, r := 2, 1, 0; i < len(halfLen); i++ {
			hl := 1
			if i < r {
				hl = min(halfLen[mid*2-i], r-i)
			}
			for ; t[i-hl] == t[i+hl]; hl++ {
			}
			if i+hl > r {
				mid, r = i, i+hl
			}
			halfLen[i] = hl
		}
		isP := func(l, r int) bool { return halfLen[l+r+2] > r-l+1 }

		rev := append([]byte(nil), tar...)
		for i := 0; i < n/2; i++ {
			rev[i], rev[n-1-i] = rev[n-1-i], rev[i]
		}
		for i := 0; i < n; i += 2 {
			if isP(0, i) && bytes.Contains(txt, rev[:n-i/2]) || isP(n-1-i, n-1) && bytes.Contains(txt, tar[:n-i/2]) {
				Fprintln(out, "YES")
				continue o
			}
		}
		Fprintln(out, "NO")
	}
}

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

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

package main

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

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

	var T int
	var txt, tar []byte
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &txt, &tar)
		n := len(tar)
		t := append(make([]byte, 0, n*2+3), '^')
		for _, c := range tar {
			t = append(t, '#', c)
		}
		t = append(t, '#', '$')
		halfLen := make([]int, len(t)-2)
		halfLen[1] = 1
		for i, mid, r := 2, 1, 0; i < len(halfLen); i++ {
			hl := 1
			if i < r {
				hl = min(halfLen[mid*2-i], r-i)
			}
			for ; t[i-hl] == t[i+hl]; hl++ {
			}
			if i+hl > r {
				mid, r = i, i+hl
			}
			halfLen[i] = hl
		}
		isP := func(l, r int) bool { return halfLen[l+r+2] > r-l+1 }

		for i := 0; i < n; i += 2 {
			if isP(0, i) {
				sub := []byte{}
				for j := n - 1; j >= i/2; j-- {
					sub = append(sub, tar[j])
				}
				if bytes.Contains(txt, sub) {
					Fprintln(out, "YES")
					continue o
				}
			}
		}
		for i := n - 1; i >= 0; i -= 2 {
			if isP(i, n-1) && bytes.Contains(txt, tar[:n-(n-1-i)/2]) {
				Fprintln(out, "YES")
				continue o
			}
		}
		Fprintln(out, "NO")
	}
}

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

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

package main

import ."fmt"
//import "sort"
import "bufio"
import "os"
//import "strconv"
//import "math"
import "strings"
//import "time"
//import ."math"

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

func main() {
    defer writer.Flush()

    t := sint64()
    for i := int64(0); i < t; i++ {
        solve()
    }

}
func solve() {

    //n := sint64()
    a := sstring()
    b := sstring()
    ans := "nO"
    for i := range a {
        substract := a[:len(a) - i]
        if len(substract) == 0 {
            continue
        }
        suffics := substract[:len(substract) - 1]
        suffics = reverse(suffics)
        substract += suffics
        boolean := strings.Contains(substract, b)
        if boolean {
            ans = "yEs"
            break
        }
    }
    printf(ans + "\n")

}
func resh(n int64) []int64 {
    p := make([]bool, n + 1)
    for i := int64(2); i <= n; i++ {
        p[i] = true
    }
    for i := int64(0); i <= n; i++ {
		if p[i] {
			for j := i * i; j <= n; j += i {
				p[j] = false
				//Print(j, " ")
			}
		}
	}
	ans := make([]int64, 0)
	for i := int64(0); i < n; i++ {
	    if p[i] {
	        ans = append(ans, i)
	    }
	}
	return ans
}
func printf(f string, a ...interface{}) {
	Fprintf(writer, f, a...)
}
func sstring() string {
	scanner.Scan()
	return scanner.Text()
}
func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}
// cтрочки
func reverse(s string) string {
	ans := []byte(s)
	l := 0
	r := len(ans) - 1
	for l < r {
		ans[l], ans[r] = ans[r], ans[l]
		l++
		r--
	}
	return string(ans)
}

//  матеша
func sum(a []int64) int64{
    ans := int64(0)
    for _, i := range a {
        ans += i
    }
    return ans
}
func abs(x int64) int64 {
    if x < 0 {
        return 0 - x
    }
    return x
}
func min(x, y int64) int64 {
    if x < y {
        return x
    }
    return y
}
func max(x, y int64) int64 {
    if x > y {
        return x
    }
    return y
}
func ceil(a, b int64) int64{
    s := a / b
    if a % b >= 0 {
        s++
    }
    if s == 0 {
        s = 1
    }
    return s
}
func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

package main

import (
	"bufio"
	"bytes"
	"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 s1, s2 string
		fmt.Fscan(in, &s1, &s2)

		var f bool
	Loop:
		for i := 0; i < len(s1); i++ {
			for j := i + 1; j <= len(s1) && j-i <= len(s2); j++ {
				var buf bytes.Buffer
				buf.WriteString(s1[i:j])

				end := j - 1 - (len(s2) - buf.Len())
				if buf.Len() < len(s2) && end >= 0 {
					for k := j - 2; k >= end; k-- {
						buf.WriteByte(s1[k])
					}
				}

				if buf.String() == s2 {
					f = true
					break Loop
				}
			}
		}

		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(r, &t)

	for k := 0; k < t; k++ {
		var s, t string
		fmt.Fscan(r, &s, &t)
		flag := false
		for i := 0; i < len(s); i++ {
			tmp := solve1553B(i, 0, true, &s, &t)
			if tmp {
				flag = true
				break
			}
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

//p1是s的移动下标,p2是t的移动下标
func solve1553B(p1, p2 int, isRight bool, s, t *string) bool {
	if (*s)[p1] == (*t)[p2] {
		if p2 == len(*t)-1 {
			return true
		}
		if isRight {
			if p1+1 < len(*s) && solve1553B(p1+1, p2+1, isRight, s, t) {
				return true
			} else if p1-1 >= 0 && solve1553B(p1-1, p2+1, !isRight, s, t) {
				return true
			}
		} else {
			if p1-1 >= 0 && solve1553B(p1-1, p2+1, isRight, s, t) {
				return true
			}
		}
	}
	return false
}

package main

import (
	"fmt"
	"os"
)

var ins = os.Stdin
var out = os.Stdout

func scan(a ...interface{}) (int, error)             { return fmt.Fscan(ins, a...) }
func printf(s string, a ...interface{}) (int, error) { return fmt.Fprintf(out, s, a...) }

func main() {
	var a int
	scan(&a)
	for c:=0; c<a;c++{
		var s,t string
		scan(&s, &t)
		printf("%s\n", fn(s,t))
	}
}

func fn(s string, t string) string{
	for i:=0; i<len(s); i++{
		if s[i] == t[0]{
			if check(s,t,i){
				return "YES"
			}
		}
	}
	return "NO"

}

func check(s,t string, i int) bool{
	if passleft(s,t,i, 0){
		return true
	}
	i++
	j:=1
	for ; i<len(s) && j < len(t); j++{
		if passleft(s,t,i, j){
			return true
		}
		if t[j] != s[i]{
			break
		}
		i++
	}
	i--
	j--
	if passleft(s,t,i, j){
		return true
	}
	return false
}

func passleft(s,t string, i,j int) bool {
	for ; i>=0 && j < len(t); i,j=i-1, j+1{
		if s[i] != t[j]{
			return false
		}
	}
	if j >= len(t){
		return true
	}
	return false
}

package main
import (. "fmt")
func main() {
  var q int;
  for Scan(&q); q > 0; q-- {
    var s, t string
    Scan(&s, &t)
    n := len(s)
    ans := false
    for i := 0; i < n; i++ {
      for j := i; j < n && j - i + 1 <= len(t); j++ {
        if s[j] != t[j - i] {
          break
        }
        if len(t) > j - i + 1 + j {
          continue
        }
        ok := true
        for k := 0; k < len(t) - (j - i + 1); k++ {
          if t[j - i + 1 + k] != s[j - k - 1] {
            ok = false;
          }
        }
        if ok {
          ans = true
        }
      }
    }
    if ans {
      Println("YES")
    } else {
      Println("NO")
    }
  }
}

package main

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

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

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

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		a := _string()
		b := _string()
		ans := false
		for i := range a {
			sub := a[:len(a)-i]
			if len(sub) == 0 {
				continue
			}
			suff := sub[:len(sub)-1]
			sub += reverse(suff)
			// printf("contains %s %s\n", sub, b)
			ans = ans || strings.Contains(sub, b)
			if ans {
				break
			}
		}
		if ans {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func reverse(s string) string {
	tmp := []byte(s)
	l := 0
	r := len(tmp) - 1
	for l < r {
		tmp[l], tmp[r] = tmp[r], tmp[l]
		l++
		r--
	}
	return string(tmp)
}

package main

import (
	"fmt"
)

const left = "left"
const right = "right"

type Tree struct {
	left  *Tree
	value string
	until string
	side  string
	right *Tree
}

func main() {
	var n int
	var t, s string
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		ok := false
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
		for i := 0; i < len(s); i++ {
			if ok {
				break
			}
			if s[i] == t[0] {
				st := ""
				for j := i; j < len(s); j++ {
					if ok {
						break
					}
					st += string(s[j])
					str := st
					if str == t {
						fmt.Println("Yes")
						ok = true
						break
					}
					if j == 0 {
						continue
					}
					for e := j - 1; e >= 0; e-- {
						str += string(s[e])
						if len(str) > len(t) {
							break
						}
						if str == t {
							fmt.Println("Yes")
							ok = true
							break
						}
					}
				}
			}
		}

		if !ok {
			fmt.Println("No")
		}
	}
}

package main

import (
	"fmt"
)

const left = "left"
const right = "right"

type Tree struct {
	left  *Tree
	value string
	until string
	side  string
	right *Tree
}

func main() {
	var n int
	var t, s string
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		ok := false
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		for i := 0; i < len(s); i++ {
			if ok {
				break
			}
			if s[i] == t[0] {
				st := ""
				for j := i; j < len(s); j++ {
					if ok {
						break
					}
					st += string(s[j])
					if j == 0 {
						continue
					}
					str := st
					if str == t {
						fmt.Println("Yes")
						ok = true
						break
					}
					for e := j - 1; e >= 0; e-- {
						str += string(s[e])
						if str == t {
							fmt.Println("Yes")
							ok = true
							break
						}
					}
				}
			}
		}

		if !ok {
			fmt.Println("No")
		}
	}
}

package main

import (
	"fmt"
)

const left = "left"
const right = "right"

type Tree struct {
	left  *Tree
	value string
	until string
	side  string
	right *Tree
}

func main() {
	var n int
	var t, s string
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Println(s, t)
	}
}

package main

import (
	"fmt"
)

const left = "left"
const right = "right"

type Tree struct {
	left  *Tree
	value string
	until string
	side  string
	right *Tree
}

func main() {
	var n int
	var t, s string
	fmt.Scanf("%d\n", &n)
	fmt.Println(n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%s\n", &s)
		fmt.Scanf("%s\n", &t)
		fmt.Println(s)
	}
}

package main

import ."fmt"
//import "sort"
import "bufio"
import "os"
//import "strconv"
//import "math"
//import "string"
//import "time"
//import ."math"

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

func main() {
    defer writer.Flush()

    t := sint64()
    for i := int64(0); i < t; i++ {
        solve()
    }

}
func solve() {

    //n := sint64()
    a := sstring()
    b := sstring()
    m := make([]int64, 0)
    for i := range a {
        if a[i] == b[0] {
            m = append(m, int64(i))
        }
    }
    flh := true
    for _, ind := range m {
        cur := ind
        count := int64(0)
        if(len(b) == 1){
            printf("yEs\n")
            flh = false
            break
        }
        flag := false
        for ; cur < int64(len(a)) && a[cur] == b[count]; cur++{
            count++
            //printf("%q",a[cur])
            if count == int64(len(b)) {
                flag = true
                break
            }
        }
        if flag {
            printf("yEs\n")
            flh = false
            break
        }
        if cur == int64(len(a)) && a[cur-1] == b[count-1] {
            cur --
            cur--
        } else {
            cur--
            cur--
        }

        for ; cur > -1 && a[cur] == b[count]; cur--{
            count++
            //printf("%q",a[cur])
            if count == int64(len(b)) {
                flag = true
                break
            }
        }
        if flag {
            flh = false
            printf("yEs\n")
            break
        }
        //Println()
    }
    if flh {
        printf("nO\n")
    }

}
func resh(n int64) []int64 {
    p := make([]bool, n + 1)
    for i := int64(2); i <= n; i++ {
        p[i] = true
    }
    for i := int64(0); i <= n; i++ {
		if p[i] {
			for j := i * i; j <= n; j += i {
				p[j] = false
				//Print(j, " ")
			}
		}
	}
	ans := make([]int64, 0)
	for i := int64(0); i < n; i++ {
	    if p[i] {
	        ans = append(ans, i)
	    }
	}
	return ans
}
func printf(f string, a ...interface{}) {
	Fprintf(writer, f, a...)
}
func sstring() string {
	scanner.Scan()
	return scanner.Text()
}
func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sum(a []int64) int64{
    ans := int64(0)
    for _, i := range a {
        ans += i
    }
    return ans
}
func abs(x int64) int64 {
    if x < 0 {
        return 0 - x
    }
    return x
}
func min(x, y int64) int64 {
    if x < y {
        return x
    }
    return y
}
func max(x, y int64) int64 {
    if x > y {
        return x
    }
    return y
}
func ceil(a, b int64) int64{
    s := a / b
    if a % b >= 0 {
        s++
    }
    if s == 0 {
        s = 1
    }
    return s
}
func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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 s1, s2 string
		fmt.Fscan(in, &s1, &s2)

		var f bool
		for i := 0; i < len(s1); i++ {
			if s1[i] != s2[0] {
				continue
			}

			p1 := i
			p2 := 1
			for p2 < len(s2) {
				if p2 == 1 && p1+1 >= len(s1) {
					break
				} else if p1+1 < len(s1) && s1[p1+1] == s2[p2] {
					p1++
					p2++
				} else if p1-1 >= 0 && s1[p1-1] == s2[p2] {
					p1--
					p2++
				} else {
					break
				}
			}

			if p2 == len(s2) {
				f = true
				break
			}
		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

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 s1, s2 string
		fmt.Fscan(in, &s1, &s2)

		var f bool
		for i := 0; i < len(s1); i++ {
			if s1[i] != s2[0] {
				continue
			}

			p1 := i
			p2 := 1
			for p2 < len(s2) {
				var x, y byte
				if p1+1 < len(s1) {
					x = s1[p1+1]
				}

				if p1-1 >= 0 {
					y = s1[p1-1]
				}

				if p2 == 1 && x != s2[p2] { // have to check right of
					break
				} else if x == s2[p2] {
					p1++
					p2++
				} else if y == s2[p2] {
					p1--
					p2++
				} else {
					break
				}
			}

			if p2 == len(s2) {
				f = true
				break
			}
		}
		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(r, &t)

	for k := 0; k < t; k++ {
		var s, t string
		fmt.Fscan(r, &s, &t)
		solve1553B(s, t)
	}
}
func solve1553B(s, t string) {
	m, n := len(s), len(t)
	for k := 0; k < m; k++ {
		if s[k] == t[0] {
			pos1, pos2 := k, 0
			for pos1 < m && pos2 < n && s[pos1] == t[pos2] {
				pos1++
				pos2++
			}
			pos1 -= 2 //往左匹配
			for pos1 >= 0 && pos2 < n && s[pos1] == t[pos2] {
				pos1--
				pos2++
			}
			if pos2 == n {
				fmt.Println("YES")
				return
			}
		}
	}
	fmt.Println("NO")
}

package main

import (
	"fmt"
	"os"
)

var ins = os.Stdin
var out = os.Stdout

func scan(a ...interface{}) (int, error)             { return fmt.Fscan(ins, a...) }
func printf(s string, a ...interface{}) (int, error) { return fmt.Fprintf(out, s, a...) }

func main() {
	var a int
	scan(&a)
	for c:=0; c<a;c++{
		var s,t string
		scan(&s, &t)
		printf("%s\n", fn(s,t))
	}
}

func fn(s string, t string) string{
	for i:=0; i<len(s); i++{
		if s[i] == t[0]{
			if check(s,t,i){
				return "YES"
			}
		}
	}
	return "NO"

}

func check(s,t string, i int) bool{
	if passleft(s,t,i, 0){
		return true
	}
	i++
	j:=1
	for ; i<len(s) && j < len(t); j++{
		if t[j] != s[i]{
			break
		}
		i++
	}
	i--
	j--
	if passleft(s,t,i, j){
		return true
	}
	return false
}

func passleft(s,t string, i,j int) bool {
	for ; i>=0 && j < len(t); i,j=i-1, j+1{
		if s[i] != t[j]{
			return false
		}
	}
	if j >= len(t){
		return true
	}
	return false
}