Subarrays with distinct elements

I have this code with o(n^2) complexity and its giving tle.
how to optimize it?
#include
#include
using namespace std;
int main()
{

int n; cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
long long int cnt=0;
for(int i=0;i<n;i++)
{
    map<int,bool> m;
    long long int p=0;
    for(int j=i;j<n;j++)
    {
        if(!m[arr[j]])
        {
           p= (p+1)%1000000007;
            m[arr[j]]= true;
        }
    }
    cnt+= ((p*(p+1))/2)%1000000007;
}
cout<<cnt%1000000007;

}

Linear time soution!
Follow this appraoch:

The solution is based on the fact that if we know all elements in a subarray arr[i…j] are distinct, sum of all lengths of distinct element subarrays in this subarray is ((j-i) * (j-i+1))/2 .
How? the possible lengths of subarrays are 1, 2, 3,……, j – i +1. So, the sum will be ((j – i ) * (j – i +1))/2.
We first find largest subarray (with distinct elements) starting from first element. We count sum of lengths in this subarray using above formula. For finding next subarray of the distinct element, we increment starting point, i and ending point, j unless (i+1, j) are distinct. If not possible, then we increment i again and move forward the same way.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Brother you write the entire code and explaination from the gfg. If someone easily understand from gfg then what he do here???