#!/bin/python3 import sys from collections import Counter def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) def initialize(s): # This function is called once before all queries. return s def answerQuery(l, r): # Return the answer for this query modulo 1000000007. st = s[l-1: r] stc = Counter(st) odds = 0 odddict = {} evendict = {} for k in stc: if stc[k] % 2 != 0: odddict[k] = stc[k] odds += 1 else: evendict[k] = stc[k] maxodd = 0 if odds >= 1: maxodd = (max(odddict.values())) nofmax = 0 sumeven = sum(evendict.values()) total = sumeven // 2 + maxodd for k in odddict: if odddict[k] == maxodd: nofmax += 1 totalfac = factorial(total) for k in evendict: totalfac /= factorial(evendict[k] // 2) for k in odddict: totalfac /= factorial(maxodd - 1) return int(totalfac) if __name__ == "__main__": s = input().strip() initialize(s) q = int(input().strip()) for a0 in range(q): l, r = input().strip().split(' ') l, r = [int(l), int(r)] result = answerQuery(l, r) print(result)