I think either you have read the question incorrectly or your logic is wrong. You don’t need to find the longest subarray with distinct elements, instead you need to find the sum of lengths of all the subarrays with distinct elements. For that, you could use this logic,
Start iterating from i = 0 and keep a map to store the last occurrence of each number that you’ve encountered so far.Also keep a variable, currMaxLen, that stores the length of the longest subarray that ended on the previous element and contained all distinct elements. Initialize this variable to 0. Now, if the current element hasn’t been encountered before, then it won’t be present in your map, and thus this element is safe to be taken with the longest subarray of distinct elements till the previous element. E.g. if I have an array, 3, 4, 10, 7, 9, 10, 12. Imagine that I have done my iterations till the second last element i.e. 10 and so the currMaxLen = 3. Now, when I come to encounter 12, it is not in the map, so I can easily say that the longest subarray ending in 12 that contains all distinct elements should be of length 1 greater than than currMaxLen, i.e. the array corresponding to 7, 9, 10, 12. Now, if this array has length x, and all of its elements are distinct, then definitely all the subarrays of this subarray will have distinct elements, i.e. {7}, {9}, {10}, {12}, {7, 9}, {9, 10}, {10, 12}, {7, 9, 10},{9, 10, 12}, {7,9, 10, 12} and on the first thought , you should think that we should just increment our answer by x * (x + 1) /2 where x is the value of currMaxLen at that time. But wait, there’s a catch, you shouldn’t include the subarrays like {7}, {9}, {10}, {7, 9}, etc since these arrays are not ending at 12 and so they must have already been accounted for in the answer before. So you just need to add currMaxLen to the answer.
For the other case, if the current element has been encountered before, then you need to take the minimum of the i - lastOccurrence of this element and currMaxLen, (try to reason it through). Similar, reasoning goes into this to increment our final answer.
Also, since the answer is required under a modulo prime, you should take the modulo on every intermediate stage of incrementing your final answer. I hope this clears your doubt.