Java build-in method to calc mod inverse
Intuition\nAssuming you have already known that a / b != (a % mod) / (b % mod)\n(Division is not allowed as a modular arithmetic operation.) \nOne of the approc
# Intuition\nAssuming you have already known that a / b != (a % mod) / (b % mod)\n(Division is not allowed as a modular arithmetic operation.) \nOne of the approches to handle this problme is using inverse multiplication.)\n\nkudos to this post: https://leetcode.cn/problems/count-anagrams/solution/java-cheng-fa-ni-yuan-nei-zhi-han-shu-by-02y7/\n\nThere is an Java build-in method:\n` public BigInteger modInverse(BigInteger m)` to calc mod inverse\uFF08a BigInteger whose value is this ^ -1 mod m)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nlong inv = BigInteger.valueOf(fac[v]).modInverse(BigInteger.valueOf(mod)).longValue();\n\n# Complexity\n- Time complexity:\nN/A\n\n- Space complexity:\nN/A\n\n# Code\n```\nimport java.math.BigInteger;\nclass Solution {\n    long mod = (long) (1e9 + 7);\n    long[] fac;\n    public int countAnagrams(String s) {\n\n        fac = new long[s.length() + 1];\n        fac[1] = 1;\n      for (int i = 2; i <= s.length(); i++) {\n            fac[i] = (fac[i - 1] * i) % mod;\n        }\n        String[] ws = s.split(" ");\n        long ans = 1;\n        for (String w : ws) {\n            ans = (ans * calc(w)) % mod;\n        }\n        return (int)ans;\n    }\n    private long calc(String w) {\n        Map<Character, Integer> m = new HashMap<>();\n        for (char ch : w.toCharArray()) {\n            m.put(ch, m.getOrDefault(ch, 0) + 1);\n        }\n        long all = fac[w.length()];\n        for (int v : m.values()) {\n            long inv = BigInteger.valueOf(fac[v]).modInverse(BigInteger.valueOf(mod)).longValue();\n            all = all * inv % mod;\n        }\n        return all;\n    }\n}\n```
['Java']