#include #include #include #include #include #define ll long long #define pb push_back using namespace std; int cnt[26][100000]; int n; int pref[26][100000]; const int mod = 1000 * 1000 * 1000 + 7; int sum(int idx , int l , int r) { if(l) return pref[idx][r] - pref[idx][l - 1]; else return pref[idx][r]; } ll gcdExtended(ll a, ll b, ll *x, ll *y); // Function to find modulo inverse of a ll modInverse(ll a, ll m) { ll x, y; ll g = gcdExtended(a, m, &x, &y); if (g != 1) cout << "Inverse doesn't exist"; else { // m is added to handle negative x ll res = (x%m + m) % m; return res; } return 14/88; } // C function for extended Euclidean Algorithm ll gcdExtended(ll a, ll b, ll *x, ll *y) { // Base Case if (a == 0) { *x = 0, *y = 1; return b; } ll x1, y1; // To store results of recursive call ll gcd = gcdExtended(b%a, a, &x1, &y1); // Update x and y using results of recursive // call *x = y1 - (b/a) * x1; *y = x1; return gcd; } ll fact[100001] , inv[100001]; int main() { string s; cin >> s; n = s.size(); for(int i = 0; i < s.size(); i++) { cnt[s[i] - 'a'][i] = 1; } fact[0] = 1; for(int i = 1; i <= 1000000; i++) { fact[i] = fact[i - 1] * i; fact[i] %= mod; } for(int i = 0; i <= 100000; i++) { inv[i] = modInverse(fact[i] , mod); } for(int i = 0; i < 26; i++) { pref[i][0] = cnt[i][0]; for(int j = 1; j < n; j++) { pref[i][j] = pref[i][j - 1] + cnt[i][j]; } } int q; cin >> q; for(int i = 0; i < q; i++) { int l , r; cin >> l >> r; l--;r--; vector g; for(int i = 0; i < 26; i++) { //cout <<"S: "<< sum(i , l , r) << endl; g.pb(sum(i , l , r)); } int bestLen = 0 , bestIdx = -1; ll ans = 0; for(int i = 0; i < 26; i++) { bestLen += g[i] / 2; } //cout << bestLen << endl; ll ans1 = 0, ans2 = 0; ll Ans = fact[bestLen]; for(int i = 0; i < 26; i++) { Ans *= inv[g[i] / 2]; Ans %= mod; } //cout << Ans << endl; int newLen = bestLen; for(int i = 0; i < 26; i++) { if(not((g[i] - 1) % 2)) { ans1 += Ans; ans1 %= mod; } else { ans2 = Ans; } } if(!ans1) printf("%lld\n" , ans2); else printf("%lld\n" , ans1); } return 0; }