#!/bin/ruby s = gets.chomp; M = 1_000_000_007; class Integer def ! (1..self).reduce :* end end gets.to_i.times{ counts = Hash.new(0); l, r = gets.split.map &:to_i; l -= 1; r -= 1; l.upto(r){|k| counts[s[k].intern] += 1; } #p counts vals = counts.values vals.sort_by{|e| -e}; take_impaire = 0; total_len = 0; take = [ ]; impaire_len = []; vals.each{|val| if val & 1 == 0 take << val/2; total_len += val / 2; elsif take_impaire == val impaire_len << val; elsif take_impaire < val impaire_len.clear; take_impaire = val; impaire_len << val; end } take.sort_by{|e| -e}; #puts "total_len = #{total_len} ; take = #{take} ; impaire_len = #{impaire_len} ; take_impaire = #{take_impaire}" puts ((impaire_len.reduce(:+)||1)*(take[0]+1).upto(total_len).inject(1){|m,v| m * v} / 1.upto(take.count-1).inject(1){|m,v| m * v.!}) % M; }