Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define endl '\n'
- #define ff first
- #define ss second
- #define pb push_back
- // #define int long long
- #define sz(v) (int)v.size()
- #define inf 2147483647
- #define llinf 9223372036854775807
- #define all(v) v.begin(),v.end()
- #define bp(n) __builtin_popcountll(n)
- #define f(i,l,r) for(int i=l;i<=r;i++)
- #define rf(i,r,l) for(int i=r;i>=l;i--)
- #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
- const int N = 5e3 + 5, mod = 1e9 + 7, bit = 61;
- // Be careful about overflow as we are using integer everywhere
- template<const int &MOD>
- struct _m_int // This is our new type which will do operations under modulo MOD
- {
- int val; // Value of variable
- _m_int(int64_t v = 0) // Typecasting into our data type from 64 bit integer
- {
- if (v < 0) v = v % MOD + MOD;
- if (v >= MOD) v %= MOD;
- val = v;
- }
- static int mod_inv(int a, int m = MOD)
- {
- // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
- int g = m, r = a, x = 0, y = 1;
- while (r != 0)
- {
- int q = g / r;
- g %= r; swap(g, r);
- x -= q * y; swap(x, y);
- }
- return x < 0 ? x + m : x;
- }
- explicit operator int() const
- {
- return val;
- }
- explicit operator int64_t() const
- {
- return val;
- }
- _m_int& operator += (const _m_int &other) // Addition
- {
- val -= MOD - other.val;
- if (val < 0) val += MOD;
- return *this;
- }
- _m_int& operator -= (const _m_int &other) // Subtraction
- {
- val -= other.val;
- if (val < 0) val += MOD;
- return *this;
- }
- static unsigned fast_mod(uint64_t x, unsigned m = MOD) // Mod operation
- {
- #if !defined(_WIN32) || defined(_WIN64)
- return x % m;
- #endif
- // Optimized mod for Codeforces 32-bit machines.
- // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.
- unsigned x_high = x >> 32, x_low = (unsigned) x;
- unsigned quot, rem;
- asm("divl %4\n"
- : "=a" (quot), "=d" (rem)
- : "d" (x_high), "a" (x_low), "r" (m));
- return rem;
- }
- _m_int& operator*=(const _m_int &other) // Multiplication
- {
- val = fast_mod((uint64_t) val * other.val);
- return *this;
- }
- _m_int& operator/=(const _m_int &other) // Division
- {
- return *this *= other.inv();
- }
- friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
- friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
- friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
- friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }
- _m_int& operator++() // Pre-increment
- {
- val = val == MOD - 1 ? 0 : val + 1;
- return *this;
- }
- _m_int& operator--() // Pre-decrement
- {
- val = val == 0 ? MOD - 1 : val - 1;
- return *this;
- }
- _m_int operator++(int) { _m_int before = *this; ++*this; return before; } // Post increment
- _m_int operator--(int) { _m_int before = *this; --*this; return before; } // Post decrement
- _m_int operator-() const // Change sign
- {
- return val == 0 ? 0 : MOD - val;
- }
- // Boolean operators
- bool operator==(const _m_int &other) const { return val == other.val; }
- bool operator!=(const _m_int &other) const { return val != other.val; }
- bool operator< (const _m_int &other) const { return val < other.val; }
- bool operator<=(const _m_int &other) const { return val <= other.val; }
- bool operator> (const _m_int &other) const { return val > other.val; }
- bool operator>=(const _m_int &other) const { return val >= other.val; }
- _m_int inv() const // Calculating inverse
- {
- return mod_inv(val);
- }
- _m_int pow(int64_t p) const // Calculating power
- {
- if (p < 0)
- return inv().pow(-p);
- _m_int a = *this, result = 1;
- while (p > 0)
- {
- if (p & 1)
- {
- result *= a;
- }
- a *= a;
- p >>= 1;
- }
- return result;
- }
- friend ostream& operator<<(ostream &os, const _m_int &m) // Writing output
- {
- return os << m.val;
- }
- };
- extern const int MOD = 1e9 + 7;
- using mod_int = _m_int<MOD>;
- mod_int fact[N], ifact[N];
- void pre()
- {
- fact[0] = 1;
- int i;
- for (i = 1; i < N; i++)
- {
- fact[i] = (fact[i - 1] * i);
- }
- ifact[N - 1] = fact[N - 1].inv();
- for (i = N - 2; i >= 0; i--)
- {
- ifact[i] = (ifact[i + 1] * (i + 1));
- }
- }
- vector<mod_int> multiply(vector<mod_int> &a, vector<mod_int> &b)
- {
- int n = sz(a);
- int m = sz(b);
- vector<mod_int> res(n + m);
- f(i, 0, n - 1)
- {
- f(j, 0, m - 1)
- {
- res[i + j] += a[i] * b[j];
- }
- }
- return res;
- }
- signed main()
- {
- fast;
- pre();
- string s;
- int mp[26] = {0};
- cin >> s;
- for (auto &x : s)
- {
- mp[x - 'a']++;
- }
- vector<mod_int> res{1};
- f(i, 0, 25)
- {
- if (mp[i] == 0)
- {
- continue;
- }
- vector<mod_int> now;
- f(j, 0, mp[i])
- {
- now.pb(ifact[j]);
- }
- res = multiply(res, now);
- }
- int n = sz(s);
- n = min(n, sz(res));
- mod_int ans = 0;
- f(i, 1, n)
- {
- ans += (fact[i] * res[i]);
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement