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;
}