#include using namespace std; const int MAXN=100007; typedef long long LL; LL fact[MAXN], inv[MAXN], mod = 1000000007; int prefix[MAXN][26]; LL power(LL a, LL n) { LL ans = 1LL; while(n) { if(n&1) { ans = (ans*a)%mod; } n>>=1; a = (a*a)%mod; } return ans; } void initialize(string s) { // This function is called once before all queries. for(int i = 0; i < s.length(); ++i) { prefix[i + 1][s[i] - 'a']++; for(int j = 0; j < 26; ++j) { prefix[i + 1][j] += prefix[i][j]; } } fact[0] = 1; for(LL i=1;i=0;i--) { inv[i] = (inv[i+1]*(i+1)) % mod; } } LL answerQuery(int l, int r) { // Return the answer for this query modulo 1000000007. int cnt[26]; for(int i = 0; i < 26; ++i) { cnt[i] = prefix[r][i] - prefix[l-1][i]; } int len = 0, odds = 0; vector vals; for(int i = 0; i < 26; ++i) { len += cnt[i] / 2; if(cnt[i]/2) { // cout << (char)('a' + i) << ":" << cnt[i]/2 << endl; vals.push_back(cnt[i]/2); } odds += cnt[i] % 2; } // cout << len << endl; //compute answer LL ans = fact[len]; //cout << ans << endl; for(int x: vals) { ans = (ans * inv[x]); ans %= mod; //cout << ans << " " << inv[x] << endl; } //possibilites for single element. if(odds) ans = (ans * odds) % mod; return ans; } int main() { string s; cin >> s; initialize(s); int q; cin >> q; for(int a0 = 0; a0 < q; a0++){ int l; int r; cin >> l >> r; LL result = answerQuery(l, r); cout << result << endl; } return 0; }