Modular Arithmetic | Inverse Modulo | Explanation with comments
\nclass Solution\n{\n\tlong long binExpo(long long a, long long b, long long m)\n\t{\n\t\tif (b == 0)\n\t\t{\n\t\t\treturn 1;\n\t\t}\n\t\tlong long ans = binExp
```\nclass Solution\n{\n\tlong long binExpo(long long a, long long b, long long m)\n\t{\n\t\tif (b == 0)\n\t\t{\n\t\t\treturn 1;\n\t\t}\n\t\tlong long ans = binExpo(a, b / 2, m);\n\t\tans = (ans * ans) % m;\n\t\tif (b & 1)\n\t\t{\n\t\t\tans = (ans * a) % m;\n\t\t}\n\t\treturn ans;\n\t}\n\npublic:\n\tint countAnagrams(string s)\n\t{\n\t\t// conversion of string of words into vector of words for simplicity\n\t\tstringstream ss(s);\n\t\tvector<string> words;\n\t\tstring str = "";\n\t\tint maxi = -1; // storing maximum length of word in s\n\n\t\twhile (ss >> str)\n\t\t{\n\t\t\twords.push_back(str);\n\t\t\tint len = str.size();\n\t\t\tmaxi = max(maxi, len); // calculating the maximum length of word for precomputation\n\t\t}\n\n\t\tlong long fact[maxi + 1];\n\t\tfact[0] = 1;\n\t\tlong long mod = 1e9 + 7;\n\t\tfor (int i = 1; i <= maxi; i++)\n\t\t{\n\t\t\tfact[i] = (fact[i - 1] * i) % mod;\n\t\t}\n\t\tunordered_map<char, int> freq; // used to store freq of each of each character\n\t\tint res = 1;\t\t\t\t   // final result\n\n\t\tfor (auto &curr : words)\n\t\t{\n\t\t\tfor (auto &j : curr)\n\t\t\t{ // calculating the freq of each char of curr word\n\t\t\t\tfreq[j]++;\n\t\t\t}\n\t\t\tlong ans = fact[curr.size()];\n\t\t\tfor (auto j : freq)\n\t\t\t{\n\t\t\t\tans = (ans * binExpo(fact[j.second], mod - 2, mod)) % mod; // inverse modulo\n\t\t\t}\n\t\t\tres = ((res % mod) * (ans % mod)) % mod;\n\t\t\tfreq.clear();\n\t\t}\n\t\treturn res;\n\t}\n};\n```
['Math', 'String', 'Combinatorics', 'Counting', 'C++']