C++ SIMPLE MODULAR ARITHMATIC AND COMBINATORICS | O(N+M) SOLUTION
\n\n# Code\ncpp []\nconst long long mxN = 1e5, MOD = 1e9 + 7;\nvector<long long> fact(mxN + 1), ifact(mxN + 1);\n\nlong long binaryExpo(long long a, long long b
\n\n# Code\n```cpp []\nconst long long mxN = 1e5, MOD = 1e9 + 7;\nvector<long long> fact(mxN + 1), ifact(mxN + 1);\n\nlong long binaryExpo(long long a, long long b) {\n    long long res = 1;\n    while (b > 0) {\n        if (b & 1)\n            res = (res * a) % MOD;\n        a = (a * a) % MOD;\n        b >>= 1;\n    }\n    return res;\n}\n\nvoid calculate_factorials() {\n    fact[0] = 1;\n    for (long long i = 1; i <= mxN; ++i) {\n        fact[i] = (fact[i - 1] * i) % MOD;\n    }\n    ifact[mxN] = binaryExpo(fact[mxN], MOD - 2);\n    for (long long i = mxN - 1; i >= 0; --i) {\n        ifact[i] = (ifact[i + 1] * (i + 1)) % MOD;\n    }\n}\n\nclass Solution {\npublic:\n    long long countAnagrams(string s) {\n        calculate_factorials();\n        vector<vector<long long>> freq;\n        long long ptr = 0, n = s.length();\n        while (ptr < n) {\n            vector<long long> hash(26, 0);\n            while (ptr < n && s[ptr] != \' \')\n                hash[s[ptr++] - \'a\']++;\n            freq.push_back(hash);\n            ptr++;\n        }\n\n        long long ans = 1, cnt = 0, no = 0;\n        for (auto& x : s) {\n            if (x != \' \')\n                cnt++;\n            else {\n              //  cout << cnt << " " << no << endl;\n                ans = (ans % MOD * fact[cnt] % MOD) % MOD;\n                for (auto& x : freq[no])\n                    ans = (ans % MOD * ifact[x] % MOD) % MOD;\n                ++no, cnt = 0;\n            }\n        }\n      //  cout << cnt << " " << no << endl;\n        ans = (ans % MOD * fact[cnt] % MOD) % MOD;\n        for (auto& x : freq[no])\n            ans = (ans % MOD * ifact[x] % MOD) % MOD;\n        return ans;\n    }\n};\n\n```
['Hash Table', 'Math', 'String', 'Combinatorics', 'Counting', 'C++']