The code is written using hint

this is code is written according to logic of hint give

but as after the iterations of while loop j comes out with one value greater and the answer is coming 19 for array 1,2,3 as it should be 10.
so how should i think to resolve this issue as i applied the given formula as it is

you have to add one extra check

if(j==n)
   j--;

this is because when you come out of this while loop

while(j<n && m.find(arr[j])==m.end())

        {

            m.insert(arr[j]);

        j++;

        }

j will be n not n-1 hence you have to decrease j in this case only

Modified code

I got your point sir, but, even after modifying the code there are 2 test cases for which the code is failing.
i tried submitting it and 2 test cases were still not passing

i have check your code by submitting and then realize that
we also have to decrease j even if j!=n but second condition holds false means we got the duplicate element

so we have to decease j every time
but after calculating formula we again have to increase j for next iteration

Also we have to take mod with 10^9+7

i have done all the modifications and now this code passed all testcases

Modified Code

while i was searching for some more alternate solutions i found this this guy made some changes in
the formula and i tried submitting it and it got submitted but i didn’t understood how it happened as the formula should be applied as it is without making any changes can you pls give it a watch its very minute change

this is the same thing
what he does is replace j with j-1 in the formula

initial formula
(j-i+1)(j-1+2)/2
if you replace j with j-1 it will become
(j-i)
(j-1+1)/2