Count Anagrams
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\npublic class Solution {\n    private static final int MOD = 1000000007;\n\n    // Function to calculate factorial modulo MOD\n    private long factorial(int n) {\n        long result = 1;\n        for (int i = 2; i <= n; i++) {\n            result = (result * i) % MOD;\n        }\n        return result;\n    }\n\n    // Function to calculate the modular inverse using Fermat\'s little theorem\n    private long modInverse(long a, int mod) {\n        return pow(a, mod - 2, mod);\n    }\n\n    // Function to calculate (x^y) % mod\n    private long pow(long x, long y, int mod) {\n        long result = 1;\n        x = x % mod;\n        while (y > 0) {\n            if ((y & 1) == 1) {\n                result = (result * x) % mod;\n            }\n            y = y >> 1;\n            x = (x * x) % mod;\n        }\n        return result;\n    }\n\n    public int countAnagrams(String s) {\n        String[] words = s.split(" ");\n        long result = 1;\n\n        for (String word : words) {\n            Map<Character, Integer> freq = new HashMap<>();\n            for (char c : word.toCharArray()) {\n                freq.put(c, freq.getOrDefault(c, 0) + 1);\n            }\n\n            // Calculate the number of anagrams for this word\n            long wordAnagrams = factorial(word.length());\n            for (int count : freq.values()) {\n                wordAnagrams = (wordAnagrams * modInverse(factorial(count), MOD)) % MOD;\n            }\n\n            result = (result * wordAnagrams) % MOD;\n        }\n\n        return (int) result;\n    }\n}\n```
['Java']