#include <iostream>
#include <map>
#include <string>
using namespace std;
// Function to calculate factorial
long long fact(int f) {
if (f == 0 || f == 1) return 1;
return f * fact(f - 1);
}
// Function to calculate rank of a string
void solve(string s, int n) {
long long ans = 1;
map<char, int> freq;
int len = n - 1;
// Count character frequencies
for (int i = 0; i < n; i++) {
freq[s[i]]++;
}
// Calculate rank
for (int i = 0; i < n; i++) {
char c = s[i];
// Calculate permutations for characters smaller than current
for (auto& pair : freq) {
if (pair.first >= c) break;
if (pair.second > 0) {
pair.second--;
long long denominator = 1;
for (auto& f : freq) {
denominator *= fact(f.second);
}
ans += (fact(len) / denominator);
pair.second++;
}
}
freq[c]--;
if (freq[c] == 0) {
freq.erase(c);
}
len--;
}
cout << ans << endl;
}
int main() {
string s;
cin >> s;
solve(s, s.size());
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIGZhY3RvcmlhbApsb25nIGxvbmcgZmFjdChpbnQgZikgewogICAgaWYgKGYgPT0gMCB8fCBmID09IDEpIHJldHVybiAxOwogICAgcmV0dXJuIGYgKiBmYWN0KGYgLSAxKTsKfQoKLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIHJhbmsgb2YgYSBzdHJpbmcKdm9pZCBzb2x2ZShzdHJpbmcgcywgaW50IG4pIHsKICAgIGxvbmcgbG9uZyBhbnMgPSAxOwogICAgbWFwPGNoYXIsIGludD4gZnJlcTsKICAgIGludCBsZW4gPSBuIC0gMTsKCiAgICAvLyBDb3VudCBjaGFyYWN0ZXIgZnJlcXVlbmNpZXMKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZnJlcVtzW2ldXSsrOwogICAgfQoKICAgIC8vIENhbGN1bGF0ZSByYW5rCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNoYXIgYyA9IHNbaV07CgogICAgICAgIC8vIENhbGN1bGF0ZSBwZXJtdXRhdGlvbnMgZm9yIGNoYXJhY3RlcnMgc21hbGxlciB0aGFuIGN1cnJlbnQKICAgICAgICBmb3IgKGF1dG8mIHBhaXIgOiBmcmVxKSB7CiAgICAgICAgICAgIGlmIChwYWlyLmZpcnN0ID49IGMpIGJyZWFrOwogICAgICAgICAgICBpZiAocGFpci5zZWNvbmQgPiAwKSB7CiAgICAgICAgICAgICAgICBwYWlyLnNlY29uZC0tOwogICAgICAgICAgICAgICAgbG9uZyBsb25nIGRlbm9taW5hdG9yID0gMTsKICAgICAgICAgICAgICAgIGZvciAoYXV0byYgZiA6IGZyZXEpIHsKICAgICAgICAgICAgICAgICAgICBkZW5vbWluYXRvciAqPSBmYWN0KGYuc2Vjb25kKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGFucyArPSAoZmFjdChsZW4pIC8gZGVub21pbmF0b3IpOwogICAgICAgICAgICAgICAgcGFpci5zZWNvbmQrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgZnJlcVtjXS0tOwogICAgICAgIGlmIChmcmVxW2NdID09IDApIHsKICAgICAgICAgICAgZnJlcS5lcmFzZShjKTsKICAgICAgICB9CiAgICAgICAgbGVuLS07CiAgICB9CgogICAgY291dCA8PCBhbnMgPDwgZW5kbDsKfQoKaW50IG1haW4oKSB7CiAgICBzdHJpbmcgczsKICAgIGNpbiA+PiBzOwogICAgc29sdmUocywgcy5zaXplKCkpOwogICAgcmV0dXJuIDA7Cn0K