Link to this code: https://cses.fi/paste/10b865ef59bbe20ef6257a/
#include<iostream>
#include<map>

long long choose2(long long n) {
	return (n * (n - 1LL)) / 2LL;
}

int main() {
	int n;
	std::cin >> n; // n := input()

	std::map<int, int> last_incidence_table; // 
	long long count = 0LL;
	int i = 1;
	int j = 1;
	// current subarray is [1,1]
	while (j <= n) {
		int x_j;
		std::cin >> x_j; // x(j) := input()
		int last_incidence_table_x_j = last_incidence_table[x_j]; // last_incidence_table(x(j))
		if (last_incidence_table_x_j >= i) { // [i,j] has a repeat element!
			count = count + choose2(j - i + 1); // add the cardinality of the ideal of this maximal distinct subarray I([i,j-1])
			count = count - choose2((j - 1) - last_incidence_table_x_j + 1); // subtract the cardinality of the intersection of this ideal and the next
			i = last_incidence_table_x_j + 1;
		}
		last_incidence_table[x_j] = j; // update table, last_incidence_table(x(j)) := j
		j = j + 1; // advance right pointer
	}
	count = count + choose2(j - i + 1); // count the straggler maximal distinct subarray
	std::cout << count << std::endl; // print count
	return 0;
}