Permutation logic
Code
# Code
```cpp []
#define li long long
const li MOD = 1e9 + 7;
class Solution {
public:
    li mod_exp(li x, li y, li mod) {
        li res = 1;
        while (y > 0) {
            if (y % 2 == 1)
                res = (res * x) % mod;
            x = (x * x) % mod;
            y /= 2;
        }
        return res;
    }
    li mod_inv(li x, li mod) { return mod_exp(x, mod - 2, mod); }
    int countAnagrams(string s) {
        li n = s.size();
        vector<int> dp(n + 1, 1);
        for (li i = 1; i <= n; i++)
            dp[i] = (i * dp[i - 1]) % MOD;
        string str = "";
        li i = 0, ans = 1;
        while (i < n) {
            vector<li> count(26, 0);
            li p = 0;
            while (i < n && s[i] != ' ') {
                count[s[i] - 'a']++;
                p++;
                i++;
            }
            li r = dp[p];
            for (li j = 0; j < 26; j++) {
                if (count[j] > 0)
                    r = (r * mod_inv(dp[count[j]], MOD)) % MOD;
            }
            ans = (ans * r) % MOD;
            i++;
        }
        return ans;
    }
};
```
['C++']