#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef long double ld; typedef std::vector vi; typedef std::vector vb; typedef std::vector vs; typedef std::vector vd; typedef std::vector vll; typedef std::vector > vvi; typedef vector vvll; typedef std::vector > vpi; typedef vector vvpi; typedef std::pair pi; typedef std::pair pll; typedef std::vector vpll; const long long mod = 1000000007; const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937_64 gen(gen_seed); #define all(c) (c).begin(),(c).end() #define forn(i, a, b) for(int i = a; i < b; i++) #define read(x) scanf("%d", &x) #define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i]) #define pb push_back #define mp make_pair ll gcd (ll a, ll b, ll & x, ll & y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll d = gcd (b%a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } ll invmod(ll a, ll modulo) { ll x, y; gcd(a, modulo, x, y); return (x%modulo+modulo)%modulo; } int main() { vll fac(1,1); vll nfac(1,1); const int A = 100010; for(ll i = 1; i<=A; i++) { fac.pb(fac.back()*i%mod); nfac.pb(invmod(fac.back(),mod)); } string s; getline(cin, s); int q; scanf("%d", &q); int n = s.size(); vvi cnt(26, vi(n+1,0)); forn(i,0,n) { forn(j,0,26) cnt[j][i+1] = cnt[j][i]; cnt[s[i]-'a'][i+1]++; } forn(affa,0,q) { int l,r; scanf("%d %d", &l, &r); vi num; forn(i,0,26) num.pb(cnt[i][r] - cnt[i][l-1]); vi a; int odd=0; for(auto x : num) { if(x%2) { odd++; x--; } if(x==0) continue; a.pb(x/2); } ll ans = 1; if(odd>0) ans=odd; int sum = 0; for(auto x : a) sum+=x; ans = ans*fac[sum]%mod; for(auto x : a) ans = ans*nfac[x]%mod; printf("%lld\n", ans); } }