#include #define _ ios_base::sync_with_stdio(false);cin.tie(0); using namespace std; #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define mp make_pair #define all(a) a.begin(),a.end() #define bitcnt(x) __builtin_popcountll(x) #define MOD 1000000007 #define PI 3.14159265 #define tot 300005 #define BLOCK 20000 #define MAXN 1000000000000000 typedef unsigned long long int uint64; typedef long long int int64; int BIT[26][100005]; int64 fact[100005]; void upd( int idx , int st ){ for( int i = st ; i <= 100002 ; i+= i&(-i) ) BIT[idx][i]++; } int query( int idx , int en ){ int ret = 0; for( int i = en ; i>0 ; i-=i&(-i) ) ret+=BIT[idx][i]; return ret; } int64 powe(int64 a, int64 b){ int64 ret=1; while(b){ if( b&1 ){ ret = ret*a; if( ret > MOD ) ret%=MOD; } b=b/2; a= a*a; if( a > MOD ) a%=MOD; } return ret; } int main(){ string s; int q,l,r; cin>>s; fact[0] = 1; for( int i = 0 ; i < s.length() ; i++ ){ int val = int(s[i])-int('a'); upd( val , i+1 ); fact[i+1] = (int64)(i+1); fact[i+1]*=fact[i]; if( fact[i+1] > MOD ) fact[i+1]%=MOD; } cin>>q; while( q-- ){ scanf("%d%d",&l,&r); int cnt[26] = {0}; int maxodd = -1; for( int i = 0 ; i < 26 ; i ++ ){ cnt[i]= query( i , r ) - query( i , l- 1 ); if( cnt[i] & 1 ) maxodd = max( maxodd , cnt[i] ); } if( maxodd == 0 ) maxodd = -1; int len = 0; int64 maxcnt = 0; for( int i = 0 ; i < 26 ; i++ ){ if( cnt[i] % 2 == 0 ){ len+=cnt[i]; continue; } len+=(cnt[i]-1); if( cnt[i] & 1 ) maxcnt++; } if( maxcnt > 0 ) len++; int64 ans = fact[len/2]; for( int i = 0 ; i < 26 ; i++ ){ int req = cnt[i]/2; if( req < 2 ) continue; ans=ans*powe( fact[req] , MOD -2 ); if( ans > MOD ) ans%=MOD; } if( maxcnt ) ans = ans*maxcnt; if( ans > MOD ) ans%=MOD; printf("%lld\n",ans); } return 0; }