#!/bin/python3 import sys def factorial(x): if (x == 0): return 1 else: return x * factorial(x-1) def initialize(s): # This function is called once before all queries. return def answerQuery(l, r): # Return the answer for this query modulo 1000000007. dubsCount = 0 singCount = 0 myString = s[l-1:r] nums = set() for term in myString: if term in nums: nums.remove(term) dubsCount += 1 singCount -= 1 else: nums.add(term) singCount += 1 if (dubsCount == 0): if (singCount > 0): return 1 else: return 0 if (singCount == 0): return factorial(dubsCount) else: return singCount * (factorial(dubsCount)) 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)