You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that:   $1 \le i < j < k < l \le n$;  $a_i = a_k$ and $a_j = a_l$; 


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$.

It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$.


-----Output-----

For each test case, print the number of described tuples.


-----Example-----
Input
2
5
2 2 2 2 2
6
1 3 3 1 2 3

Output
5
2



-----Note-----

In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples.

In the second test case, there are $2$ valid tuples:   $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$;  $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.

Source: codeforces | Difficulty: HARD
Tags: Combinatorics

Amortized analysis

Complete search

Mathematics

Data structures


Solutions:
import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	cnt_l = [0] * (n + 1)
	ans = 0
	for i in range(n):
		cnt_r = [0] * (n + 1)
		for j in range(n - 1, i, -1):
			ans += cnt_l[a[j]] * cnt_r[a[i]]
			cnt_r[a[j]] += 1
		cnt_l[a[i]] += 1
	print(ans)


import os
import heapq
import sys, threading
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)
threading.stack_size(2 ** 27)

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = inpt()
		left = defaultdict(int)
		right = defaultdict(int)
		ans = 0
		for i in range(n - 2):
			right.clear()
			for j in range(n - 1, i, -1):
				if ar[j] in left and ar[i] in right:
					ans += left[ar[j]] * right[ar[i]]
				right[ar[j]] += 1
			left[ar[i]] += 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()
	left = defaultdict(int)
	right = defaultdict(int)
	ans = 0
	for i in range(1, n - 2):
		left[a[i - 1]] += 1
		for j in range(n - 2, i, -1):
			right[a[j + 1]] += 1
			ans += left[a[j]] * right[a[i]]
		right.clear()
	print(ans)


for _ in [0] * int(input()):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	bOne = [0] * (n + 2)
	for j in range(1, n - 2, 1):
		bOne[arr[j - 1]] += 1
		bSec = [0] * (n + 2)
		added = 0
		for l in range(j + 2, n, 1):
			v = arr[l - 1]
			added -= bSec[v] * bOne[v]
			bSec[v] += 1
			added += bSec[v] * bOne[v]
			if arr[j] == arr[l]:
				c += added
	print(c)


from collections import Counter, defaultdict
from functools import lru_cache, reduce
import operator
t = int(input())
x = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	h1 = Counter(a)
	h2 = Counter()
	res = 0
	for i in range(n):
		h1[a[i]] -= 1
		s = 0
		for j in range(i + 1, n):
			s -= h1[a[j]] * h2[a[j]]
			h1[a[j]] -= 1
			if j > i + 1:
				if a[j] != a[j - 1]:
					s -= h1[a[j - 1]] * h2[a[j - 1]]
				h2[a[j - 1]] += 1
				if a[j] != a[j - 1]:
					s += h1[a[j - 1]] * h2[a[j - 1]]
			s += h1[a[j]] * h2[a[j]]
			if a[i] == a[j]:
				res += s
		h2[a[n - 1]] += 1
		(h1, h2) = (h2, h1)
	x.append(res)
print(*x, sep='\n')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d1 = [0] * 3002
	ans = 0
	for j in range(n):
		d2 = [0] * 3002
		for k in range(n - 1, j, -1):
			ans += d1[l[k]] * d2[l[j]]
			d2[l[k]] += 1
		d1[l[j]] += 1
	print(ans)


class BIT:

	def __init__(self, n):
		self.BIT = [0] * (n + 1)
		self.num = n

	def query(self, idx):
		res_sum = 0
		while idx > 0:
			res_sum += self.BIT[idx]
			idx -= idx & -idx
		return res_sum

	def update(self, idx, x):
		while idx <= self.num:
			self.BIT[idx] += x
			idx += idx & -idx
		return
import sys, random
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pair = [[] for i in range(n + 1)]
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] == a[j]:
				pair[i + 1].append(j + 1)
	bit = BIT(n)
	ans = 0
	for i in range(1, n + 1):
		minus = bit.query(i)
		for r in pair[i]:
			ans += bit.query(r - 1) - minus
		for r in pair[i]:
			bit.update(r, 1)
	print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

def solve(arr, n):
	cntLeft = dict()
	cntRight = dict()
	res = 0
	for j in range(n):
		cntRight = dict()
		for k in range(n - 1, j, -1):
			res += cntLeft.get(arr[k], 0) * cntRight.get(arr[j], 0)
			if arr[k] not in cntRight:
				cntRight[arr[k]] = 1
			else:
				cntRight[arr[k]] += 1
		if arr[j] not in cntLeft:
			cntLeft[arr[j]] = 1
		else:
			cntLeft[arr[j]] += 1
	return res
t = inp()
while t > 0:
	n = inp()
	arr = inlt()
	print(solve(arr, n))
	t -= 1


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		prefix = [0] * (n + 1)
		tuples = 0
		for i in range(1, n - 2):
			prefix[a[i - 1]] += 1
			i_after_j = 0
			for j in range(n - 2, i, -1):
				i_after_j += a[j + 1] == a[i]
				tuples += prefix[a[j]] * i_after_j
		print(tuples)
prog()


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	h2 = [0] * (n + 1)
	h1 = [0] * (n + 1)
	for x in a:
		h1[x] += 1
	res = 0
	for i in range(n):
		h1[a[i]] -= 1
		if i < n - 1:
			h1[a[i + 1]] -= 1
		s = 0
		for j in range(i + 2, n):
			s -= h1[a[j]] * h2[a[j]]
			h1[a[j]] -= 1
			if a[j] != a[j - 1]:
				s -= h1[a[j - 1]] * h2[a[j - 1]]
			h2[a[j - 1]] += 1
			if a[j] != a[j - 1]:
				s += h1[a[j - 1]] * h2[a[j - 1]]
			s += h1[a[j]] * h2[a[j]]
			if a[i] == a[j]:
				res += s
		h2[a[n - 1]] += 1
		(h1, h2) = (h2, h1)
	print(res)


dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

def solve(n, ar):
	l_count = [0] * (n + 100)
	ans = 0
	for i in range(n):
		r_count = [0] * (n + 100)
		for j in range(n - 1, i, -1):
			ans += l_count[ar[j]] * r_count[ar[i]]
			r_count[ar[j]] += 1
		l_count[ar[i]] += 1
	print(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	r = [0] * (n + 1)
	l = [0] * (n + 1)
	ans = 0
	for i in range(n):
		r = [0 for i in range(n + 1)]
		for j in range(n - 1, i, -1):
			ans += r[lis[i]] * l[lis[j]]
			r[lis[j]] += 1
		l[lis[i]] += 1
	print(ans)


import sys
import bisect
input = sys.stdin.readline
t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pref = [[0 for j in range(3001)] for i in range(n + 2)]
	for i in range(n):
		pref[i][l[i]] += 1
		for j in range(3001):
			if i >= 1:
				pref[i][j] += pref[i - 1][j]
	ans = 0
	for j in range(n - 2):
		for k in range(j + 1, n - 1):
			cnt1 = pref[j - 1][l[k]]
			cnt2 = pref[n - 1][l[j]] - pref[k][l[j]]
			ans += cnt1 * cnt2
	print(ans)


import sys, bisect
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	B = [[] for i in range(n)]
	for i in range(n):
		for j in range(i + 1, n):
			if A[i] == A[j]:
				B[i].append(j)
	ans = 0
	C = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			C[i][j] = C[i][j - 1] + len(B[j]) - bisect.bisect(B[j], i)
	for i in range(n):
		for j in B[i]:
			ans = ans + C[j][j - 1] - C[j][i]
	print(ans)


from collections import Counter
from sys import stdin, stdout

def input():
	return stdin.readline().strip()
import operator as op
from functools import reduce

def ncr(n, r):
	if r > n:
		return 0
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def ans_slow(N, A):
	ret = 0
	for i in range(N):
		for j in range(i + 1, N):
			for k in range(j + 1, N):
				for l in range(k + 1, N):
					if A[i] == A[k] and A[j] == A[l]:
						ret += 1
	return ret

def ans_fast(N, A):
	cA = Counter(A)
	front = Counter()
	num_closes = [0] * (N + 10)
	front_weight = []
	last = []
	for i in range(0, N + 10):
		row = [0] * (N + 10)
		front_weight += [row]
		row2 = [0] * (N + 10)
		last += [row2]
	ret = 0
	for a in A:
		front[a] += 1
		for i in range(1, N + 1):
			if a == i:
				continue
			front_weight[a][i] += last[a][i] * num_closes[a]
			back_weight = cA[i] - front[i]
			if front_weight[a][i] + back_weight > 0:
				ret += front_weight[a][i] * back_weight
			last[a][i] = 0
			last[i][a] += 1
		num_closes[a] += 1
	for k in cA:
		v = cA[k]
		ret += ncr(v, 4)
	return ret

def ans(N, A):
	return ans_fast(len(A), A)
T = int(input())
for t in range(T):
	N = int(input())
	S = list(map(int, input().split()))
	print(ans(N, S))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	count = [[0] * (n + 1) for _ in range(n + 1)]
	ans = 0
	for i in range(n):
		for j in range(i):
			count[s[j]][s[i]] += 1
		for j in range(i + 2, n):
			ans += count[s[i + 1]][s[j]]
	print(ans)


for _ in [0] * int(input()):
	(n, o, c) = (int(input()), list(map(int, input().split())), 0)
	b = [0] * (n + 2)
	for j in range(1, n - 2):
		b[o[j - 1]] += 1
		q = [0] * (n + 2)
		a = 0
		for l in range(j + 2, n):
			v = o[l - 1]
			a -= q[v] * b[v]
			q[v] += 1
			a += q[v] * b[v]
			c += a * (o[j] == o[l])
	print(c)


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def printd(*args, **kwargs):
	pass

def get_str():
	return input().decode().strip()

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

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	a = list(rint())
	for i in range(n):
		a[i] -= 1
	freq = [0] * n ** 2
	ans = 0
	for j in range(n - 1, -1, -1):
		k = j + 1
		for l in range(k + 1, n):
			freq[n * a[k] + a[l]] += 1
		for i in range(j):
			ans += freq[n * a[i] + a[j]]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = [[0] * n for _ in range(n)]
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] == a[j]:
				d[i][j] = 1
	for i in range(n):
		for j in range(n - 1):
			d[i][j + 1] += d[i][j]
	for i in range(n - 1):
		for j in range(n):
			d[i + 1][j] += d[i][j]
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] == a[j]:
				ans += d[j - 1][n - 1] - d[j - 1][j] - d[i][n - 1] + d[i][j]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	l = [0] * (n + 1)
	for j in range(1, n - 1):
		l[a[j - 1]] += 1
		r = [0] * (n + 1)
		for k in range(n - 2, j, -1):
			r[a[k + 1]] += 1
			res += l[a[k]] * r[a[j]]
	print(res)


import io, os
from collections import deque
import bisect
RL = lambda : list(map(int, input().split(' ')))
T = int(input())

def comb4(n):
	return n * (n - 1) * (n - 2) * (n - 3) // 24

def countPairs(l1, l2):
	n1 = len(l1)
	n2 = len(l2)
	ans = 0
	greater = []
	for i in range(n2):
		ind = bisect.bisect_left(l1, l2[i])
		greater.append(n1 - ind)
	for i in range(n2 - 2, -1, -1):
		greater[i] += greater[i + 1]
	for i in range(n1):
		ind = bisect.bisect_left(l2, l1[i])
		if ind == 0 or ind == n2:
			continue
		else:
			ans += ind * greater[ind]
	return ans
for _ in range(T):
	n = int(input())
	l = RL()
	dic = dict()
	for i in range(n):
		if l[i] not in dic.keys():
			dic[l[i]] = [i]
		else:
			dic[l[i]].append(i)
	fin = []
	su = 0
	for (k, v) in dic.items():
		if len(v) > 1:
			fin.append(v)
		if len(v) > 3:
			su += comb4(len(v))
	for i in range(len(fin)):
		for j in range(len(fin)):
			if i != j:
				su += countPairs(fin[i], fin[j])
	print(su)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	cnt1 = Counter()
	cnt2 = Counter()
	for j in range(1, n - 1):
		cnt1[a[j - 1]] += 1
		cnt2.clear()
		for k in range(n - 2, j, -1):
			cnt2[a[k + 1]] += 1
			res += cnt1[a[k]] * cnt2[a[j]]
	print(res)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	left = [0] * 3002
	ans = 0
	for i in range(n):
		right = [0] * 3002
		for j in range(n - 1, i, -1):
			ans += right[arr[i]] * left[arr[j]]
			right[arr[j]] += 1
		left[arr[i]] += 1
	print(ans)


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	occurrences = {}
	contributions = {}
	tot = 0
	for i in range(n):
		if a[i] in contributions:
			tot += contributions[a[i]]
		if a[i] in occurrences:
			occurrences[a[i]].append(i)
			passed = 1
			for j in range(occurrences[a[i]][0] + 1, i):
				if a[j] in contributions:
					contributions[a[j]] += passed
				else:
					contributions[a[j]] = passed
				if a[j] == a[i]:
					passed += 1
		else:
			occurrences[a[i]] = [i]
	print(tot)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	dic = dd(int)
	ans = 0
	for j in range(n - 1, -1, -1):
		k = j + 1
		for l in range(k + 1, n):
			dic[arr[k] * n + arr[l]] += 1
		for i in range(j):
			ans += dic[arr[i] * n + arr[j]]
	print(ans)


from collections import deque as dq
from collections import defaultdict as dd
from collections import Counter as ct
from functools import lru_cache as lc
from heapq import heappush as hpush, heappop as hpop, heapify as hfy
from bisect import bisect_left, bisect_right
import itertools as it
import math
import sys
input = sys.stdin.readline

def nCr(n, r):
	return math.factorial(n) // math.factorial(r) // math.factorial(n - r)

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	PartialCount = [[0] * (n + 1) for _ in range(n + 1)]
	for i in range(n):
		PartialCount[i + 1] = PartialCount[i].copy()
		PartialCount[i + 1][A[i]] += 1

	def get_cnt(val, left, right):
		return PartialCount[right][val] - PartialCount[left][val]
	ret = 0
	for i in range(1, n + 1):
		cnt = get_cnt(i, 0, n)
		if cnt >= 4:
			ret += nCr(cnt, 4)
	for j in range(1, n - 2):
		for k in range(j + 1, n - 1):
			if A[j] == A[k]:
				continue
			cnt_i = get_cnt(A[k], 0, j)
			cnt_l = get_cnt(A[j], k + 1, n)
			ret += cnt_i * cnt_l
	print(ret)
t = int(input())
for i in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	fromLeft = [0] * (n + 1)
	ans = 0
	for l in range(0, n):
		fromRight = [0] * (n + 1)
		for r in range(n - 1, l, -1):
			ans += fromLeft[a[r]] * fromRight[a[l]]
			fromRight[a[r]] += 1
		fromLeft[a[l]] += 1
	print(ans)


from collections import Counter
T = int(input())
for _ in range(T):
	(n, ar) = (int(input()), list(map(lambda u: int(u) - 1, input().split())))
	sm = 0
	for i in range(n):
		(left, cur) = (0, [(0, 0, 0)] * n)
		for j in range(i + 1, n):
			(lv, rv) = (ar[i], ar[j])
			if lv == rv:
				left += 1
				continue
			(total, cur_cc, left_cc) = cur[rv]
			cur[rv] = (total + (left - left_cc) * cur_cc, cur_cc + 1, left)
			sm += cur[rv][0]
	cc = Counter(ar)
	for u in cc.values():
		if u >= 4:
			sm += u * (u - 1) * (u - 2) * (u - 3) // 24
	print(sm)


def bins(l, n):
	low = -1
	high = len(l)
	while low + 1 < high:
		avg = (low + high) // 2
		if l[avg] < n:
			low = avg
		else:
			high = avg
	return [low, high]
for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	inds = {}
	for i in range(len(l)):
		if l[i] in inds:
			inds[l[i]].append(i)
		else:
			inds[l[i]] = [i]
	bl = []
	for i in inds:
		if len(inds[i]) > 1:
			bl.append(inds[i])
	ans = 0
	for i in bl:
		z = len(i)
		ans += z * (z - 1) * (z - 2) * (z - 3) // 24
	for i in range(len(bl)):
		for j in range(len(bl)):
			if i != j:
				a = bl[i]
				b = bl[j]
				if len(a) == 2 and len(b) == 2:
					if a[0] < b[0] and b[0] < a[1] and (a[1] < b[1]):
						ans += 1
				elif len(b) == 2:
					ai = bins(a, b[0])
					aj = bins(a, b[1])
					ans += ai[1] * (aj[0] - ai[0])
				else:
					binss = [0 for i in range(len(a))]
					for asdf in range(len(a)):
						binss[asdf] = bins(b, a[asdf])
					for x in range(len(a)):
						for y in range(x + 1, len(a)):
							lessthan = binss[x]
							morethan = binss[y]
							between = morethan[0] - lessthan[0]
							bigger = len(b) - morethan[1]
							ans += between * bigger
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	left = defaultdict(int)
	for i in range(n):
		right = defaultdict(int)
		for j in range(n - 1, i, -1):
			ans += left[arr[j]] * right[arr[i]]
			right[arr[j]] += 1
		left[arr[i]] += 1
	print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	ans = 0
	for j in range(n - 1, -1, -1):
		for l in range(j + 2, n):
			temp = a[j + 1] * n + a[l]
			if temp in d:
				d[a[j + 1] * n + a[l]] += 1
			else:
				d[temp] = 1
		for i in range(j):
			temp = a[i] * n + a[j]
			if temp in d:
				ans += d[temp]
	print(ans)


from sys import stdin
input = stdin.readline
q = int(input())

def po2(x):
	return x * (x - 1) // 2
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		l[i] -= 1
	pref = [[0] * (n + 1) for i in range(n)]
	for i in range(1, n + 1):
		for j in range(n):
			if j == l[i - 1]:
				pref[j][i] = pref[j][i - 1] + 1
			else:
				pref[j][i] = pref[j][i - 1]
	count = [pref[i][n] for i in range(n)]
	wynik = 0
	pary = [[0] * (n + 1) for i in range(n)]
	for i in range(n):
		if i == 0:
			continue
		for j in range(i + 1, n + 1):
			if j == i + 1:
				pary[i][j] = 0
			else:
				cu = l[j - 1]
				pary[i][j] = pary[i][j - 1] + pref[cu][i]
	wyn = 0
	for i in range(n):
		for j in range(i + 1, n):
			if l[i] != l[j]:
				continue
			wyn += pary[i][j]
	print(wyn)


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(int(input())):
	n = I()
	A = Neo()
	(Ans, cnt) = (0, [0] * (n + 1))
	for (i, a) in enumerate(A):
		cur = 0
		for a2 in A[i + 1:]:
			if a2 == a:
				Ans += cur
			cur += cnt[a2]
		cnt[a] += 1
	print(Ans)


import sys
input = sys.stdin.readline

def solution():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	cnt_L = [0] * (n + 1)
	for j in range(1, n - 2):
		cnt_L[a[j - 1]] += 1
		current_sum = 0
		cnt_R = [0] * (n + 1)
		for l in range(j + 2, n):
			current_sum -= cnt_L[a[l - 1]] * cnt_R[a[l - 1]]
			cnt_R[a[l - 1]] += 1
			current_sum += cnt_L[a[l - 1]] * cnt_R[a[l - 1]]
			if a[j] == a[l]:
				ans += current_sum
	print(ans)
T = int(input())
for _ in range(T):
	solution()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	dic = {}
	for i in range(1, n + 1):
		dic[i] = 0
	ans = 0
	for i in range(n):
		dic1 = {}
		for j in range(n - 1, i, -1):
			ans += dic[ar[j]] * dic1.get(ar[i], 0)
			dic1[ar[j]] = dic1.get(ar[j], 0) + 1
		dic[ar[i]] += 1
	print(ans)


res = ''
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = {}
	r = 0
	for (i, row) in enumerate(a):
		m = 0
		for row2 in a[i + 1:]:
			if row == row2:
				r += m
			m += c.get(row2, 0)
		c[row] = c.get(row, 0) + 1
	res += f'{r}\n'
print(res)


def getcnt(x, l, r):
	if r < l:
		return 0
	if l == 0:
		return p[x][r]
	else:
		return p[x][r] - p[x][l - 1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = [[0 for _a in range(n)] for _b in range(n + 1)]
	for x in range(1, n + 1):
		cnt = 0
		for i in range(n):
			if a[i] == x:
				cnt += 1
			p[x][i] = cnt
	ans = 0
	for i in range(n - 1):
		y = a[i]
		for j in range(i + 1, n):
			x = a[j]
			ans += getcnt(x, 0, i - 1) * getcnt(y, j + 1, n - 1)
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dic_l = defaultdict(lambda : 0)
	ans = 0
	for i in range(n):
		j = n - 1
		dic_r = defaultdict(lambda : 0)
		while j > i:
			ans += dic_l[l[j]] * dic_r[l[i]]
			dic_r[l[j]] += 1
			j -= 1
		dic_l[l[i]] += 1
	print(ans)


import sys
from math import sqrt
from collections import defaultdict
inpt = sys.stdin.readline

def inp():
	return int(inpt())

def inlist():
	return list(map(int, inpt().split()))

def instr():
	s = inpt()
	return list(s[:len(s) - 1])

def invr():
	return map(int, inpt().split())

class Graph:

	def __init__(self, n):
		self.n = n
		self.graph = defaultdict(list)
		self.marked = defaultdict(int)

	def addEdge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def dfs(self, u):
		self.marked[u] = 1
		for i in self.graph[u]:
			if self.marked[i] == 0:
				self.dfs(i)

def main():
	t = inp()
	for l in range(t):
		n = inp()
		a = inlist()
		freq = defaultdict(int)
		ans = 0
		for k in range(n - 1, -1, -1):
			for l in range(k + 1, n, 1):
				freq[a[k] * n + a[l]] += 1
			j = k - 1
			for i in range(j):
				ans += freq[a[i] * n + a[j]]
		print(ans)
main()


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	l = [[] for i in range(n + 2)]
	r = [0] * (n + 3)
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] == a[j]:
				l[j].append(i)
	ans = 0
	for i in range(n):
		c = 0
		for j in range(i + 1, n):
			if a[i] == a[j]:
				ans += c
			c += bisect_left(l[j], i)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = [0 for _ in range(n + 1)]
	e = [0 for _ in range(n + 1)]
	c = 0
	for j in range(0, n - 2):
		e = [0 for _ in range(n + 1)]
		for k in range(n - 1, j, -1):
			c += s[a[k]] * e[a[j]]
			e[a[k]] += 1
		s[a[j]] += 1
	print(c)


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	ls = get_list()
	l = [0] * (n + 1)
	c = 0
	for j in range(n):
		r = [0] * (n + 1)
		for k in range(n - 1, j, -1):
			c += l[ls[k]] * r[ls[j]]
			r[ls[k]] += 1
		l[ls[j]] += 1
	print(c)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def check(s, n, v):
	for i in range(n):
		if s[i:i + n].count(v) == 0:
			return False
	return True

def solve():
	n = mint()
	a = list(mints())
	dp = [[0] * (n + 1) for i in range(n)]
	for i in range(n):
		d = dp[i]
		for j in range(n):
			d[j + 1] = d[j] + (a[j] == a[i])
	r = 0
	for i in range(1, n):
		for j in range(i + 1, n):
			r += dp[j][i] * (dp[i][n] - dp[i][j + 1])
	print(r)
for i in range(mint()):
	solve()


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pre = defaultdict(int)
	ans = 0
	for j in range(n - 1):
		suf = defaultdict(int)
		for k in range(n - 1, j, -1):
			ans += suf[a[j]] * pre[a[k]]
			suf[a[k]] += 1
		pre[a[j]] += 1
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(item) - 1 for item in input().split()]
	cnt = [[0] * n for _ in range(n + 1)]
	for (i, item) in enumerate(a):
		for val in range(n):
			cnt[i + 1][val] = cnt[i][val]
		cnt[i + 1][item] += 1
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			left = cnt[i][a[j]]
			right = cnt[n][a[i]] - cnt[j + 1][a[i]]
			ans += left * right
	print(ans)


from collections import defaultdict
import sys
input = sys.stdin.readline
T = int(input())

def binom(n, k):
	C = [0 for i in range(k + 1)]
	C[0] = 1
	for i in range(1, n + 1):
		j = min(i, k)
		while j > 0:
			C[j] = C[j] + C[j - 1]
			j -= 1
	return C[k]

def add(l1, l2):
	answer = 0
	gps = []
	pos1 = []
	pos2 = []
	il1 = 0
	il2 = 0
	current_type = None
	cnt = 0
	while il1 < len(l1) and il2 < len(l2):
		m = min(l1[il1], l2[il2])
		if m == l1[il1]:
			if current_type == 2:
				gps.append((2, cnt))
				cnt = 0
			current_type = 1
			cnt += 1
			il1 += 1
		if m == l2[il2]:
			if current_type == 1:
				gps.append((1, cnt))
				cnt = 0
			current_type = 2
			cnt += 1
			il2 += 1
	if il1 < len(l1):
		gps.append((2, cnt))
		gps.append((1, len(l1) - il1))
	if il2 < len(l2):
		gps.append((1, cnt))
		gps.append((2, len(l2) - il2))
	for i in range(len(gps)):
		if gps[i][0] == 1:
			pos1.append(i)
		else:
			pos2.append(i)
	for ip1 in range(len(pos1) - 1):
		p1 = pos1[ip1]
		tot2 = 0
		for gpi in range(p1 + 1, len(gps)):
			if gps[gpi][0] == 2:
				tot2 += gps[gpi][1]
		cur2 = 0
		for gpi in range(p1 + 1, len(gps)):
			if gps[gpi][0] == 2:
				cur2 += gps[gpi][1]
			else:
				answer += gps[p1][1] * gps[gpi][1] * cur2 * (tot2 - cur2)
	for ip2 in range(len(pos2) - 1):
		p2 = pos2[ip2]
		tot1 = 0
		for gpi in range(p2 + 1, len(gps)):
			if gps[gpi][0] == 1:
				tot1 += gps[gpi][1]
		cur1 = 0
		for gpi in range(p2 + 1, len(gps)):
			if gps[gpi][0] == 1:
				cur1 += gps[gpi][1]
			else:
				answer += gps[p2][1] * gps[gpi][1] * cur1 * (tot1 - cur1)
	return answer
for t in range(T):
	N = int(input())
	A = [int(_) for _ in input().split()]
	pos = defaultdict(list)
	for (i, el) in enumerate(A):
		pos[el].append(i)
	for el in pos.keys():
		pos[el] = sorted(pos[el])
	answer = 0
	for el1 in pos.keys():
		for el2 in pos.keys():
			if el1 == el2:
				el = el1
				answer += binom(len(pos[el]), 4)
			elif el1 < el2:
				if len(pos[el1]) < 2 or len(pos[el2]) < 2:
					continue
				answer += add(pos[el1], pos[el2])
	print(answer)


import collections

def solve(A, n):
	res = 0
	left = [0] * 5000
	right = [0] * 5000
	for j in range(n):
		for i in range(5000):
			right[i] = 0
		for k in range(n - 1, j, -1):
			res += left[A[k]] * right[A[j]]
			right[A[k]] += 1
		left[A[j]] += 1
	return res
res = []
t = int(input())
while t:
	n = int(input())
	arr = input().split(' ')
	A = [int(n) for n in arr]
	res.append(solve(A, n))
	t -= 1
for r in res:
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	cntL = [0] * (n + 1)
	cntR = [0] * (n + 1)
	for j in range(1, n - 2):
		cntR = [0] * (n + 1)
		cntL[arr[j - 1]] += 1
		for k in range(n - 2, j, -1):
			cntR[arr[k + 1]] += 1
			ans += cntL[arr[k]] * cntR[arr[j]]
	print(ans)


gans = []
for _ in range(int(input())):
	n = int(input())
	s = list(map(lambda x: int(x) - 1, input().split()))
	u = []
	cnt = []
	k = 1
	for i in range(1, n):
		if s[i] == s[i - 1]:
			k += 1
		else:
			u.append(s[i - 1])
			cnt.append(k)
			k = 1
	u.append(s[-1])
	m = len(u)
	cnt.append(k)
	dp = []
	ans = 0
	for cl in range(n):
		dp.append([0] * (m + 1))
		for i in range(m):
			dp[cl][i] = dp[cl][i - 1]
			if u[i] == cl:
				dp[cl][i] += cnt[i]
		cur = dp[cl][m - 1]
		if cur >= 4:
			ans += cur * (cur - 1) * (cur - 2) * (cur - 3) // (2 * 3 * 4)
	d = dict()
	for i in range(m - 2):
		ansi = 0
		d = dict()
		for k in range(i + 1, m - 1):
			if u[i] == u[k]:
				ans += ansi * cnt[i] * cnt[k]
				continue
			if u[k] in d.keys():
				cur = d[u[k]][:]
				ansi -= cur[0] * cur[1]
				cur[0] += cnt[k]
				cur[1] -= cnt[k]
				ansi += cur[0] * cur[1]
				d[u[k]][0] += cnt[k]
				d[u[k]][1] -= cnt[k]
			else:
				d[u[k]] = [cnt[k], dp[u[k]][m - 1] - dp[u[k]][k]]
				ansi += (dp[u[k]][m - 1] - dp[u[k]][k]) * cnt[k]
	gans.append(ans)
print('\n'.join(map(str, gans)))


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def printd(*args, **kwargs):
	pass

def get_str():
	return input().decode().strip()

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

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	a = list(rint())
	aset = list(set(a))
	nn = len(aset)
	si = [0] * 3001
	for i in range(nn):
		si[aset[i]] = i
	ps = [[] for i in range(nn)]
	printd(a)
	for i in range(nn):
		ps[i] = [1 if a[0] == aset[i] and j == 0 else 0 for j in range(n)]
	for i in range(1, n):
		for j in range(nn):
			if aset[j] == a[i]:
				ps[j][i] = ps[j][i - 1] + 1
			else:
				ps[j][i] = ps[j][i - 1]
	ans = 0
	printd('ps', ps)
	for i in range(n):
		fb = [[] for _ in range(nn)]
		if i + 2 >= n:
			continue
		for j in range(nn):
			fb[j] = [0, ps[j][-1] - ps[j][i + 1]]
		ans1 = 0
		for j in range(i + 2, n):
			printd('before', fb)
			f = a[j - 1]
			b = a[j]
			fi = si[f]
			bi = si[b]
			ans1 -= fb[fi][0] * fb[fi][1]
			if fi != bi:
				ans1 -= fb[bi][0] * fb[bi][1]
			fb[fi][0] += 1
			fb[bi][1] -= 1
			ans1 += fb[fi][0] * fb[fi][1]
			if f != b:
				ans1 += fb[bi][0] * fb[bi][1]
			printd('after', fb)
			if a[i] == a[j]:
				printd('i, j, adding', i, j, ans1)
				ans += ans1
	print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

def solve(arr, n):
	cntLeft = [0 for i in range(n + 1)]
	cntRight = [0 for i in range(n + 1)]
	res = 0
	for j in range(n):
		cntRight = [0 for i in range(n + 1)]
		for k in range(n - 1, j, -1):
			res += cntLeft[arr[k]] * cntRight[arr[j]]
			cntRight[arr[k]] += 1
		cntLeft[arr[j]] += 1
	return res
t = inp()
while t > 0:
	n = inp()
	arr = inlt()
	print(solve(arr, n))
	t -= 1


def compress(arr):
	arr2 = sorted(arr)
	order = dict()
	length = len(arr)
	for i in range(length):
		order[arr2[i]] = i
	arr3 = [0] * length
	for i in range(length):
		arr3[i] = order[arr[i]]
	return arr3
t = int(input())
for case in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = compress(arr)
	right_pairs = [0] * ((n + 1) * (n + 1))
	result = 0
	for i in range(n - 1, -1, -1):
		for j in range(i + 2, n):
			right_pairs[arr[i + 1] * n + arr[j]] += 1
		for j in range(0, i):
			result += right_pairs[arr[j] * n + arr[i]]
	print(result)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split())
	d = defaultdict(list)
	for (ind, el) in enumerate(a):
		d[el].append(ind)
	for (k, v) in list(d.items()):
		if len(v) == 1:
			del d[k]
	ans = 0
	d = list(d.values())
	for i in range(len(d)):
		for j in range(i, len(d)):
			if i == j:
				if len(d[i]) < 4:
					continue
				cc = len(d[i])
				ans += int(cc * (cc - 1) * (cc - 2) * (cc - 3) / 24)
				continue
			ld = d[i] if min(d[i]) < min(d[j]) else d[j]
			rd = d[i] if min(d[i]) > min(d[j]) else d[j]
			cnt = [0] * (len(rd) + 1)
			for (rind, r) in enumerate(rd):
				lind = 0
				while lind < len(ld) and ld[lind] < r:
					cnt[rind] += 1
					lind += 1
			cnt[-1] = len(ld)
			for ic in range(len(rd)):
				for jc in range(ic + 1, len(rd)):
					lcount = cnt[ic]
					midcount = cnt[jc] - cnt[ic]
					rcount = cnt[-1] - cnt[jc]
					ans += lcount * midcount + rcount * midcount
	print(ans)


from sys import stdin, stdout

def zigzags(n, a_a):
	ans = 0
	freq = [0 for _ in range((n + 1) * (n + 1))]
	for j in range(n - 3, 0, -1):
		k = j + 1
		for l in range(k + 1, n):
			freq[a_a[k] * n + a_a[l]] += 1
		for i in range(j):
			ans += freq[a_a[i] * n + a_a[j]]
	return ans
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a_a = list(map(int, stdin.readline().split()))
	ans = zigzags(n, a_a)
	stdout.write(str(ans) + '\n')


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = 0
	for i in range(n):
		(f, s) = ([0] * (n + 1), [0] * (n + 1))
		pa = 0
		for j in range(i + 2, n):
			s[a[j]] += 1
		for j in range(i + 2, n):
			pa -= s[a[j]] * f[a[j]]
			if a[j] != a[j - 1]:
				pa -= f[a[j - 1]] * s[a[j - 1]]
			f[a[j - 1]] += 1
			s[a[j]] -= 1
			pa += f[a[j]] * s[a[j]]
			if a[j] != a[j - 1]:
				pa += f[a[j - 1]] * s[a[j - 1]]
			if a[i] == a[j]:
				ans += pa
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	ptn = [[0] * n for _ in range(n)]
	cnt = [0] * n
	for (i, r_val) in enumerate(a):
		r_val -= 1
		for l_val in range(n):
			ptn[l_val][r_val] += cnt[l_val]
		cnt[r_val] += 1
		if i + 1 < n:
			l_val = a[i + 1] - 1
			for j in range(i + 2, n):
				r_val = a[j] - 1
				ans += ptn[l_val][r_val]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cntLeft = [0] * (n + 1)
	ans = 0
	for j in range(n):
		cntRight = [0] * (n + 1)
		for k in range(n - 1, j, -1):
			ans += cntRight[a[j]] * cntLeft[a[k]]
			cntRight[a[k]] += 1
		cntLeft[a[j]] += 1
	print(ans)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
for __ in range(itg()):
	n = itg()
	arr = tuple(mpint())
	ans = 0
	cnt1 = [0] * 3007
	for i in range(n):
		cnt2 = [0] * 3007
		count = 0
		for j in range(i + 1, n):
			if arr[i] == arr[j]:
				ans += count
			count += cnt1[arr[j]]
			cnt2[arr[j]] += 1
		cnt1[arr[i]] += 1
	print(ans)


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = list(map(lambda x: x - 1, a))
	ans = 0
	for i in range(n):
		cntl = [0] * n
		cntr = [0] * n
		now = 0
		for j in range(i + 2, n):
			cntr[a[j]] += 1
		for j in range(i + 1, n - 1):
			if a[i] == a[j]:
				ans += now
			if a[j] == a[j + 1]:
				now -= cntl[a[j]] * cntr[a[j + 1]]
				cntl[a[j]] += 1
				cntr[a[j + 1]] -= 1
				now += cntl[a[j]] * cntr[a[j + 1]]
			else:
				now += cntr[a[j]]
				now -= cntl[a[j + 1]]
				cntl[a[j]] += 1
				cntr[a[j + 1]] -= 1
	print(ans)


from collections import defaultdict

def countTuples(arr, N):
	ans = 0
	val = 0
	freq = defaultdict(lambda : 0)
	for j in range(N - 2):
		val = 0
		for l in range(j + 1, N):
			if arr[j] == arr[l]:
				ans += val
			val += freq[arr[l]]
		freq[arr[j]] += 1
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(countTuples(a, n))


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
from itertools import accumulate as acm, compress as cp
for __ in range(itg()):
	n = itg()
	arr = tuple(mpint())
	ans = 0
	count = [0] * 3001
	for i in range(n):
		ans += sum(cp(acm(map(count.__getitem__, arr[i + 1:])), map(lambda x: arr[i] == x, arr[i + 2:])))
		count[arr[i]] += 1
	print(ans)


from sys import stdin
input = stdin.readline

def nc2(n):
	return n * (n - 1) // 2

def nc4(n):
	return n * (n - 1) * (n - 2) * (n - 3) // 24

def answer():
	count = [[0 for i in range(n + 1)] for i in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(n + 1):
			count[i][j] = count[i - 1][j]
		count[i][a[i - 1]] += 1
	ans = 0
	for j in range(1, n + 1):
		for k in range(j + 1, n + 1):
			x1 = count[j - 1][a[k - 1]]
			x2 = count[n][a[j - 1]] - count[k][a[j - 1]]
			ans += x1 * x2
	return ans
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(answer())


import sys, math, cmath, time
start_time = time.time()

def solve():
	n = inp()
	a = inlt()
	qq = max(a)
	ww = 0
	qw = 0
	for i in range(len(a) - 3):
		q = [0] * qq
		w = counter(a[i + 2:], qq)
		for j in range(i + 2, len(a)):
			q[a[j - 1] - 1] = q[a[j - 1] - 1] + 1
			ww = ww + w[a[j - 1] - 1]
			w[a[j] - 1] = w[a[j] - 1] - 1
			ww = ww - q[a[j] - 1]
			if a[i] == a[j]:
				qw = qw + ww
	print(qw)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

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

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

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

def counter(a, qq):
	q = [0] * qq
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pre = [[0] * (max(a) + 1) for i in range(n + 1)]
	for i in range(n):
		pre[i + 1][a[i]] += 1
	for i in range(n):
		for j in range(max(a) + 1):
			pre[i + 1][j] += pre[i][j]
	ans = 0
	for j in range(2, n - 1):
		for k in range(j + 1, n):
			ans += pre[j - 1][a[k - 1]] * (pre[n][a[j - 1]] - pre[k][a[j - 1]])
	print(ans)


def fun():
	n = int(input())
	a = list(map(int, input().split(' ')))
	prefix = [[0 for i in range(n + 1)] for j in range(n + 1)]
	ans22 = 0
	for i in range(n):
		for j in range(1, n + 1):
			prefix[i + 1][j] += prefix[i][j]
		prefix[i + 1][a[i]] += 1
	for i in range(n):
		has = [0 for i in range(n + 1)]
		ans = 0
		for j in range(i + 1, n):
			if a[j] == a[i]:
				ans22 += ans
			if has[a[j]] > 0 and a[j] == a[i] and (prefix[n][a[j]] - prefix[j][a[j]] > 0):
				ans22 -= has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]])
				ans22 += has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]] - 1)
			if has[a[j]] == 0:
				ans += prefix[n][a[j]] - prefix[j + 1][a[j]]
			elif prefix[n][a[j]] - prefix[j][a[j]] > 0:
				ans -= has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]])
				ans += (has[a[j]] + 1) * (prefix[n][a[j]] - prefix[j][a[j]] - 1)
			has[a[j]] += 1
	return ans22

def solve():
	t = int(input())
	for i in range(t):
		print(fun())
solve()


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
from collections import defaultdict

def same_pair(iterable):
	iterable = tuple(iterable)
	size = len(iterable)
	default_dict = defaultdict(lambda : 0)
	result = 0
	for index1 in range(size):
		count = 0
		for index2 in range(index1 + 1, size):
			if iterable[index1] == iterable[index2]:
				result += count
			count += default_dict[iterable[index2]]
		default_dict[iterable[index1]] += 1
	return result
for __ in range(itg()):
	itg()
	print(same_pair(mpint()))


for _ in range(int(input())):
	n = int(input())
	l = list(map(lambda x: int(x) - 1, input().split()))
	c = [0] * n
	dp = []
	for i in l:
		c[i] += 1
		dp.append(c.copy())
	ans = 0
	for i in range(1, n - 2):
		for j in range(i + 1, n - 1):
			bcwd = dp[-1][l[i]] - dp[j][l[i]]
			ans += dp[i - 1][l[j]] * bcwd
	print(ans)


from sys import stdin
input = stdin.readline

def A():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		print(s[n - 1] * n)
from collections import deque

def D():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		dfind = [[0 for i in range(n)] for j in range(n)]
		afind = [[0 for i in range(n)] for j in range(n)]
		for secondInd in range(1, n - 2):
			for thirdInd in range(n - 2, secondInd, -1):
				bonus = int(a[thirdInd + 1] == a[secondInd])
				dfind[secondInd][thirdInd] = dfind[secondInd][thirdInd + 1] + bonus
		for thirdInd in range(2, n - 1):
			for secondInd in range(1, thirdInd):
				bonus = int(a[secondInd - 1] == a[thirdInd])
				afind[secondInd][thirdInd] = afind[secondInd - 1][thirdInd] + bonus
		tot = 0
		for secondInd in range(1, n - 2):
			for thirdInd in range(secondInd, n - 1):
				tot += afind[secondInd][thirdInd] * dfind[secondInd][thirdInd]
		print(tot)
D()


import sys
import math
import collections
import bisect
import itertools
import decimal
INF = 10 ** 20
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline().rstrip())
ns = lambda : map(int, sys.stdin.readline().rstrip().split())
na = lambda : list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda : list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))

def main():
	t = ni()
	for _ in range(t):
		n = ni()
		a = na()
		ans = 0
		cum_l = [0 for _ in range(n + 1)]
		for i in range(n):
			cum_r = [0 for _ in range(n + 1)]
			for j in range(n - 1, i, -1):
				if i - 1 >= 0 and j + 1 < n:
					ans += cum_l[a[j] - 1] * cum_r[a[i] - 1]
				cum_r[a[j] - 1] += 1
			cum_l[a[i] - 1] += 1
		print(ans)
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	answer = 0
	count = [0] * (n + 1)
	for i in range(n - 1):
		cnt = [0] * (n + 1)
		for j in range(n - 1, i, -1):
			answer += cnt[arr[i]] * count[arr[j]]
			cnt[arr[j]] += 1
		count[arr[i]] += 1
	out(answer)


t = int(input())
from collections import defaultdict
from bisect import bisect
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [[0 for j in range(n + 1)] for k in range(n + 1)]
	l2 = [[0 for j in range(n + 1)] for k in range(n + 1)]
	l3 = [0] * (n + 1)
	ans = 0
	for j in range(n):
		for k in range(j + 1, n):
			l1[l[j]][l[k]] += 1
	for j in range(n - 1, -1, -1):
		for k in range(j - 1, -1, -1):
			l1[l[k]][l[j]] -= 1
		for k in range(j + 1, n):
			l2[l[j]][l[k]] += 1
		for k in range(1, n + 1):
			ans += l1[l[j]][k] * l2[l[j]][k]
			l2[l[j]][k] = 0
	print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d1 = dd(int)
	ans = 0
	c = 0
	for i in range(n - 1):
		d1[a[i]] += 1
		d2 = dd(int)
		for j in range(n - 1, i, -1):
			d2[a[j]] += 1
			if a[i] == a[j]:
				ans += (d1[a[j]] - 1) * (d2[a[i]] - 1)
			else:
				ans += d1[a[j]] * d2[a[i]]
			c += 1
	print(ans)


from sys import stdin
from collections import defaultdict
from copy import deepcopy
import heapq
import math
from functools import reduce

def get_ans(n, lst):
	pos = defaultdict(list)
	for i in range(3):
		pos[lst[i]].append(i)
	pairs_stat = defaultdict(int)
	if lst[0] == lst[2]:
		pairs_stat[1] += 1
	ans = 0
	for i in range(3, n):
		cur_num = lst[i]
		if len(pos[cur_num]):
			for pair in pos[cur_num]:
				ans += pairs_stat[pair]
			pnum = 1
			for g in range(pos[cur_num][0] + 1, i):
				pairs_stat[g] += pnum
				if lst[g] == cur_num:
					pnum += 1
		pos[cur_num].append(i)
	return ans
t = int(stdin.buffer.readline())
for _ in range(t):
	n = int(stdin.buffer.readline())
	lst = list(map(int, stdin.buffer.readline().split()))
	print(get_ans(n, lst))


from itertools import count
import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def output(*args):
	sys.stdout.buffer.write(('\n'.join(map(str, args)) + '\n').encode('utf-8'))

def main():
	from collections import Counter
	t = int(input())
	ans_a = [''] * t
	for ti in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		cnt = Counter(a)
		ans = sum((v * (v - 1) * (v - 2) * (v - 3) // 24 for v in cnt.values() if v >= 4))
		for i in range(n - 1):
			left = [0] * (n + 1)
			pair = 0
			for j in range(i + 1, n):
				if a[j] != a[i]:
					pair -= left[a[j]] * (cnt[a[j]] - left[a[j]])
					left[a[j]] += 1
					pair += left[a[j]] * (cnt[a[j]] - left[a[j]])
				else:
					ans += pair
			cnt[a[i]] -= 1
		ans_a[ti] = str(ans)
	output(*ans_a)
main()


import sys
from collections import Counter
input = sys.stdin.readline

def calc(a):
	return a * (a - 1) * (a - 2) * (a - 3) // 24
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	left = [0] * (n + 1)
	complete = [0] * (n + 1)
	for i in a:
		complete[i] += 1
	ans = 0
	for i in range(n):
		b = left[:]
		c = complete[:]
		t = 0
		for j in range(i + 1, n):
			if a[j] != a[i]:
				c[a[j]] -= 1
				b[a[j]] += 1
				if b[a[j]] != 0 and c[a[j]] != 0:
					t -= (b[a[j]] - 1) * (c[a[j]] + 1)
					t += b[a[j]] * c[a[j]]
				elif c[a[j]] == 0:
					t -= b[a[j]] - 1
			else:
				ans += t
		complete[a[i]] -= 1
	c = Counter(a)
	for i in c:
		if c[i] >= 4:
			ans += calc(c[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	cnt1 = [0] * (n + 1)
	for j in range(1, n - 1):
		cnt1[a[j - 1]] += 1
		cnt2 = [0] * (n + 1)
		for k in range(n - 2, j, -1):
			cnt2[a[k + 1]] += 1
			res += cnt1[a[k]] * cnt2[a[j]]
	print(res)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	DP1 = [0] * (n + 1)
	DP2 = [[0] * (n + 1) for i in range(n + 1)]
	DP3 = [[0] * (n + 1) for i in range(n + 1)]
	DP4 = [[0] * (n + 1) for i in range(n + 1)]
	for a in A:
		for i in range(1, n + 1):
			DP4[i][a] += DP3[i][a]
		for i in range(1, n + 1):
			DP3[a][i] += DP2[a][i]
		for i in range(1, n + 1):
			DP2[i][a] += DP1[i]
		DP1[a] += 1
	ANS = 0
	for i in range(n + 1):
		ANS += sum(DP4[i])
	print(ANS)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	count = [0] * n
	ans = 0
	for (i, a) in enumerate(l):
		cur = 0
		for j in l[i + 1:]:
			if j == a:
				ans += cur
			cur += count[j - 1]
		count[a - 1] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	count = 0
	left = dict()
	for i in range(n):
		right = dict()
		for j in range(n - 1, i, -1):
			try:
				count += left[arr[j]] * right[arr[i]]
			except:
				pass
			try:
				right[arr[j]] += 1
			except:
				right[arr[j]] = 1
		try:
			left[arr[i]] += 1
		except:
			left[arr[i]] = 1
	print(count)


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	aa = rl()
	aa = [x - 1 for x in aa]
	ans = 0
	for i in range(n):
		cnt_left = [0] * n
		cnt_right = [0] * n
		combination_left_and_right = 0
		for l in range(i + 2, n):
			cnt_right[aa[l]] += 1
		for k in range(i + 2, n - 1):
			if aa[k] == aa[k - 1]:
				a = aa[k]
				combination_left_and_right -= cnt_left[a] * cnt_right[a]
				cnt_left[a] += 1
				cnt_right[a] -= 1
				combination_left_and_right += cnt_left[a] * cnt_right[a]
			else:
				combination_left_and_right += cnt_right[aa[k - 1]]
				combination_left_and_right -= cnt_left[aa[k]]
				cnt_right[aa[k]] -= 1
				cnt_left[aa[k - 1]] += 1
			if aa[k] == aa[i]:
				ans += combination_left_and_right
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		prefix_freq = dict()
		for (i, num) in enumerate(nums):
			if num in prefix_freq:
				prefix_freq[num][i] = 1
			else:
				prefix_freq[num] = [0] * n
				prefix_freq[num][i] = 1
		for key in prefix_freq.keys():
			val = prefix_freq[key]
			for i in range(1, n):
				val[i] += val[i - 1]
		ans = 0
		for j in range(1, n):
			for k in range(j + 1, n):
				(aj, ak) = (nums[j], nums[k])
				ans += prefix_freq[ak][j - 1] * (prefix_freq[aj][-1] - prefix_freq[aj][k])
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	accu = [[0] * (n + 1) for _ in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			accu[i][j] = accu[i][j - 1] + (a[j - 1] == i)
	ans = 0
	for j in range(2, n - 1):
		for k in range(j + 1, n):
			ans += accu[a[k - 1]][j - 1] * (accu[a[j - 1]][-1] - accu[a[j - 1]][k])
	print(ans)


def Zigzag(Numbers, n):
	Ocurrences = dict()
	Ocurrences[Numbers[0]] = 1
	ZigzagsCounter = 0
	for j in range(1, n - 2):
		InternalCounter = 0
		for l in range(j + 1, n):
			if Numbers[j] == Numbers[l]:
				ZigzagsCounter += InternalCounter
			InternalCounter += Ocurrences.get(Numbers[l], 0)
		Ocurrences[Numbers[j]] = Ocurrences.get(Numbers[j], 0) + 1
	return ZigzagsCounter
NTesters = int(input())
while NTesters > 0:
	n = int(input())
	Numbers = list(map(int, input().split()))
	print(Zigzag(Numbers, n))
	NTesters -= 1


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	aa = LI1()
	cnt = [0] * n
	ans = 0
	for (i, a) in enumerate(aa):
		cur = 0
		for a2 in aa[i + 1:]:
			if a2 == a:
				ans += cur
			cur += cnt[a2]
		cnt[a] += 1
	print(ans)


t = int(input())
a = []
id = []
for _ in range(t):
	n = int(input())
	A = map(int, input().split())
	a = []
	for x in A:
		a.append(x)
	cnt = [[0 for i in range(n + 1)] for j in range(n + 1)]
	ans = 0
	for j in range(n):
		for i in range(j):
			cnt[a[i]][a[j]] += 1
		k = j + 1
		for l in range(k + 1, n):
			ans += cnt[a[k]][a[l]]
	print(ans)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	lcount = [0] * (n + 100)
	ans = 0
	for l in range(n):
		rcount = [0] * (n + 100)
		for r in range(n - 1, l, -1):
			ans += lcount[arr[r]] * rcount[arr[l]]
			rcount[arr[r]] += 1
		lcount[arr[l]] += 1
	print(ans)


def solve():
	n = int(input())
	v = [0] * (n + 2)
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		cur = 0
		for j in range(i + 1, n):
			if a[i] == a[j]:
				ans += cur
			cur += v[a[j]]
		v[a[i]] += 1
	print(ans)
T = int(input())
while T > 0:
	T -= 1
	solve()


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	left = [0] * (n + 1)
	right = [0] * (n + 1)
	ans = 0
	for j in range(n):
		for i in range(n + 1):
			right[i] = 0
		for k in range(n - 1, j, -1):
			ans += left[arr[k]] * right[arr[j]]
			right[arr[k]] += 1
		left[arr[j]] += 1
	print(ans)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	l = [0] * 3007
	for i in range(n):
		r = [0] * 3007
		for j in range(n - 1, i, -1):
			ans = ans + l[a[j]] * r[a[i]]
			r[a[j]] = r[a[j]] + 1
		l[a[i]] = l[a[i]] + 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [[0 for i in range(n + 1)] for j in range(n)]
	c = [[0 for i in range(n + 1)] for j in range(n)]
	for i in range(n - 1):
		b[i][a[i]] += 1
		b[i + 1] = b[i][:]
	i = n - 1
	while i > 0:
		c[i][a[i]] += 1
		c[i - 1] = c[i][:]
		i -= 1
	(i, u, v, add) = (1, 0, 0, 0)
	j = n - 2
	for i in range(1, n - 2):
		j = n - 2
		while j > i:
			u = c[j + 1][a[i]]
			v = b[i - 1][a[j]]
			add += u * v
			j -= 1
	print(add)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	values = sorted(list(set(a)))
	k = len(values)
	idx = {values[i]: i for i in range(k)}
	arr1 = [[0] * k for _ in range(k)]
	arr2 = [[0] * k for _ in range(k)]
	arr3 = [[0] * k for _ in range(k)]
	tot = 0
	for v in a:
		i = idx[v]
		for j in range(k):
			tot += arr3[j][i]
			arr3[i][j] += arr2[j][i]
			arr2[i][j] += arr1[j][i]
			arr1[i][j] += 1
	print(tot)


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict
import operator as op
from functools import reduce

@functools.lru_cache(maxsize=None)
def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom
for i in range(3000):
	ncr(i, 4)

def solve(a):
	n = len(a)
	l = [0] * (n + 1)
	ans = 0
	for j in range(n):
		r = [0] * (n + 1)
		for k in range(n - 1, j, -1):
			ans += l[a[k]] * r[a[j]]
			r[a[k]] += 1
		l[a[j]] += 1
	return ans

def console(*args):
	return
inp = sys.stdin.readlines()
for case_num in range(int(inp[0])):
	lst = list(map(int, inp[2 * case_num + 2].split()))
	res = solve(lst)
	print(res)


import sys
input = sys.stdin.buffer.readline
T = int(input())
for testcase in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	dg = 10000
	for i in range(n - 3):
		c = [0] * (n + 1)
		s = 0
		c[a[i + 1]] += 1
		for j in range(i + 3, n):
			c[a[j]] += dg
		s += c[a[i + 1]] // dg
		if a[i] == a[i + 2]:
			res += s
		for k in range(i + 2, n - 1):
			if k == i + 2:
				continue
			if a[k - 1] != a[k]:
				rr = c[a[k - 1]]
				(q, p) = divmod(rr, dg)
				s += q
				c[a[k - 1]] += 1
				rr = c[a[k]]
				(q, p) = divmod(rr, dg)
				s -= p
				c[a[k]] -= dg
			else:
				rr = c[a[k]]
				(q, p) = divmod(rr, dg)
				s -= p * q
				s += (p + 1) * (q - 1)
				c[a[k]] += 1 - dg
			if a[i] == a[k]:
				res += s
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	ans = out = 0
	for i in range(0, n - 1):
		cnt_tmp = [0] * (n + 1)
		ans = 0
		for j in range(i + 1, n):
			temp = ans
			ans -= cnt_tmp[s[j]] * cnt[s[j]]
			cnt_tmp[s[j]] += 1
			ans += cnt_tmp[s[j]] * cnt[s[j]]
			temp = ans - temp
			if s[i] == s[j]:
				out += ans - temp
		cnt[s[i]] += 1
	print(out)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	n = N()
	ar = lis()
	cntl = [0] * (n + 1)
	ans = 0
	for j in range(1, n - 2):
		cntl[ar[j - 1]] += 1
		cntr = [0] * (n + 1)
		for k in range(n - 2, j, -1):
			cntr[ar[k + 1]] += 1
			ans += cntl[ar[k]] * cntr[ar[j]]
	print(ans)
testcase(int(inp()))


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	N = int(input())
	A = alele()
	for i in range(N):
		A[i] -= 1
	count = [0] * N
	ans = 0
	for i in range(N):
		cur = 0
		for j in range(i + 1, N):
			if A[i] == A[j]:
				ans += cur
			cur += count[A[j]]
		count[A[i]] += 1
	print(ans)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
ans = 0

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		arr = inlt()
		ans = 0
		before = {}
		before[arr[0]] = 1
		for i in range(1, n - 1):
			total = 0
			for j in range(i + 1, n):
				if arr[i] == arr[j]:
					ans = ans + total
				if arr[j] in before:
					total = total + before[arr[j]]
			if arr[i] in before:
				before[arr[i]] += 1
			else:
				before[arr[i]] = 1
		print(ans)
main()


def process(A):
	d = {}
	n = len(A)
	for i in range(n):
		x = A[i]
		if x not in d:
			d[x] = []
		d[x].append(i)
	answer = 0
	for x in d:
		v = len(d[x])
		answer += v * (v - 1) * (v - 2) * (v - 3) // 24
	for x in d:
		for y in d:
			if x != y:
				pairs1 = {}
				pairs2 = {}
				for j in d[y]:
					for i in d[x]:
						if i > j:
							break
						if i not in pairs1:
							pairs1[i] = 0
						if j not in pairs2:
							pairs2[j] = 0
						pairs1[i] += 1
						pairs2[j] += 1
				for i in pairs1:
					for j in pairs2:
						if j < i:
							answer += pairs1[i] * pairs2[j]
	return answer
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	print(process(A))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	left = [0 for i in range(n + 1)]
	right = [0 for i in range(n + 1)]
	state = [0 for i in range(n)]
	k = n - 1
	while k >= 0:
		state[k] = right[:]
		right[arr[k]] += 1
		k -= 1
	ans = 0
	k = 0
	for j in range(0, n - 2):
		k = n - 1
		while k > j:
			ans += left[arr[k]] * state[k][arr[j]]
			k -= 1
		left[arr[j]] += 1
	print(ans)


import sys
input = sys.stdin.readline

def process():
	(N,) = map(int, input().split())
	X = list(map(int, input().split()))
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for i in range(1, N + 1):
		dp[i] = dp[i - 1][:]
		dp[i][X[i - 1]] = dp[i - 1][X[i - 1]] + 1
	dp2 = [[0] * (N + 1) for _ in range(N + 2)]
	for i in range(N, 0, -1):
		dp2[i] = dp2[i + 1][:]
		dp2[i][X[i - 1]] = dp2[i + 1][X[i - 1]] + 1
	R = 0
	for i in range(N):
		a = X[i]
		for j in range(i + 1, N):
			b = X[j]
			R += dp[i][b] * dp2[j + 2][a]
	print(R)
(T,) = map(int, input().split())
for _ in range(T):
	process()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	left = {}
	right = {}
	for i in range(n):
		right = {}
		for j in range(0, n):
			right[a[j]] = 0
		for j in range(n - 1, i, -1):
			if a[j] in left:
				ans += right[a[i]] * left[a[j]]
			right[a[j]] += 1
		if a[i] not in left:
			left[a[i]] = 1
		else:
			left[a[i]] += 1
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	j = 0
	k = n - 1
	Dict_j = defaultdict(int)
	Dict_k = defaultdict(int)
	count = 0
	for j in range(n):
		Dict_k.clear()
		for k in range(n - 1, j, -1):
			count += Dict_j[arr[k]] * Dict_k[arr[j]]
			Dict_k[arr[k]] += 1
		Dict_j[arr[j]] += 1
	print(count)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	counts = [0] * (n + 1)
	ans = 0
	for j in range(0, n):
		ik = 0
		for l in range(j + 1, n):
			if a[l] == a[j]:
				ans += ik
			ik += counts[a[l]]
		counts[a[j]] += 1
	print(ans)
	t -= 1


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	lst = list(map(int, input().split()))
	M = [[0] * (n + 1) for x in range(0, n + 1)]
	for i in range(0, n):
		for j in range(i + 1, n):
			M[lst[i]][lst[j]] += 1
	ans = 0
	for i in range(0, n):
		for j in range(i + 1, n):
			M[lst[i]][lst[j]] -= 1
		for j in range(0, i):
			ans += M[lst[j]][lst[i]]
	print(ans)


def make_freq_dict(n, a):
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
	return d

def make_occs_dict(n, a):
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = [i]
		else:
			d[a[i]].append(i)
	return d

def new_freqs(a):
	d = {}
	for i in a:
		d[i] = 0
	return d

def sames_choose_4(num):
	if num < 4:
		return 0
	numerator = num * (num - 1) * (num - 2) * (num - 3)
	denominator = 24
	return numerator // denominator

def solve(n, a):
	total = 0
	og_freqs = make_freq_dict(n, a)
	for k in og_freqs:
		total += sames_choose_4(og_freqs[k])
	for i in range(n - 3):
		freqs = new_freqs(a)
		ai = a[i]
		og_freqs[ai] -= 1
		add_this = 0
		subtotals = {}
		for j in range(i + 1, n):
			aj = a[j]
			if aj == ai:
				total += add_this
			else:
				freqs[aj] += 1
				if aj in subtotals:
					add_this -= subtotals[aj]
				subtotals[aj] = freqs[aj] * (og_freqs[aj] - freqs[aj])
				add_this += subtotals[aj]
	return total

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		print(solve(n, a))
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = list(map(lambda x: x - 1, a))
	acc = [[0] for _ in range(n)]
	for i in range(n):
		for j in range(n):
			acc[j].append(acc[j][-1] + (1 if a[i] == j else 0))
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			ans += acc[a[j]][i] * (acc[a[i]][n] - acc[a[i]][j + 1])
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, ('0 ' + input()).split()))
	c = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if j == a[i]:
				c[i][j] = c[i - 1][j] + 1
			else:
				c[i][j] = c[i - 1][j]
	res = 0
	for j in range(1, n + 1):
		for k in range(j + 1, n + 1):
			res += c[j - 1][a[k]] * (c[n][a[j]] - c[k][a[j]])
	print(res)


import math
from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	a = gil()
	occur = [None] * n
	prev = {}
	for i in range(n):
		temp = prev.copy()
		temp[a[i]] = temp.get(a[i], 0) + 1
		occur[i] = temp
		prev = temp
	cnt = 0
	for j in range(1, n):
		for k in range(j + 1, n - 1):
			i = occur[j - 1].get(a[k], 0)
			l = prev[a[j]] - occur[k][a[j]]
			cnt += i * l
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	left = [0] * 30005
	ans = 0
	for j in range(n):
		right = [0] * 3005
		for k in range(n - 1, j, -1):
			ans += right[arr[j]] * left[arr[k]]
			right[arr[k]] += 1
		left[arr[j]] += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	arr1 = [[0] * (n + 1) for _ in range(n + 1)]
	arr2 = [[0] * (n + 1) for _ in range(n + 1)]
	arr3 = [[0] * (n + 1) for _ in range(n + 1)]
	tot = 0
	for v in a:
		for w in range(n + 1):
			tot += arr3[w][v]
			arr3[v][w] += arr2[w][v]
			arr2[v][w] += arr1[w][v]
			arr1[v][w] += 1
	print(tot)


for i in range(int(input())):
	(n, a) = (int(input()), list(map(int, input().split())))
	c = [0] * (n + 2)
	ans = 0
	for i in range(n):
		matches = 0
		for j in range(i + 1, n):
			if a[i] == a[j]:
				ans += matches
			matches += c[a[j]]
		c[a[i]] += 1
	print(ans)


from bisect import bisect_left, bisect_right

class Result:

	def __init__(self, index, value):
		self.index = index
		self.value = value

class BinarySearch:

	def __init__(self):
		pass

	@staticmethod
	def greater_than(num: int, func, size: int=1):
		if isinstance(func, list):
			index = bisect_right(func, num)
			if index == len(func):
				return Result(None, None)
			else:
				return Result(index, func[index])
		else:
			(alpha, omega) = (0, size - 1)
			if func(omega) <= num:
				return Result(None, None)
			while alpha < omega:
				if func(alpha) > num:
					return Result(alpha, func(alpha))
				if omega == alpha + 1:
					return Result(omega, func(omega))
				mid = (alpha + omega) // 2
				if func(mid) > num:
					omega = mid
				else:
					alpha = mid

	@staticmethod
	def less_than(num: int, func, size: int=1):
		if isinstance(func, list):
			index = bisect_left(func, num) - 1
			if index == -1:
				return Result(None, None)
			else:
				return Result(index, func[index])
		else:
			(alpha, omega) = (0, size - 1)
			if func(alpha) >= num:
				return Result(None, None)
			while alpha < omega:
				if func(omega) < num:
					return Result(omega, func(omega))
				if omega == alpha + 1:
					return Result(alpha, func(alpha))
				mid = (alpha + omega) // 2
				if func(mid) < num:
					alpha = mid
				else:
					omega = mid
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from itertools import combinations
bs = BinarySearch()
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	b = [i for i in range(n)]
	di = {}
	for i in range(n):
		if a[i] not in di:
			di[a[i]] = []
		di[a[i]] += [i]
	ans = 0
	for (i, j) in combinations(b, 2):
		(p, q) = (di[a[i]], di[a[j]])
		(i1, i2) = (bs.less_than(i, q).index, bs.greater_than(j, p).index)
		if i1 is None or i2 is None:
			continue
		ans += (i1 + 1) * (len(p) - i2)
	print(ans)


from collections import defaultdict as dft
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	for i in range(2, n):
		freq = dft(int)
		for j in range(i + 1, n):
			freq[arr[j]] += 1
		ind = i - 1
		d = dft(int)
		temp = 0
		while ind >= 0:
			if arr[ind] == arr[i]:
				res += temp
			temp -= d[arr[ind]] * freq[arr[ind]]
			d[arr[ind]] += 1
			temp += d[arr[ind]] * freq[arr[ind]]
			ind -= 1
	print(res)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	left = [0] * 3002
	ans = 0
	for i in range(n):
		right = [0] * 3002
		for j in range(n - 1, i, -1):
			ans += right[l[i]] * left[l[j]]
			right[l[j]] += 1
		left[l[i]] += 1
	print(ans)
for _ in range(int(input())):
	solve()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	n = int(inp())
	a = lis()
	pre = [[0 for i in range(n + 1)] for i in range(n + 1)]
	freq = {}
	for i in range(n):
		if a[i] not in freq:
			freq[a[i]] = 1
		else:
			freq[a[i]] += 1
	for i in range(1, n + 1):
		for j in range(n):
			if a[j] == i:
				pre[i][j] = pre[i][j - 1] + 1
			else:
				pre[i][j] = pre[i][j - 1]
	ans = 0
	for j in range(1, n):
		for k in range(j + 1, n - 1):
			ans += pre[a[k]][j - 1] * (pre[a[j]][n - 1] - pre[a[j]][k])
	print(ans)
testcase(int(inp()))


for _ in range(int(input())):
	n = int(input())
	arr = [*map(int, input().split())]
	max_ele = max(arr) + 1
	counter = [[0] * max_ele for _ in range(n)]
	counter[0][arr[0]] += 1
	for i in range(1, n):
		for j in range(n):
			counter[i][arr[j]] = counter[i - 1][arr[j]]
		counter[i][arr[i]] += 1
	ans = 0
	for i in range(1, n - 1):
		for j in range(i + 1, n - 1):
			ans += counter[i - 1][arr[j]] * (counter[-1][arr[i]] - counter[j][arr[i]])
	print(ans)


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	pref = [[0 for j in range(n + 1)] for i in range(n)]
	ans = 0
	for i in range(n):
		pref[i][a[i]] += 1
	for i in range(1, n):
		for j in range(n + 1):
			pref[i][j] += pref[i - 1][j]
	for i in range(n):
		for j in range(i):
			if not j or i == n - 1:
				continue
			ans += pref[j - 1][a[i]] * (pref[n - 1][a[j]] - pref[i][a[j]])
	print(ans)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(v):
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return adj
mod = 10 ** 9 + 7

def solve():
	n = II()
	li = [0] + L()
	dp = [[0 for j in range(n + 1)] for i in range(n + 1)]
	ans = 0
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if li[j] == i:
				dp[i][j] = dp[i][j - 1] + 1
			else:
				dp[i][j] = dp[i][j - 1]
	for i in range(2, n + 1):
		for j in range(i + 1, n):
			(a1, a2) = (li[i], li[j])
			ans += dp[a2][i - 1] * (dp[a1][n] - dp[a1][j])
	print(ans)
	return
t = II()
for i in range(t):
	solve()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	total = 0
	a1 = [[0] * (n + 1) for _ in range(n + 1)]
	a2 = [[0] * (n + 1) for _ in range(n + 1)]
	a3 = [[0] * (n + 1) for _ in range(n + 1)]
	for i in a:
		for j in range(n + 1):
			total += a3[j][i]
			a3[i][j] += a2[j][i]
			a2[i][j] += a1[j][i]
			a1[i][j] += 1
	print(total)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = li()
	ans = 0
	for i in range(n):
		rightmap = defaultdict(int)
		currans = 0
		leftmap = defaultdict(int)
		for j in range(i, n):
			leftmap[l[j]] += 1
		for j in range(n - 1, i, -1):
			if l[i] == l[j]:
				ans += currans - rightmap[l[j]] * leftmap[l[j]]
			currans -= rightmap[l[j]] * leftmap[l[j]]
			rightmap[l[j]] += 1
			leftmap[l[j]] -= 1
			currans += rightmap[l[j]] * leftmap[l[j]]
	cnt = Counter(l)
	for i in cnt:
		curr = cnt[i]
		if curr > 3:
			ans += curr * (curr - 1) * (curr - 2) * (curr - 3) // 24
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	total = 0
	overall = [0] * (n + 1)
	for x in a:
		overall[x] += 1
	start = [0] * (n + 1)
	for i in range(n):
		start[a[i]] += 1
		mid = [0] * (n + 1)
		mid_count = 0
		for j in range(i + 1, n):
			if a[i] == a[j]:
				total += mid_count
			else:
				x = a[j]
				mid_count -= mid[x] * (overall[x] - mid[x] - start[x])
				mid[a[j]] += 1
				mid_count += mid[x] * (overall[x] - mid[x] - start[x])
	for (x, count) in enumerate(overall):
		total += count * (count - 1) * (count - 2) * (count - 3) // 24
	print(total)


a = int(input())
for i in range(a):
	s = int(input())
	z = list(map(int, input().split()))
	from collections import *
	al = defaultdict(list)
	for i in range(len(z)):
		al[z[i]].append(i)
	freq = [[0 for i in range(len(z) + 1)] for i in range(len(z))]
	reverse = [[0 for i in range(len(z) + 1)] for i in range(len(z))]
	for i in range(len(z)):
		for j in range(len(z) + 1):
			if i == 0 and j == z[i]:
				freq[i][j] += 1
			elif i == 0:
				continue
			else:
				freq[i][j] = freq[i - 1][j]
				if z[i] == j:
					freq[i][j] += 1
	for i in range(len(z) - 1, -1, -1):
		for j in range(len(z) + 1):
			if i == len(z) - 1 and j == z[i]:
				reverse[i][j] += 1
			elif i == len(z) - 1:
				continue
			else:
				reverse[i][j] = reverse[i + 1][j]
				if z[i] == j:
					reverse[i][j] += 1
	cnt = 0
	finalans = 0
	for i in al:
		t = al[i]
		total = [0 for i in range(len(z) + 1)]
		for j in range(len(t)):
			for x in range(len(z) + 1):
				if x == i:
					continue
				else:
					finalans += total[x] * reverse[t[j]][x]
					if j != len(t) - 1:
						total[x] += (j + 1) * (freq[t[j + 1]][x] - freq[t[j]][x])
		q = len(t)
		finalans += max(q * (q - 1) * (q - 2) * (q - 3) // 24, 0)
	print(finalans)


def calcCntAtPrefix(a):
	cntAtPrefix = [[0] * (len(a) + 1)]
	for (i, x) in enumerate(a):
		cntAtPrefix.append(cntAtPrefix[-1][:])
		cntAtPrefix[-1][x] += 1
	return cntAtPrefix

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	cntAtPrefix = calcCntAtPrefix(a)
	cntAtSuffix = calcCntAtPrefix(a[::-1])
	ans = 0
	for j in range(n):
		for k in range(j + 1, n):
			ans += cntAtPrefix[j][a[k]] * cntAtSuffix[n - 1 - k][a[j]]
	print(ans)
for t in range(int(input())):
	solve()


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	ct = [0] * (n + 10)
	ans = 0
	for i in range(0, len(s)):
		c = 0
		for j in range(i + 1, len(s)):
			if s[i] == s[j]:
				ans += c
			c += ct[s[j]]
		ct[s[i]] += 1
	print(ans)


from collections import Counter
from functools import lru_cache, reduce
import operator

def fact(n):
	return reduce(operator.mul, range(1, n + 1), 1)

@lru_cache(maxsize=None)
def pairs(n):
	return fact(n) // fact(n - 2) // 2
t = int(input())
x = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	h = Counter(a)
	res = 0
	for i in range(n):
		h[a[i]] -= 1
		h1 = Counter(h)
		h2 = Counter()
		s = 0
		for j in range(i + 1, n):
			s -= h1[a[j]] * h2[a[j]]
			h1[a[j]] -= 1
			if j > i + 1:
				if a[j] != a[j - 1]:
					s -= h1[a[j - 1]] * h2[a[j - 1]]
				h2[a[j - 1]] += 1
				if a[j] != a[j - 1]:
					s += h1[a[j - 1]] * h2[a[j - 1]]
			s += h1[a[j]] * h2[a[j]]
			if a[i] == a[j]:
				res += s
	x.append(res)
print(*x, sep='\n')


import sys
input = sys.stdin.readline

def f(n):
	return n * (n - 1) * (n - 2) * (n - 3) // 4 // 3 // 2
for kek in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	total = dict()
	left = dict()
	for i in a:
		if i in total:
			total[i] += 1
		else:
			total[i] = 1
		left[i] = 0
	for l in range(0, len(a) - 1):
		d = dict()
		t = 0
		for r in range(l + 1, len(a)):
			if a[l] == a[r]:
				ans += t
			if a[r] in d:
				t -= d[a[r]] * left[a[r]]
				d[a[r]] += 1
				t += d[a[r]] * left[a[r]]
			else:
				d[a[r]] = 1
				t += d[a[r]] * left[a[r]]
		left[a[l]] += 1
	print(ans)


import sys

def input():
	return sys.stdin.readline()[:-1]

def solve():
	N = int(input())
	A = list(map(int, input().split()))
	B = [None for i in range(N + 1)]
	b = [0] * 3001
	B[0] = list(b)
	for i in range(N):
		b[A[i]] += 1
		B[i + 1] = list(b)
	ans = 0
	for i in range(N - 2):
		C = [0] * 3001
		D = [0] * 3001
		v = 0
		for j in range(3001):
			C[j] = B[N][j] - B[i + 2][j]
		for j in range(i + 2, N):
			C[A[j]] -= 1
			D[A[j - 1]] += 1
			if A[j] == A[j - 1]:
				v += C[A[j]] * D[A[j]] - (C[A[j]] + 1) * (D[A[j]] - 1)
			else:
				v -= D[A[j]]
				v += C[A[j - 1]]
			if A[i] == A[j]:
				ans += v
	print(ans)

def main():
	T = int(input())
	for i in range(T):
		solve()
main()


import io
import os
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect_right

def nC4(n):
	return n * (n - 1) * (n - 2) * (n - 3) // (1 * 2 * 3 * 4)

def solve(N, A):
	valToIndices = defaultdict(list)
	for (i, x) in enumerate(A):
		valToIndices[x].append(i)
	ans = 0
	for (k, v) in valToIndices.items():
		ans += nC4(len(v))
	for j in range(N):
		for k in range(j + 1, N):
			aj = A[j]
			ak = A[k]
			if aj != ak:
				numI = bisect_left(valToIndices[ak], j)
				numL = len(valToIndices[aj]) - bisect_right(valToIndices[aj], k)
				ans += numI * numL
	return ans
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


def bin(place, a):
	l = -1
	r = len(place)
	while l < r - 1:
		m = (l + r) // 2
		if place[m] >= a:
			r = m
		else:
			l = m
	return l

def bin2(place, a):
	l = -1
	r = len(place)
	while l < r - 1:
		m = (l + r) // 2
		if place[m] > a:
			r = m
		else:
			l = m
	return r

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = list(map(int, input().split()))
		place = [[] for i in range(n)]
		answer = 0
		for i in range(n):
			values[i] -= 1
			place[values[i]].append(i)
		for j in range(1, n):
			for k in range(j + 1, n):
				count_i = bin(place[values[k]], j) + 1
				count_l = len(place[values[j]]) - bin2(place[values[j]], k)
				answer += max(0, count_i * count_l)
		print(answer)
main()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d2 = Counter(l)
	ans = 0
	l1 = [0] * (n + 2)
	for i in range(n - 3):
		d2[l[i]] -= 1
		l1 = [0] * (n + 2)
		tt = 0
		for j in range(i + 1, n - 2):
			if l1[l[j]] == 0:
				l1[l[j]] += 1
				if l[i] == l[j]:
					tt += l1[l[j]] * (d2[l[j]] - l1[l[j]] - 1)
				else:
					tt += l1[l[j]] * (d2[l[j]] - l1[l[j]])
				if l[i] == l[j + 1]:
					ans += tt
			else:
				l1[l[j]] += 1
				if l[i] == l[j]:
					tt = tt - (l1[l[j]] - 1) * (d2[l[j]] - (l1[l[j]] - 1) - 1) + l1[l[j]] * (d2[l[j]] - l1[l[j]] - 1)
				else:
					tt = tt - (l1[l[j]] - 1) * (d2[l[j]] - (l1[l[j]] - 1)) + l1[l[j]] * (d2[l[j]] - l1[l[j]])
				if l[i] == l[j + 1]:
					ans += tt
	print(ans)


import sys, bisect as bi
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	nt = [[] for i in range(n + 1)]
	pr = [[] for i in range(n + 1)]
	an = 0
	for i in range(n):
		for j in range(i + 1, n):
			if l[i] == l[j]:
				nt[i].append(j)
				pr[j].append(i)
	for i in range(1, n):
		for j in range(i + 1, n):
			if l[i] != l[j]:
				an += (len(nt[i]) - bi.bisect(nt[i], j)) * bi.bisect(pr[j], i)
			else:
				an += (len(nt[i]) - bi.bisect(nt[i], j)) * (bi.bisect(pr[j], i) - 1)
	print(an)


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
rlf = lambda : map(float, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))
MOD = 10 ** 9 + 7

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		A = list(rli())
		seen = [0 for _ in range(n * n + 10000)]
		ans = 0
		for j in range(n - 1, -1, -1):
			k = j + 1
			for l in range(k + 1, n):
				seen[A[k] + A[l] * n] += 1
			for i in range(0, j):
				ans += seen[A[i] + A[j] * n]
		print(ans)
	stdout.close()
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	freq = {}
	answer = 0
	for x in arr:
		if x not in freq:
			freq[x] = 0
		freq[x] += 1
	for i in range(n):
		freq[arr[i]] -= 1
		equal_pairs = 0
		so_far = {}
		for j in range(i + 1, n):
			freq[arr[j]] -= 1
			if arr[j] in so_far:
				equal_pairs -= so_far[arr[j]]
			if arr[i] == arr[j]:
				answer += equal_pairs
			if arr[j] not in so_far:
				so_far[arr[j]] = 1
			else:
				so_far[arr[j]] += 1
			equal_pairs += freq[arr[j]]
		for j in range(i + 1, n):
			freq[arr[j]] += 1
	print(answer)


from sys import stdin
input = lambda : stdin.readline().strip()
lin = lambda : list(map(int, input().split()))
iin = lambda : int(input())
main = lambda : map(int, input().split())
from math import ceil, sqrt, factorial, log
from collections import deque
from bisect import bisect_left
mod = 998244353
mod = 1000000007

def solve(we):
	n = iin()
	a = lin()
	cnt = [0] * (n + 10)
	ans = 0
	for l in range(n):
		cur = 0
		for r in range(l + 1, n):
			if a[l] == a[r]:
				ans += cur
			cur += cnt[a[r]]
		cnt[a[l]] += 1
	print(ans)
qwe = 1
qwe = iin()
for _ in range(qwe):
	solve(_ + 1)


import os, sys
from io import BytesIO, IOBase
from collections import defaultdict

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		x = list(set(a))
		dct = defaultdict(list)
		for (ind, val) in enumerate(a):
			dct[val].append(ind)
		ans = 0
		for i in dct:
			val = len(dct[i])
			ans += val * (val - 1) * (val - 2) * (val - 3) // 24
		for i in x:
			for j in x:
				if i == j:
					continue
				(za, zb, k, nums, pre_nums) = (dct[i], dct[j], 0, [], [0])
				for zz in za:
					while k != len(zb) and zz > zb[k]:
						k += 1
					nums.append(len(zb) - k)
					pre_nums.append(pre_nums[-1] + nums[-1])
				for xx in range(len(nums)):
					ans += nums[xx] * pre_nums[xx] - xx * nums[xx] ** 2
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	l = max(A) + 1
	B = [0] * l
	for i in A:
		B[i] += 1
	F = [[0] * l for i in range(l)]
	G = [0] * l
	for i in range(n):
		B[A[i]] -= 1
		for j in range(l):
			ans += F[A[i]][j] * B[j]
		for j in range(l):
			F[j][A[i]] += G[j]
		G[A[i]] += 1
	print(ans)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

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

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		(a1, ls) = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

def decompress(ls):
	d = defaultdict(int)
	c = 0
	rt = []
	for i in ls:
		if i in d:
			rt += (d[i],)
		else:
			d[i] = c
			c += 1
			rt += (d[i],)
	return (rt, c)
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	ans = 0
	n = I()
	(a, m) = decompress(LI())
	c = [[0] * (n + 1) for i in range(m)]
	for i in range(n):
		t = a[i]
		c[t][i + 1] += 1
	for i in range(m):
		for j in range(n):
			c[i][j + 1] += c[i][j]
	for i in range(n - 2):
		for j in range(i + 1, n - 1):
			(x, y) = (a[i], a[j])
			ans += (c[y][i] - c[y][0]) * (c[x][n] - c[x][j + 1])
	print(ans)


from math import sin, cos, tan, asin, acos, atan, sqrt, floor, ceil, pi, log10, log, gcd, hypot
from itertools import accumulate, permutations, combinations
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
from copy import deepcopy
from decimal import Decimal
import heapq
import sys
mod = 10 ** 9 + 7

def input():
	return sys.stdin.readline().rstrip()

def sol():
	n = int(input())
	a = list(map(int, input().split()))
	unique_a = list(set(a))
	unique_a.sort()
	idx_a = {v: i for (i, v) in enumerate(unique_a)}
	lcnt = [[0] * len(unique_a) for i in range(n + 1)]
	for i in range(n):
		value = a[i]
		lcnt[i + 1][idx_a[value]] += 1
		for j in range(len(unique_a)):
			lcnt[i + 1][j] += lcnt[i][j]
	ans = 0
	for j in range(n):
		l_cnt = 0
		for k in range(n - 1, j, -1):
			kv = a[k]
			ans += lcnt[j][idx_a[kv]] * l_cnt
			if kv == a[j]:
				l_cnt += 1
	print(ans)
	return

def main():
	testcase = int(input())
	for i in range(testcase):
		sol()
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math
from collections import defaultdict

def gift():
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		l = [0] * (n + 1)
		ans = 0
		for j in range(n):
			r = [0] * (n + 1)
			for k in range(n - 1, j, -1):
				ans += l[a[k]] * r[a[j]]
				r[a[k]] += 1
			l[a[j]] += 1
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


import sys
input = sys.stdin.buffer.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		A = [a - 1 for a in A]
		X = [[0] * n for i in range(n)]
		for l in range(n - 1):
			for r in range(l + 1, n):
				if A[l] == A[r]:
					X[l][r] += 1
		s = [[0] * (n + 1) for _ in range(n + 1)]
		for i in range(n):
			for j in range(n):
				s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + X[i][j]
		ans = 0
		for l in range(n - 3):
			for r in range(l + 1, n - 1):
				if A[l] == A[r]:
					x1 = l + 1
					x2 = r
					y1 = r + 1
					y2 = n
					ans += s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1]
		print(ans)
main()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class BinaryTrie:

	class Node:

		def __init__(self, bit: bool=False):
			self.bit = bit
			self.children = []
			self.count = 0
			self.counter = 1

	def __init__(self, size):
		self.root = BinaryTrie.Node()
		self.size = size

	def convert(self, key):
		bits = []
		if isinstance(key, int):
			key = bin(key)[2:]
		if isinstance(key, str):
			for i in range(self.size - len(key)):
				bits += [False]
			for i in key:
				if i == '0':
					bits += [False]
				else:
					bits += [True]
		else:
			return list(key)
		return bits

	def add(self, key):
		node = self.root
		bits = self.convert(key)
		for bit in bits:
			found_in_child = False
			for child in node.children:
				if child.bit == bit:
					child.counter += 1
					node = child
					found_in_child = True
					break
			if not found_in_child:
				new_node = BinaryTrie.Node(bit)
				node.children.append(new_node)
				node = new_node
		node.count += 1

	def remove(self, key):
		node = self.root
		bits = self.convert(key)
		nodelist = [node]
		for bit in bits:
			for child in node.children:
				if child.bit == bit:
					node = child
					node.counter -= 1
					nodelist.append(node)
					break
		node.count -= 1
		if not node.children and (not node.count):
			for i in range(len(nodelist) - 2, -1, -1):
				nodelist[i].children.remove(nodelist[i + 1])
				if nodelist[i].children or nodelist[i].count:
					break

	def query(self, prefix, root=None):
		if not root:
			root = self.root
		node = root
		if not root.children:
			return 0
		for bit in prefix:
			bit_not_found = True
			for child in node.children:
				if child.bit == bit:
					bit_not_found = False
					node = child
					break
			if bit_not_found:
				return 0
		return node

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.data = 0
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key, val):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
			pCrawl.data += val
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		c = 0
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None

	def present(self, key):
		ans = 0
		pCrawl = self.root
		length = len(key)
		c = 0
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
			ans += pCrawl.data
		if pCrawl != None:
			return ans
		return -1
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	ind = defaultdict(list)
	for i in range(n):
		ind[l[i]].append(i)
	for i in range(n):
		for j in range(n - 1, i, -1):
			w = binarySearchCount(ind[l[j]], len(ind[l[j]]), i)
			w1 = countGreater(ind[l[i]], len(ind[l[i]]), j)
			ans += w * w1
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a_vec = input().split(' ')
	a_vec = [int(a) for a in a_vec]
	freq = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
	res = 0
	for j in range(n - 1, -1, -1):
		k = j + 1
		for l in range(k + 1, n):
			freq[a_vec[k]][a_vec[l]] += 1
		for i in range(0, j):
			res += freq[a_vec[i]][a_vec[j]]
	print(res)


from collections import deque as dq
from collections import defaultdict as dd
from collections import Counter as ct
from functools import lru_cache as lc
from heapq import heappush as hpush, heappop as hpop, heapify as hfy
from bisect import bisect_left, bisect_right
import itertools as it
import math
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	count = [0] * (n + 1)
	ret = 0
	for i in range(n):
		add = 0
		for j in range(i + 1, n):
			if A[i] == A[j]:
				ret += add
			add += count[A[j]]
		count[A[i]] += 1
	print(ret)
t = int(input())
for i in range(t):
	solve()


import sys
input = sys.stdin.readline

def before(j, a):
	if arr[j] == a:
		return prefix[j][a] - 1
	else:
		return prefix[j][a]

def after(k, b):
	return prefix[n - 1][b] - prefix[k][b]
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	prefix = [[0 for i in range(n + 1)] for j in range(n)]
	for i in range(n):
		for j in range(n + 1):
			prefix[i][j] = prefix[i - 1][j]
		prefix[i][arr[i]] = prefix[i - 1][arr[i]] + 1
	count = 0
	for j in range(n):
		for k in range(j + 1, n):
			count += before(j, arr[k]) * after(k, arr[j])
	print(count)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	col = [0] * (n + 1)
	ans = 0
	col[arr[0]] += 1
	for j in range(1, n - 2):
		c = col[arr[j + 1]]
		val = arr[j]
		for l in range(j + 2, n):
			if arr[l] == val:
				ans += c
			c += col[arr[l]]
		col[arr[j]] += 1
	print(ans)


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	left = [0] * 3001
	cnt = 0
	for j in range(n - 1):
		right = [0] * 3001
		for k in range(n - 1, j, -1):
			cnt = cnt + left[a[k]] * right[a[j]]
			right[a[k]] += 1
		left[a[j]] += 1
	print(cnt)

def main():
	t = int(input())
	while t > 0:
		solve()
		t -= 1
main()


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = inar()
		left_dic = defaultdict(int)
		right_dic = defaultdict(int)
		ans = 0
		for i in range(n - 2):
			right_dic.clear()
			for j in range(n - 1, i, -1):
				if arr[i] in right_dic and arr[j] in left_dic:
					ans += right_dic[arr[i]] * left_dic[arr[j]]
				right_dic[arr[j]] += 1
			left_dic[arr[i]] += 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import defaultdict
tests = int(input())
for t in range(tests):
	n = int(input())
	ls = list(map(int, input().split()))
	res = 0
	for j in range(1, n - 2):
		dict_i = defaultdict(int)
		dict_l = defaultdict(int)
		dict_l[ls[n - 1]] = 1
		for i in range(0, j):
			dict_i[ls[i]] += 1
		for k in range(n - 2, j, -1):
			res += dict_i[ls[k]] * dict_l[ls[j]]
			dict_l[ls[k]] += 1
	print(res)


from bisect import bisect_right
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	relationLower = [[] for _ in range(n)]
	relationHigher = [0] * n
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] == a[j]:
				relationHigher[i] += 1
				relationLower[j].append(i)
	total = 0
	for i in range(n):
		relations = 0
		for j in range(i + 2, n):
			relations += relationHigher[j - 1]
			relations -= len(relationLower[j]) - bisect_right(relationLower[j], i)
			if a[i] == a[j]:
				total += relations
	print(total)


from collections import defaultdict
from bisect import bisect_left, bisect_right

def solve():
	N = int(input())
	a = list(map(int, input().split()))
	res = 0
	d = defaultdict(list)
	for (i, v) in enumerate(a):
		d[v].append(i)
	for j in range(N):
		for k in range(j + 1, N):
			res += bisect_right(d[a[k]], j - 1) * (len(d[a[j]]) - bisect_right(d[a[j]], k))
	print(res)
	return

def main():
	T = int(input())
	for i in range(T):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cl = [0] * (n + 1)
	cr = [0] * (n + 1)
	for i in a:
		cr[i] += 1
	ans = 0
	for i in range(n - 3):
		cl[a[i]] += 1
		now = a[i]
		nr = [cr[j] - cl[j] for j in range(n + 1)]
		cand = 0
		cn = [0] * (n + 1)
		for j in range(i + 1, n - 1):
			y = a[j]
			if y != now:
				cand += (nr[y] - cn[y] - 1) * (cn[y] + 1) - (nr[y] - cn[y]) * cn[y]
				cn[y] += 1
			else:
				ans += cand
				if nr[y] - cn[y] - 1 > 0:
					cand += (nr[y] - cn[y] - 2) * (cn[y] + 1) - (nr[y] - cn[y] - 1) * cn[y]
				cn[y] += 1
	print(ans)


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	l = [0] * (n + 1)
	ans = 0
	for j in range(n):
		r = [0] * (n + 1)
		for k in range(n - 1, j, -1):
			ans += l[a[k]] * r[a[j]]
			r[a[k]] += 1
		l[a[j]] += 1
	print(ans)


import math
import random
from collections import Counter, deque
from sys import stdout
import time
from math import factorial, log, gcd
import sys
from decimal import Decimal
import heapq
from copy import deepcopy

def S():
	return sys.stdin.readline().split()

def I():
	return [int(i) for i in sys.stdin.readline().split()]

def II():
	return int(sys.stdin.readline())

def IS():
	return sys.stdin.readline().replace('\n', '')

def main():
	n = II()
	a = I()
	m = [0] * (n + 1)
	d_left = [tuple(m)]
	for i in range(n):
		m[a[i]] += 1
		d_left.append(tuple(m))
	m = [0] * (n + 1)
	d_right = [tuple(m)]
	for i in range(n - 1, -1, -1):
		m[a[i]] += 1
		d_right.append(tuple(m))
	d_right = d_right[::-1]
	ans = 0
	for k in range(n - 1, -1, -1):
		for j in range(k):
			ans += d_left[j][a[k]] * d_right[k + 1][a[j]]
	print(ans)
for _ in range(II()):
	main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	seen_left = {}
	ans = 0
	for i in range(n - 1):
		seen_right = {}
		for j in reversed(range(i + 1, n)):
			if a[j] in seen_left and a[i] in seen_right:
				ans += seen_left[a[j]] * seen_right[a[i]]
			seen_right[a[j]] = seen_right.get(a[j], 0) + 1
		seen_left[a[i]] = seen_left.get(a[i], 0) + 1
	print(ans)


from collections import defaultdict
t = int(input())
for q in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	dp = [[0] * (n + 1) for i in range(n)]
	dp[0][b[0]] = 1
	i = 1
	while i < n:
		for j in range(1, n + 1):
			if j == b[i]:
				dp[i][j] = dp[i - 1][j] + 1
			else:
				dp[i][j] = dp[i - 1][j]
		i += 1
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			if i == 0:
				p1 = 0
			else:
				p1 = dp[i - 1][b[j]]
			p2 = dp[n - 1][b[i]] - dp[j][b[i]]
			ans += p1 * p2
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	a = RLL()
	res = 0
	l = [0] * (n + 1)
	for j in range(n - 1):
		r = [0] * (n + 1)
		for k in range(n - 1, j, -1):
			res += l[a[k]] * r[a[j]]
			r[a[k]] += 1
		l[a[j]] += 1
	print(res)


from itertools import accumulate as acm, compress as cp
for __ in range(int(input())):
	n = int(input())
	arr = tuple(map(int, input().split()))
	ans = 0
	count = [0] * 3001
	for i in range(n):
		ans += sum(cp(acm(map(count.__getitem__, arr[i + 1:])), map(lambda x: arr[i] == x, arr[i + 2:])))
		count[arr[i]] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = [[0] * n for _ in range(n)]
	ans = 0
	for j in range(1, n):
		for i in range(0, j):
			if c[a[i] - 1][a[j] - 1]:
				c[a[i] - 1][a[j] - 1] += 1
			else:
				c[a[i] - 1][a[j] - 1] = 1
		k = j + 1
		for l in range(k + 1, n):
			ans += c[a[k] - 1][a[l] - 1]
	print(ans)


t = int(input())
for ii in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	total = {}
	for i in a:
		if i not in total:
			total[i] = 1
		else:
			total[i] += 1
	before = []
	for i in range(n):
		before.append([0] * n)
	for i in range(n):
		element = i + 1
		cnt = 0
		for j in range(n):
			if a[j] == element:
				cnt += 1
			before[i][j] = cnt
	tup = 0
	for i in range(1, n - 2):
		for j in range(i + 1, n - 1):
			q = a[i]
			r = a[j]
			if q != r:
				tup += (total[q] - before[q - 1][j]) * before[r - 1][i]
			else:
				tup += (total[q] - before[q - 1][j]) * (before[r - 1][i] - 1)
	print(tup)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def substring_count(a, b):
	m = len(a)
	n = len(b)
	lookup = [[0] * (n + 1) for i in range(m + 1)]
	for i in range(n + 1):
		lookup[0][i] = 0
	for i in range(m + 1):
		lookup[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]
			else:
				lookup[i][j] = lookup[i - 1][j]
	return lookup[m][n]

def solve():
	N = getInt()
	A = getInts()
	a_inds = col.defaultdict(list)
	for (i, a) in enumerate(A):
		a_inds[a].append(i)
	ans = 0
	nums = []
	for key in a_inds:
		val = len(a_inds[key])
		if val >= 4:
			ans += val * (val - 1) * (val - 2) * (val - 3) // 24
			nums.append(key)
		elif val >= 2:
			nums.append(key)
	for i in range(len(nums)):
		for j in range(i + 1, len(nums)):
			A = list(a_inds[nums[i]])
			B = list(a_inds[nums[j]])
			tmp = ''
			while A and B:
				if A[-1] > B[-1]:
					tmp += 'a'
					A.pop()
				else:
					tmp += 'b'
					B.pop()
			while A:
				tmp += 'a'
				A.pop()
			while B:
				tmp += 'b'
				B.pop()
			ans += substring_count(tmp, 'abab')
			ans += substring_count(tmp, 'baba')
	return ans
for _ in range(getInt()):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	base = n + 1
	d = {}
	cnt = 0
	for i in range(n - 2, 0, -1):
		for j in range(i):
			cnt += d.get(a[j] + a[i] * base, 0)
		for k in range(i + 1, n):
			x = a[i] + a[k] * base
			d[x] = d.get(x, 0) + 1
	print(cnt)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	n = int(data())
	a = mdata()
	d = dd(list)
	for i in range(len(a)):
		d[a[i]].append(i)
	ans = 0
	for i in range(n - 1):
		for j in range(i + 1, n):
			ind1 = br(d[a[j]], i)
			ind2 = len(d[a[i]]) - bl(d[a[i]], j)
			if a[i] == a[j]:
				ind1 -= 1
				ind2 -= 1
			ans += ind1 * ind2
	out(ans)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	cntL = [0] * (n + 1)
	cntL[a[0]] += 1
	for j in range(1, n - 2):
		cntR = [0] * (n + 1)
		cntR[a[-1]] += 1
		for k in range(n - 2, j, -1):
			ans += cntL[a[k]] * cntR[a[j]]
			cntR[a[k]] += 1
		cntL[a[j]] += 1
	print(ans)
t = int(input())
for _ in range(t):
	solve()


import sys

def input():
	a = sys.stdin.readline()
	if a[-1] == '\n':
		a = a[:-1]
	return a

def print(*argv):
	n = len(argv)
	for i in range(n):
		if i == n - 1:
			sys.stdout.write(str(argv[i]) + '\n')
		else:
			sys.stdout.write(str(argv[i]) + ' ')

def lcm(x, y):
	from math import gcd
	return x * y / gcd(x, y)

def solve(n, lst):
	cnt = 0
	preC = {}
	for i in range(n):
		sufC = {}
		for j in range(n - 1, -1, -1):
			if i == j:
				break
			(val, val1) = (preC.get(lst[j]), sufC.get(lst[i]))
			if val == None:
				val = 0
			if val1 == None:
				val1 = 0
			cnt += val * val1
			if sufC.get(lst[j]) == None:
				sufC[lst[j]] = 1
			else:
				sufC[lst[j]] += 1
		if preC.get(lst[i]) == None:
			preC[lst[i]] = 1
		else:
			preC[lst[i]] += 1
	return cnt
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	m = max(arr)
	look = [[0 for _ in range(m + 1)] for _ in range(n)]
	for i in range(len(arr)):
		for j in range(m + 1):
			look[i][j] = look[i - 1][j]
		x = arr[i]
		look[i][x] += 1
	ans = 0
	for i in range(1, n - 2):
		for j in range(i + 1, n - 1):
			x = arr[j]
			t = look[i - 1][x]
			y = arr[i]
			v = look[-1][y] - look[j][y]
			ans += t * v
	print(ans)


import bisect
for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	positions = [[] for _ in range(n)]
	last = [-1] * n
	for (i, v) in enumerate(arr):
		positions[v - 1].append(i)
	for i in range(n):
		v2 = arr[i]
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			nj = bisect.bisect_left(positions[arr[j] - 1], i)
			if nj >= len(positions[arr[j] - 1]):
				continue
			nj -= 1
			if nj < 0:
				continue
			ni = bisect.bisect_right(positions[arr[i] - 1], j)
			if ni >= len(positions[arr[i] - 1]):
				continue
			ans += (nj + 1) * (len(positions[arr[i] - 1]) - ni)
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	l = defaultdict(lambda : 0)
	out = 0
	for j in range(n):
		r = defaultdict(lambda : 0)
		for k in range(n - 1, j, -1):
			out += r[arr[j]] * l[arr[k]]
			r[arr[k]] += 1
		l[arr[j]] += 1
	print(out)


from sys import stdin, stdout
from bisect import bisect_right
R = lambda : stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(' '))) if f else list(R().split(' '))
output = lambda x: stdout.write(str(x) + '\n')
output_list = lambda x: output(' '.join(map(str, x)))

def ncr(n, r):
	ans = 1
	for i in range(r):
		ans *= n - i
	return ans
for tc in range(int(R())):
	n = int(R())
	a = RL(f=int)
	cnt = []
	for i in range(n + 1):
		cnt.append((n + 1) * [0])
	ans = 0
	for j in range(n - 1, -1, -1):
		for l in range(j + 2, n, 1):
			cnt[a[j + 1]][a[l]] += 1
		for i in range(j):
			ans += cnt[a[i]][a[j]]
	output(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(RL())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		arr = RLL()
		rec = [[0] * (n + 1)]
		for i in arr:
			now = rec[-1].copy()
			now[i] += 1
			rec.append(now)
		res = 0
		for i in range(1, n - 2):
			for j in range(i + 1, n - 1):
				pre = rec[i][arr[j]] * (rec[-1][arr[i]] - rec[j + 1][arr[i]])
				res += pre
		print(res)
main()


def singleElementCounts(x, l, r):
	if l < 0:
		l = 0
	if r >= n:
		r = n - 1
	if l == 0:
		return singleElementOccurrenceCnt[x][r]
	else:
		return singleElementOccurrenceCnt[x][r] - singleElementOccurrenceCnt[x][l - 1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	singleElementOccurrenceCnt = [[0 for __ in range(n + 1)] for ___ in range(n + 1)]
	for x in range(1, n + 1):
		t = 0
		for i in range(n):
			if a[i] == x:
				t += 1
			singleElementOccurrenceCnt[x][i] = t
	totalCnts = [[0 for __ in range(n + 1)] for ___ in range(n + 1)]
	for i in range(n - 1):
		for j in range(i + 1, n):
			totalCnts[a[i]][a[j]] += 1
	ans = 0
	cntsSoFar = [[0 for __ in range(n + 1)] for ___ in range(n + 1)]
	for j in range(1, n):
		for i in range(j):
			(x, y) = (a[i], a[j])
			cntsSoFar[x][y] += 1
		for i in range(j):
			(x, y) = (a[i], a[j])
			ans += totalCnts[x][y] - cntsSoFar[x][y] - singleElementCounts(x, 0, j) * singleElementCounts(y, j + 1, n - 1)
	print(ans)


from math import *
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	c = []
	k = 3001
	for i in range(n):
		c.append([0] * k)
	for i in range(n):
		if i != 0:
			for j in range(k):
				c[i][j] = c[i - 1][j]
		c[i][ar[i]] += 1
	cs = []
	for i in range(n):
		cs.append([0] * k)
	for i in range(n - 1, -1, -1):
		if i != n - 1:
			for j in range(k):
				a = 0
				cs[i][j] = cs[i + 1][j]
		cs[i][ar[i]] += 1
	ans = 0
	for i in range(1, n - 2):
		for j in range(i + 1, n - 1):
			ans += c[i - 1][ar[j]] * cs[j + 1][ar[i]]
	print(ans)


for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * (n + 10)
	ans = 0
	for l in range(n):
		cur = 0
		for r in range(l + 1, n):
			if a[l] == a[r]:
				ans += cur
			cur += cnt[a[r]]
		cnt[a[l]] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	left = {}
	sum = 0
	for i in range(n - 1):
		right = {}
		for j in range(n - 1, i, -1):
			if arr[i] in right and arr[j] in left:
				sum += right[arr[i]] * left[arr[j]]
			if arr[j] in right:
				right[arr[j]] += 1
			else:
				right[arr[j]] = 1
		if arr[i] in left:
			left[arr[i]] += 1
		else:
			left[arr[i]] = 1
	print(sum)


t = int(input())
for j in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	x = [0 for k in range(n + 1)]
	xy = [[0 for k in range(n + 1)] for t in range(n + 1)]
	xyx = [[0 for k in range(n + 1)] for t in range(n + 1)]
	xyxy = [[0 for k in range(n + 1)] for t in range(n + 1)]
	for a in m:
		for b in range(1, n + 1):
			if a != b:
				xyxy[b][a] += xyx[b][a]
				xyx[a][b] += xy[a][b]
				xy[b][a] += x[b]
		x[a] += 1
	ans = sum(map(sum, xyxy))
	for p in x:
		ans += p * (p - 1) * (p - 2) * (p - 3) // 24
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	matrix = [[] for j in range(n)]
	for j in range(n):
		for k in range(j + 2, n):
			if arr[j] == arr[k]:
				matrix[j].append(k)
	matrix1 = [[0] * n for j in range(n)]
	matrix2 = [[0] * n for j in range(n)]
	matrix3 = [[0] * n for j in range(n)]
	for j in range(n):
		a = len(matrix[j])
		for k in range(a):
			matrix1[matrix[j][k]][j] += 1
	for j in range(n):
		matrix2[0][j] = matrix1[0][j]
	for j in range(n):
		for k in range(1, n):
			matrix2[k][j] = matrix2[k - 1][j] + matrix1[k][j]
	for j in range(n):
		matrix3[j][0] = matrix2[j][0]
	for j in range(n):
		for k in range(1, n):
			matrix3[j][k] = matrix3[j][k - 1] + matrix2[j][k]
	ans = 0
	for j in range(n):
		a = len(matrix[j])
		for k in range(a):
			ans += matrix3[matrix[j][k]][j] + matrix3[-1][matrix[j][k] - 1]
			ans -= matrix3[-1][j] + matrix3[matrix[j][k]][matrix[j][k] - 1]
	print(ans)


def HalfDead():
	n = int(input())
	a = list(map(int, input().split()))
	dp1 = [[0] * (n + 1) for _ in range(n + 1)]
	dp2 = [[0] * (n + 1) for _ in range(n + 1)]
	dp3 = [[0] * (n + 1) for _ in range(n + 1)]
	res = 0
	for v in a:
		for w in range(n + 1):
			res += dp3[w][v]
			dp3[v][w] += dp2[w][v]
			dp2[v][w] += dp1[w][v]
			dp1[v][w] += 1
	print(res)
for _ in range(int(input())):
	HalfDead()


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		m = {}
		ans = 0
		for i in range(n):
			l = [0] * (n + 1)
			for j in range(i + 1, n):
				l[a[j]] += 1
			r = [0] * (n + 1)
			c = 0
			for j in range(i + 1, n):
				if a[i] == a[j]:
					prev = l[a[j]] * r[a[j]]
					now = l[a[j]] * (r[a[j]] - 1)
					x1 = 0
					if prev > 0:
						x1 = prev - now
					ans += c - x1
				prev = l[a[j]] * r[a[j]]
				l[a[j]] -= 1
				r[a[j]] += 1
				now = l[a[j]] * r[a[j]]
				c -= prev
				c += now
		print(ans)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


def main():
	for _ in range(Iint()):
		n = Iint()
		array = Ilist()
		cntLeft = [0] * (n + 1)
		ans = 0
		for (i, Ival) in enumerate(array):
			cntRight = [0] * (n + 1)
			for j in range(n - 1, i, -1):
				ans += cntLeft[array[j]] * cntRight[array[i]]
				cntRight[array[j]] += 1
			cntLeft[array[i]] += 1
		print(ans)

def I():
	return input()

def Iint():
	return int(input())

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

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

def Plist(li, s=''):
	print(s.join(map(str, li)))
main()


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import defaultdict, Counter
import heapq
from math import ceil, floor
from pprint import pprint

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		suf = [[0] * (n + 1) for i in range(n + 1)]
		for i in range(n):
			for j in range(i + 1, n):
				suf[arr[i]][arr[j]] += 1
		ans = 0
		for i in range(n):
			el = arr[i]
			for j in range(i + 1, n):
				suf[el][arr[j]] -= 1
			for j in range(0, i):
				ans += suf[arr[j]][el]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	main()


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	b = {val: i for (i, val) in enumerate(sorted(set(a)))}
	a = [b[x] for x in a]
	cntLeft = {i: 0 for i in range(n)}
	cntRight = {i: 0 for i in range(n)}
	ans = 0
	for j in range(n):
		cntRight = {i: 0 for i in range(n)}
		for k in range(n - 1, j, -1):
			ans += cntLeft[a[k]] * cntRight[a[j]]
			cntRight[a[k]] += 1
		cntLeft[a[j]] += 1
	print(ans)