#include #include #include #include using namespace std; vector chars; void initialize(string s) { for (int i = 0; i < s.length(); ++i) { chars.push_back(s.at(i)); } } int answerQuery(vector& segment) { if (segment.size() == 0) return 1; int ans = 0; sort(segment.begin(), segment.end()); bool pairFound = false; for (int i = 0; i < segment.size() - 1; ++i) { char here = segment[i]; if (here == segment[i + 1]) { pairFound = true; vector newSegment(segment); newSegment.erase(newSegment.begin() + i, newSegment.begin() + i + 2); ans += answerQuery(newSegment); ans %= 1000000007; while (i < segment.size() && here == segment[i]) { ++i; } --i; } } if (!pairFound) { return segment.size(); } return ans; } int answerQuery(int l, int r) { int ans = 0; vector segment(chars.begin() + l - 1, chars.begin() + r); sort(segment.begin(), segment.end()); bool pairFound = false; for (int i = 0; i < segment.size() - 1; ++i) { char here = segment[i]; if (here == segment[i + 1]) { pairFound = true; vector newSegment(segment); newSegment.erase(newSegment.begin() + i, newSegment.begin() + i + 2); ans += answerQuery(newSegment); ans %= 1000000007; while (i < segment.size() && here == segment[i]) { ++i; } --i; } } if (!pairFound) { return r - l; } return ans; // Return the answer for this query modulo 1000000007. } 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; }