[C++] Combinatorics, Permutations
\nclass Solution {\npublic:\n    int countAnagrams(string s) {\n        int mod = 1e9 + 7;\n        int n = s.size();\n        long anagrams = 1;\n        vecto
```\nclass Solution {\npublic:\n    int countAnagrams(string s) {\n        int mod = 1e9 + 7;\n        int n = s.size();\n        long anagrams = 1;\n        vector<long> facts(n + 1);\n        facts[0] = 1;\n        for (int i = 1; i <= n; i++) {\n            facts[i] = facts[i - 1] * i % mod;\n        }\n        unordered_map<int, long> invs;\n        int count = 0;\n        array<int, 26> counts = {0};\n        for (int i = 0; i <= n; i++) {\n            if (i < n && s[i] != \' \') {\n                counts[s[i] - \'a\']++;\n                count++;\n            } else {\n                long perms = facts[count];\n                for (const auto& v : counts) {\n                    perms = perms * mod_inv(facts[v], mod, invs) % mod;\n                }\n                anagrams = anagrams * perms % mod;\n                counts.fill(0);\n                count = 0;\n            }\n        }\n        return anagrams;\n    }\n\nprivate:\n    long mod_inv(long a, long m, unordered_map<int, long>& cache) {\n        auto it = cache.find(a);\n        if (it != cache.end()) {\n            return it->second;\n        } else {\n            long inv = mod_exp(a, m - 2, m) % m;\n            cache[a] = inv;\n            return inv;\n        }\n    }\n\n    long mod_exp(long a, long b, long m) {\n        long power = 1;\n        while (b > 0) {\n            if (b & 1) {\n                power = power * a % m;\n            }\n            a = a * a % m;\n            b >>= 1;\n        }\n        return power;\n    }\n};\n```
['Hash Table', 'Math', 'String', 'Combinatorics', 'C++']