#include #include #include #include #include using namespace std; int ch_pos(char ch) { return ch - 'a'; } const int MAX_N = 100500; const long long MODULO = 1000000007; long long fact[MAX_N]; long long fact_op[MAX_N]; long long fast_pow(long long x, long long d) { if (x == 0) { return 0; } if (d == 0) { return 1; } if (d == 1) { return x; } long long half = fast_pow(x, d / 2); long long ans = (half * half) % MODULO; if (d %2 == 1) { ans = (ans * x) % MODULO; } return ans; } int main() { fact[0] = 1; fact_op[0] = 1; for (long long i = 1; i < MAX_N; ++i) { fact[i] = (fact[i - 1] * i) % MODULO; fact_op[i] = fast_pow(fact[i], MODULO - 2); long long check_prod = (fact[i] * fact_op[i]) % MODULO; if (check_prod != 1) { cerr << "wrong opposite elem" << endl; cerr << fact[i] << " " << fact_op[i] << endl; } } ios_base::sync_with_stdio(false); string s; cin >> s; vector> cnt(s.size() + 1, vector(26, 0)); for (int i = 0; i < s.size(); ++i) { for (int j = 0; j < 26; ++j) { cnt[i + 1][j] = cnt[i][j]; } ++cnt[i + 1][ch_pos(s[i])]; } int Q; cin >> Q; for (int q = 0; q < Q; ++q) { int l, r; cin >> l >> r; // cerr << "query " << l << " " << r << endl; long long n_even_pairs = 0; long long n_odd_guys = 0; long long ans = 1; for (int j = 0; j < 26; ++j) { long long cnt_j = (cnt[r][j] - cnt[l - 1][j]); // cerr << ((char)('a' + j)) << " " << cnt_j << endl; n_even_pairs += cnt_j / 2; n_odd_guys += cnt_j % 2; ans = (ans * fact_op[cnt_j / 2]) % MODULO; } // cerr << "n_odd " << n_odd_guys << endl; // cerr << "n_even " << n_even_pairs << endl; ans = (ans * fact[n_even_pairs]) % MODULO; ans = (ans * max(1LL, n_odd_guys)) % MODULO; cout << ans << "\n"; } return 0; }