#include using namespace std; map> positions; void initialize(string s) { for(int i = 0; i < s.length(); i++){ positions[s[i] - 'a'].push_back(i+1); } } int fact(int n){ if(n == 1) return 1; return n*fact(n-1); } int answerQuery(int l, int r) { // Return the answer for this query modulo 1000000007. if(l == r) return 1; vector freq(26, 0); int ans = 0; for(int i = 0; i < 26; i++){ vector f = positions[i]; for(int p: f){ if(p < l) continue; if(p > r) break; freq[i]++; } } int count = 0; int ones = 0; for(int i = 0; i < 26; i++){ if(freq[i]){ if(freq[i] % 2 == 0) count++; if(freq[i] == 1) ones++; } } if(ones) ans = ones*fact(count); else ans = fact(count); 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; int result = answerQuery(l, r); cout << result << endl; } return 0; }