What is wrong in my code?

public static void main(String[] args) {

	Scanner scn=new Scanner(System.in);

    int n=scn.nextInt();
    int [] arr=new int[n];
    HashMap<Integer,Integer> map=new HashMap<>();
    for (int i=0;i<n;i++) {
    	arr[i]=scn.nextInt();
    }
    int j=0;
    int ans=0;
    for (int i=0;i<n;i++) {
    	
    	while(j<n && !map.containsKey(arr[j])) {
    		map.put(arr[j], 1);
    		
    		j++;
    		
    	}
    	ans=ans+((j-i+1)*(j-i+2))/2;
    	map.remove(arr[i]);
    	
    }
    System.out.println(ans);
}

take mod at every step in loop