C++ Solution, Math with Explanation [Each step in detail]
Explanation\n1. The basic idea is to multiply the number of ways to write each word.\n2. The number of ways to write a word of size n is n factorial i.e\xA0n!.\
# Explanation\n1. The basic idea is to multiply the number of ways to write each word.\n2. The number of ways to write a word of size n is `n factorial` i.e\xA0`n!`.\n3. For example *"abc" ->"abc","acb","bac","bca","cab","cba".* total 6 ways = 3!.\n3. Here we want total ***unique count***, so words like "aa" can be written in only `1` way. So we divide our ways by the factorial of all repeating characters\' frequencies.\n4. So we create a frequency array of word freq[].\n4. Now our formula is as follows:` ways = n! / ( freq[i]! * freq[i+1]! *...freq[n-1]! )`.\n5. So our overall answer is `ways[i] * ways[i+1] *...ways[n]`.\n6. But the problem here is that our answer can be a large number, so we have to return `modulo 1e9+7`.\n7. Well, that\'s not a big problem as we can just use `cnt % mod` for our answer, but here we have to use modulo for every computation as numbers can be very large. In our formula:` a= n!`, `b = (freq[i]! * freq[i+1]!... freq[n-1]!` is  `(a / b)%mod`.\n8. But ` (a / b) % mod != (a% mod) / (b% mod)`. So here we use `Modular Multiplicative Inverse`,\xA0i.e `(A / B) % mod =\xA0A * ( B ^ -1 ) % mod`. Now `( B ^ -1 ) % mod` we can get by\xA0`Fermat\'s little theorem`, whose end conclusion is\xA0`(b ^ -1) % mod = b ^ (mod -2)`\xA0if mod is `prime`.\n9. now mod is 1e9+7. We can\'t get` b ^ (mod -2)` using for loop which is `O(n)`. for this we can use ***Binary Exponentiation***  which allows us to calculate \u200A`a ^ n` in `O(log(n))`.\n\n\n### Read about   [*Modular Multiplicative Inverse*](https://cp-algorithms.com/algebra/module-inverse.html) and [*Binary Exponentiation*](https://cp-algorithms.com/algebra/binary-exp.html)\n\n\n# *C++ Code*\n```\nclass Solution {\npublic:\nint mod=1e9+7;\nint fact[100002];\n\nint modmul(int a,int b){\n    return((long long)(a%mod)*(b%mod))%mod;\n}\n\nint binExpo(int a,int b){\n    if(!b)return 1;\n    int res=binExpo(a,b/2);\n    if(b&1){\n        return modmul(a,modmul(res,res));\n    }else{\n        return modmul(res,res);\n    }\n}\n\nint modmulinv(int a){\n    return binExpo(a,mod-2);\n}\n\nvoid getfact() {\n    fact[0]=1;\n    for(int i=1;i<=100001;i++){\n        fact[i]=modmul(fact[i-1],i);\n    }\n}\n\nint ways(string str) {\n   int freq[26] = {0};\n   for(int i = 0; i<str.size(); i++) {\n      freq[str[i] - \'a\']++;\n   }\n   int totalWays = fact[str.size()];\n   int factR=1; \n   for(int i = 0; i<26; i++) {\n         factR=modmul(factR,fact[freq[i]]);\n   }\n   return modmul(totalWays,modmulinv(factR));\n}\n \n    int countAnagrams(string s) {\n        getfact();\n        istringstream ss(s);\n        string word; \n         int ans=1;\n        while (ss >> word)\n        {\n            ans=modmul(ans,ways(word));\n        }\n        return ans;\n    }\n  \n};\n```
['Math', 'C++']