[Java/Python 3] Traverse indices with same values.
java\n    public int countPairs(int[] nums, int k) {\n        Map<Integer, List<Integer>> indices = new HashMap<>();\n        for (int i = 0; i < nums.length; +
```java\n    public int countPairs(int[] nums, int k) {\n        Map<Integer, List<Integer>> indices = new HashMap<>();\n        for (int i = 0; i < nums.length; ++i) {\n            indices.computeIfAbsent(nums[i], l -> new ArrayList<>()).add(i);\n        } \n        int cnt = 0;\n        for (List<Integer> ind : indices.values()) {\n            for (int i = 0; i < ind.size(); ++i) {\n                for (int j = 0; j < i; ++j) {\n                    if (ind.get(i) * ind.get(j) % k == 0) {\n                        ++cnt;\n                    }\n                }\n            }\n        }\n        return cnt;\n    }\n```\n```python\n    def countPairs(self, nums: List[int], k: int) -> int:\n        cnt, d = 0, defaultdict(list)\n        for i, n in enumerate(nums):\n            d[n].append(i)\n        for indices in d.values():    \n            for i, a in enumerate(indices):\n                for b in indices[: i]:\n                    if a * b % k == 0:\n                        cnt += 1\n        return cnt\n```
['Java', 'Python3']