classSolution { intksm(int a, int b, int c){ int res = 1; while (b > 0) { if (b & 1) res = (longlong)res * a % c; a = (longlong) a * a % c; b >>= 1; } return res; }
intget_ans(string s){ int n = s.length(); int mo = 1000000007; vector<int> f(n + 1); vector<int> ni(n + 1); f[0] = 1; f[1] = 1; ni[0] = 1; ni[1] = 1; for (int i = 2; i <= n; i++) { f[i] = (longlong) f[i - 1] * i % mo; ni[i] = ksm(f[i], mo - 2, mo); } int ans = 0; vector<int> num(26); num[s[s.length() - 1] - 'a']++; for (int now = s.length() - 2; now >= 0; now--) { num[s[now] - 'a']++; int tmp = f[s.length() - now - 1]; for (int j = 0; j < 26; j++) { tmp = (longlong) tmp * ni[num[j]] % mo; } for (int i = s[now] - 'a' - 1; i >= 0; i--) { if (num[i] == 0) continue; int qq = (longlong) tmp * num[i] % mo; ans = (ans + qq) % mo; } } return ans % mo; }