Find the sum of MEX of all subarrays of the given array. The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array.
For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4 because 0, 1, 2 and 3 belong to the array, but 4 does not. It's worth mentioning that the MEX of an array of length n is always between 0 and n inclusive.
Constraints: n<=10^5,a[i]<=n
I thought of a O(n^2) solutions which will not pass the given constraints. We need to find a O(nlogn) or O(n) solution