Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.

Now, they have an array $a$ of $n$ positive integers, Hossam will choose a number $a_i$ and Hazem will choose a number $a_j$.

Count the number of interesting pairs $(a_i, a_j)$ that meet all the following conditions:

$1 \le i, j \le n$;

$i \neq j$;

The absolute difference $|a_i - a_j|$ must be equal to the maximum absolute difference over all the pairs in the array. More formally, $|a_i - a_j| = \max_{1 \le p, q \le n} |a_p - a_q|$.


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$), which denotes the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer $n$ ($2 \le n \le 10^5$).

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case print an integer — the number of interesting pairs $(a_i, a_j)$.


-----Examples-----

Input
2
5
6 2 3 8 1
6
7 2 8 3 2 10
Output
2
4


-----Note-----

In the first example, the two ways are:

Hossam chooses the fourth number $8$ and Hazem chooses the fifth number $1$.

Hossam chooses the fifth number $1$ and Hazem chooses the fourth number $8$.

In the second example, the four ways are:

Hossam chooses the second number $2$ and Hazem chooses the sixth number $10$.

Hossam chooses the sixth number $10$ and Hazem chooses the second number $2$.

Hossam chooses the fifth number $2$ and Hazem chooses the sixth number $10$.

Hossam chooses the sixth number $10$ and Hazem chooses the fifth number $2$.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Combinatorics

Mathematics


Solutions:
from math import comb as cx
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if len(set(l)) == 1:
		print(len(l) * (len(l) - 1))
	else:
		z = l.count(max(l))
		c = l.count(min(l))
		print(c * z * 2)


t = int(input())

def solve(n, arr):
	(maxval, minval) = (arr.count(max(arr)), arr.count(min(arr)))
	if min(arr) != max(arr):
		return 2 * maxval * minval
	return maxval * (maxval - 1)
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = min(l)
	b = max(l)
	if a != b:
		x = l.count(a)
		y = l.count(b)
		print(2 * x * y)
	else:
		print(n * (n - 1))


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	M = max(l)
	a = l.count(m)
	b = l.count(M)
	if a != b:
		ans = a * b * 2
	elif a == n:
		ans = n * (n - 1)
	else:
		ans = a * b * 2
	print(ans)
	t = t - 1


t = int(input(''))
for z in range(t):
	n = int(input(''))
	a = list(map(int, input('').split()))[:n]
	if max(a) == min(a):
		print((n - 1) * n)
	else:
		ma = a.count(max(a))
		mi = a.count(min(a))
		print(ma * mi * 2)


tests = int(input())

def inp():
	input()
	return list(map(int, input().split(' ')))
result = []
for i in range(tests):
	nums = inp()
	low = min(nums)
	high = max(nums)
	if low != high:
		low_num = nums.count(low)
		high_num = nums.count(high)
		result.append(low_num * high_num * 2)
	else:
		a = nums.count(low)
		result.append(a * (a - 1))
for i in result:
	print(i)


tests = int(input())
for test in range(tests):
	n = int(input())
	arr = list(map(int, input().split()))
	ai = min(arr)
	aj = max(arr)
	sol = 2 * arr.count(ai) * arr.count(aj)
	if ai == aj:
		sol = n * (n - 1)
	print(sol)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mx = max(a)
	mn = min(a)
	if mn == mx:
		new = n * (n - 1) // 2
		print(new * 2)
	else:
		new = a.count(mx) * a.count(mn)
		print(new * 2)


test_cases = int(input())
for x in range(test_cases):
	num = int(input())
	arr = list(map(int, input().split()))
	a = min(arr)
	b = max(arr)
	if a == b:
		print(num * (num - 1))
		continue
	print(arr.count(a) * arr.count(b) * 2)


for s in [*open(0)][2::2]:
	a = (*map(int, s.split()),)
	print(2 * a.count((m := min(a))) * (a.count((M := max(a))) - (f := (M == m))) // (1 + f))


def solve():
	n = int(input())
	lst = list(map(int, input().split()))
	ans = 0
	mini = min(lst)
	maxi = max(lst)
	cnt1 = lst.count(mini)
	cnt2 = lst.count(maxi)
	if mini == maxi:
		ans = n * (n - 1)
	else:
		ans = 2 * cnt1 * cnt2
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for i in range(int(input())):
	z = int(input())
	li = list(map(int, input().split()))
	x = min(li)
	y = max(li)
	if x != y:
		print(li.count(x) * li.count(y) * 2)
	else:
		print(z * (z - 1))


tests = int(input())
for _ in range(tests):
	leniii = int(input())
	li = list(map(int, input().split()))
	if min(li) == max(li):
		print(leniii ** 2 - leniii)
		continue
	print(li.count(min(li)) * li.count(max(li)) * 2)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxi = max(arr)
	mini = min(arr)
	macount = arr.count(maxi)
	micount = arr.count(mini)
	if maxi == mini:
		ans = macount * (macount - 1)
	else:
		ans = macount * micount * 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = min(a)
	y = max(a)
	nx = a.count(x)
	ny = a.count(y)
	if nx == n:
		print(n * (n - 1))
	else:
		print(2 * nx * ny)


for t in range(0, int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	m1 = min(s)
	m2 = max(s)
	if m1 != m2:
		print(s.count(m1) * s.count(m2) * 2)
	else:
		print(len(s) * (len(s) - 1))


t = int(input())
for j in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = min(a)
	a2 = max(a)
	a11 = a.count(a1)
	a22 = a.count(a2)
	if a11 == n:
		print(n * (n - 1))
	else:
		print(2 * a11 * a22)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	maxa = max(a)
	mina = min(a)
	if maxa != mina:
		print(2 * a.count(maxa) * a.count(mina))
	else:
		print(n * (n - 1))


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	miner = min(arr)
	maxer = max(arr)
	miners = arr.count(miner)
	maxers = arr.count(maxer)
	if maxer == miner:
		print(maxers * (maxers - 1))
	else:
		print(2 * miners * maxers)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = max(l)
	s = min(l)
	a = l.count(s)
	b = l.count(m)
	print(a * b * 2) if m != s else print(a * (a - 1))


for z in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(min1, max1) = (min(arr), max(arr))
	(l1, l2) = (arr.count(min1), arr.count(max1))
	if min1 == max1:
		print(l1 * (l2 - 1))
	else:
		print(2 * l1 * l2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(sep=' ')))
	flag = 1
	if a.count(min(a)) == n:
		flag = 0
	if flag == 1:
		ma = max(a)
		mi = min(a)
		x = a.count(ma)
		y = a.count(mi)
		print(x * y * 2)
	else:
		print(n * (n - 1))


n = int(input())
for i in range(n):
	b = int(input())
	l = list(map(int, input().split()))
	minimum = min(l)
	maximum = max(l)
	if minimum == maximum:
		print(b * (b - 1))
	else:
		min_count = l.count(minimum)
		max_count = l.count(maximum)
		print(2 * min_count * max_count)


for t in range(int(input())):
	n = int(input())
	a = [int(s) for s in input().split()]
	if max(a) == min(a):
		print(n * (n - 1))
	else:
		print(2 * a.count(max(a)) * a.count(min(a)))


def foo2(arr):
	if len(arr) == 1:
		return 2
	(max_val, min_val) = (max(arr), min(arr))
	if max_val == min_val:
		return len(arr) * (len(arr) - 1)
	(a, b) = (arr.count(max_val), arr.count(min_val))
	return 2 * a * b
test_size = int(input())
final = []
for i in range(test_size):
	size = input()
	temp = input().split()
	temp = [int(i) for i in temp]
	final.append(foo2(temp))
for i in final:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	mmax = max(a)
	mmin = min(a)
	c_max = a.count(mmax)
	c_min = a.count(mmin)
	if mmax == mmin:
		print(c_max * (c_max - 1))
		continue
	print(2 * c_max * c_min)


for iji in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = max(a)
	l = min(a)
	kk = a.count(k)
	lk = a.count(l)
	if k == l:
		print(n * (n - 1))
	else:
		print(kk * lk * 2)


a = int(input())
for x in range(a):
	b = int(input())
	c = input().split()
	c = list(map(int, c))
	if len(set(c)) != 1:
		result = c.count(max(c)) * c.count(min(c)) * 2
	else:
		result = b * (b - 1)
	print(result)


for _ in range(int(input())):
	(n, a) = (int(input()), list(map(int, input().split())))
	if max(a) == min(a):
		print(n * n - n)
	else:
		print(a.count(max(a)) * a.count(min(a)) * 2)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = max(l)
	b = min(l)
	c = l.count(a)
	d = l.count(b)
	if a == b:
		print(c * (c - 1))
	else:
		print(2 * c * d)


for loop in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	amax = a.count(max(a))
	if max(a) == min(a):
		print(n * (n - 1))
	else:
		amin = a.count(min(a))
		print(int(amin * amax * 2))


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	minn = min(a)
	maxx = max(a)
	if minn == maxx:
		print(n * (n - 1))
	else:
		print(a.count(minn) * a.count(maxx) * 2)
	t -= 1


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	e = max(l)
	w = min(l)
	t = l.count(e)
	o = l.count(w)
	if e == w:
		print(t * (o - 1))
	else:
		print(o * t * 2)


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt1 = a.count(min(a))
	if cnt1 == n:
		print(n * (n - 1))
	else:
		cnt2 = a.count(max(a))
		print(cnt1 * cnt2 * 2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	l1 = a.count(a[0])
	l2 = a.count(a[n - 1])
	if l1 == n or l2 == n:
		print(n * (n - 1))
	else:
		print(2 * l1 * l2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	b = a.count(max(a))
	c = a.count(min(a))
	if min(a) != max(a):
		l = 2 * c * b
		print(l)
	else:
		print(n * (n - 1))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mn = min(a)
	mx = max(a)
	c1 = a.count(mn)
	c2 = a.count(mx)
	if mn == mx:
		print(n * (n - 1))
	else:
		print(c1 * c2 * 2)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	mxc = a.count(max(a))
	mic = a.count(min(a))
	m = max(a) - min(a)
	if m == 0:
		c = n * (n - 1)
	else:
		c = mxc * mic * 2
	print(c)


import math
t = int(input())
for i in range(t):
	c = 0
	n = int(input())
	l = list(map(int, input().split()))
	r = max(l) - min(l)
	if r == 0:
		print(n * (n - 1))
	else:
		d = max(l)
		e = min(l)
		print(l.count(d) * l.count(e) * 2)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mini = a.count(min(a))
	maxi = a.count(max(a))
	if len(set(a)) == 1:
		print(n * (n - 1))
	else:
		print(mini * maxi * 2)


for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	min_ = a.count(a[0])
	max_ = a.count(a[-1])
	if a[0] == a[-1]:
		print(min_ * (min_ - 1))
	else:
		print(2 * (min_ * max_))


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	lowest = min(arr)
	highest = max(arr)
	if lowest == highest:
		print(n * (n - 1))
		return
	frontequal = 0
	backequal = 0
	for i in range(n):
		if arr[i] == lowest:
			frontequal += 1
		if arr[i] == highest:
			backequal += 1
	print(frontequal * backequal * 2)
tc = int(input())
for _ in range(tc):
	solve()


t = int(input())
import math
for i in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	result = 0
	(minimum, maksimum) = (min(array), max(array))
	if minimum == maksimum:
		result = n * (n - 1)
	else:
		result = array.count(minimum) * array.count(maksimum) * 2
	print(result)


for _ in range(int(input())):
	n = int(input())
	j = [int(i) for i in input().split(' ')]
	ma = j.count(max(j))
	mi = j.count(min(j))
	if min(j) == max(j):
		print(ma * (mi - 1))
	else:
		print(ma * mi * 2)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	(p, q) = (max(a), min(a))
	if p != q:
		print(2 * a.count(q) * a.count(p))
	else:
		print(n * (n - 1))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxi = max(arr)
	mini = min(arr)
	if maxi - mini == 0:
		res = n * (n - 1)
	else:
		c_max = arr.count(maxi)
		c_min = arr.count(mini)
		res = c_max * c_min * 2
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ma = max(arr)
	mi = min(arr)
	count = arr.count(ma)
	count1 = arr.count(mi)
	if ma == mi:
		print(n * (n - 1))
	else:
		print(count * count1 * 2)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a.count(a[0]) == n:
		print(n * (n - 1))
	else:
		print(a.count(min(a)) * a.count(max(a)) * 2)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(arr.count(min(arr)) * arr.count(max(arr)) * 2 if min(arr) != max(arr) else n * (n - 1))


def main():
	t = int(input())
	while t > 0:
		n = int(input())
		arr = list(map(int, input().split()))
		min_arr = min(arr)
		max_arr = max(arr)
		count_min = 0
		count_max = 0
		if min_arr == max_arr:
			pairs_cnt = n * (n - 1)
		else:
			for num in arr:
				if num == min_arr:
					count_min += 1
				if num == max_arr:
					count_max += 1
			pairs_cnt = count_min * count_max * 2
		print(pairs_cnt)
		t = t - 1
main()


amount = int(input())
for a in range(amount):
	length = int(input())
	array = input()
	array = array.split()
	array = [int(a) for a in array]
	mini = min(array)
	maxi = max(array)
	if mini == maxi:
		print(len(array) * (len(array) - 1))
	else:
		count_mini = array.count(mini)
		count_maxi = array.count(maxi)
		count_mini = int(count_mini)
		count_maxi = int(count_maxi)
		print(count_mini * count_maxi * 2)


for _ in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	maxi = max(k)
	mini = min(k)
	print(2 * (k.count(maxi) * k.count(mini)) if maxi != mini else n * (n - 1))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = min(l)
	b = max(l)
	if a == b:
		print(n * (n - 1))
		continue
	x = l.count(a) * l.count(b) * 2
	print(x)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	mini = min(lst)
	maxi = max(lst)
	ctmi = lst.count(mini)
	ctmx = lst.count(maxi)
	if mini == maxi:
		print((ctmi - 1) * ctmx)
	else:
		print(ctmi * 2 * ctmx)


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = a.count(max(a))
	s = a.count(min(a))
	if l == n:
		print(n * (n - 1))
		continue
	print(2 * l * s)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	m = min(a)
	M = max(a)
	if m == M:
		print(n * (n - 1))
	else:
		m = a.count(m)
		M = a.count(M)
		print(m * M * 2)
for _ in range(int(input())):
	solve()


t = int(input())
for x in range(t):
	l = int(input())
	li = [int(i) for i in input().split()]
	m = min(li)
	M = max(li)
	if m == M:
		print(l * (l - 1))
	else:
		m = li.count(m)
		M = li.count(M)
		print(m * M * 2)


def solution():
	n = int(input())
	arr = list(map(int, input().split()))
	if max(arr) == min(arr):
		n = n - 1
		interesting_pairs = n * (n + 1)
	else:
		o_max = arr.count(max(arr))
		o_min = arr.count(min(arr))
		interesting_pairs = o_max * o_min * 2
	print(interesting_pairs)
t = int(input())
for i in range(t):
	solution()


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	an = [int(ai) for ai in s.split(' ')]
	min_num = min(an)
	max_num = max(an)
	min_count = an.count(min_num)
	max_count = an.count(max_num)
	if min_num == max_num:
		print(n * (n - 1))
	else:
		print(min_count * max_count * 2)


def f(lst):
	a = max(lst)
	b = min(lst)
	if a == b:
		n = len(lst)
		return n * (n - 1)
	else:
		return 2 * lst.count(a) * lst.count(b)
t = int(input())
for i in range(t):
	n = int(input())
	inp = input()
	lst = list(map(int, inp.split(' ')))
	print(f(lst))


n = int(input())
l1 = []
l2 = []
for i in range(n):
	l1.append(int(input()))
	l2.append([int(x) for x in input().split()])
for i in range(n):
	a = max(l2[i])
	b = min(l2[i])
	if a == b:
		print(l1[i] * (l1[i] - 1))
	else:
		print(l2[i].count(a) * l2[i].count(b) * 2)
print()


n = int(input())
for i in range(n):
	k = int(input())
	l = list(map(int, input().split()))
	g = l.count(min(l))
	y = l.count(max(l))
	if y == k:
		print(y * (y - 1))
	else:
		print(g * y * 2)


t = int(input())
for i in range(t):
	n = int(input())
	lst = [int(i) for i in input().split()]
	if min(lst) == max(lst):
		print(n * (n - 1))
	else:
		mx = lst.count(max(lst))
		mn = lst.count(min(lst))
		print(mx * mn * 2)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	if max(A) == min(A):
		print(N * (N - 1))
	else:
		print(A.count(max(A)) * A.count(min(A)) * 2)


num_test_cases = int(input())
for _ in range(num_test_cases):
	n = int(input())
	arr = sorted(map(int, input().split()))
	if arr[0] == arr[-1]:
		print(n * (n - 1))
		continue
	res = 2 * arr.count(arr[0]) * arr.count(arr[-1])
	print(res)


n = int(input())
for j in range(n):
	f = int(input())
	a = list(map(int, input().strip().split()))
	if max(a) == min(a):
		print(f * (f - 1))
	else:
		print(2 * a.count(max(a)) * a.count(min(a)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m1 = max(a)
	m2 = min(a)
	if m1 == m2:
		p = n * (n - 1)
	else:
		p = 2 * a.count(m1) * a.count(m2)
	print(p)


t = int(input())
for x in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	smallest = min(l)
	largest = max(l)
	num_s = l.count(smallest)
	num_l = l.count(largest)
	if smallest == largest:
		print(num_l * (num_s - 1))
	else:
		print(2 * num_l * num_s)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(Max, Min) = (max(a), min(a))
	(maxcount, mincount) = (a.count(Max), a.count(Min))
	if Max == Min:
		print(maxcount * (mincount - 1) if maxcount > 1 and mincount > 1 else 0)
	else:
		print(mincount * maxcount * 2)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mini_count = l.count(min(l))
	maxi_count = l.count(max(l))
	ins = 0
	if max(l) == min(l):
		print(mini_count * (mini_count - 1))
	else:
		print(maxi_count * mini_count * 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maximum = max(a)
	minimum = min(a)
	maxmindiff = maximum - minimum
	if maxmindiff == 0:
		print(n * (n - 1))
	else:
		count_min = a.count(minimum)
		count_max = a.count(maximum)
		print(2 * (count_max * count_min))


import sys
input = sys.stdin.readline

def readList():
	return list(map(int, input().split()))

def readInt():
	return int(input())

def readInts():
	return map(int, input().split())

def readStr():
	return input().strip()

def solve():
	n = readInt()
	arr = readList()
	(mi, ma) = (min(arr), max(arr))
	cnt1 = cnt2 = 0
	for i in range(n):
		if arr[i] == mi:
			cnt1 += 1
		elif arr[i] == ma:
			cnt2 += 1
	if mi == ma:
		return cnt1 * (cnt1 - 1)
	else:
		return cnt1 * cnt2 * 2
for _ in range(int(input())):
	print(solve())


testcase_no = int(input())
for _ in range(testcase_no):
	n = int(input())
	numbers = list(map(int, input().split()))
	max_element = max(numbers)
	min_element = min(numbers)
	if max_element == min_element:
		print(n * (n - 1))
	else:
		print(numbers.count(max_element) * numbers.count(min_element) * 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	M = max(a)
	if m == M:
		answer = n * (n - 1)
	else:
		cnt_min = 0
		cnt_max = 0
		for x in a:
			if x == m:
				cnt_min += 1
			if x == M:
				cnt_max += 1
		answer = 2 * cnt_min * cnt_max
	print(answer)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt1 = arr.count(min(arr))
	cnt2 = arr.count(max(arr))
	if len(set(arr)) == 1:
		print(n * (n - 1))
	else:
		print(cnt1 * cnt2 * 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = max(a)
	c = min(a)
	e = a.count(b)
	f = a.count(c)
	if b == c:
		print(n * (n - 1))
	else:
		print(e * f * 2)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxx = max(arr)
	minn = min(arr)
	mcount = arr.count(maxx)
	if maxx == minn:
		print(mcount ** 2 - mcount)
	else:
		mincount = arr.count(minn)
		for1 = mcount * 2
		for_m = for1 * mincount
		print(for_m)


try:
	for _ in range(int(input())):
		n = int(input())
		lst = list(map(int, input().split()))
		mins = lst.count(min(lst))
		maxs = lst.count(max(lst))
		if min(lst) == max(lst):
			print(mins * (maxs - 1))
			continue
		print(2 * mins * maxs)
except Exception as e:
	print(e)