There is an integer array $\boldsymbol{d}$ which does not contain more than two elements of the same value. How many distinct ascending triples ($d[i]<d[j]<d[k],i<j<k$) are present? 

Input format 

The first line contains an integer, $N$, denoting the number of elements in the array. This is followed by a single line, containing $N$ space-separated integers. Please note that there are no leading spaces before the first number, and there are no trailing spaces after the last number.

Output format: 

A single integer that denotes the number of distinct ascending triplets present in the array.

Constraints: 

$N\leq10^5$ 

Every element of the array is present at most twice. 

Every element of the array is a 32-bit non-negative integer.

Sample input:  

6  
1 1 2 2 3 4  

Sample output:  

4

Explanation 

The distinct triplets are 

(1,2,3) 

(1,2,4) 

(1,3,4) 

(2,3,4)

The elements of the array might not be sorted. Make no assumptions of the same.

Difficulty: HARD

Solutions:
root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))


root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


root = 1
last_level = 262144
tree_1 = [0 for i in range(last_level * 2 + 1)]
tree_2 = [0 for i in range(last_level * 2 + 1)]
tri = [0 for i in range(100048)]

def less_than(x, tab):
	index = root
	sum = 0
	c_level = last_level
	while index < x + last_level:
		if x < c_level // 2:
			index *= 2
		else:
			index *= 2
			sum += tab[index]
			index += 1
			x -= c_level // 2
		c_level //= 2
	return sum

def add_elem_1(x):
	tree = tree_1
	index = x + last_level
	tree[index] = 1
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2

def add_elem_2(x):
	tree = tree_2
	index = x + last_level
	tree[index] = less_than(x, tree_1)
	index //= 2
	while index > 0:
		tree[index] = tree[2 * index] + tree[2 * index + 1]
		index //= 2
n = int(input())
n_l = input()
input_array = [int(x) for x in n_l.split()]
for i in input_array:
	add_elem_1(i)
	add_elem_2(i)
	tri[i] = less_than(i, tree_2)
sum = 0
for i in tri:
	sum += i
print(sum)


import math
import os
import random
import re
import sys
from collections import Counter
n = int(input())
num = sorted([(int(x), i + 1) for (i, x) in enumerate(input().split())])
m = n + 1
s = [[0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)]
s[0][0] = 1

def Sum(s, n):
	ret = s[0]
	while n > 0:
		ret += s[n]
		n = n ^ n & ~n + 1
	return ret

def Update(s, n, v):
	while n <= m:
		s[n] += v
		n += n & ~n + 1
for (i, (n, id)) in enumerate(num):
	if i and num[i - 1][0] == num[i][0]:
		continue
	process = [(n, id)]
	if i + 1 < len(num) and num[i][0] == num[i + 1][0]:
		process.append(num[i + 1])
	for state in (3, 2, 1):
		values = []
		for (n, id) in process:
			values.append(Sum(s[state - 1], id - 1))
		if len(values) > 1:
			values[1] -= values[0]
		for (j, (n, id)) in enumerate(process):
			Update(s[state], id, values[j])
print(Sum(s[3], m - 1))