http://www.codechef.com/problems/WCOUNT/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WordCounting
{
class MainClass
{
private static int _module = 1000000007;
private static long[] _cache = new long[501];
private static long[] _powersOf2 = new long[30];
static void Main(string[] args)
{
_cache[0] = 1;
_cache[1] = 1;
var words = new List<string>();
var count = int.Parse(Console.ReadLine());
for (var i = 0; i < count; i++)
words.Add(Console.ReadLine().Trim());
foreach (var item in words)
Console.WriteLine(Variants(item));
}
static Dictionary<char, int> GetFreq(string word)
{
var result = new Dictionary<char, int>(26*2);
foreach (var c in word.ToCharArray())
{
if (result.ContainsKey(c))
result[c]++;
else
result.Add(c, 1);
}
return result;
}
static long Variants(string word)
{
long result = 1;
var l = word.Length;
var stat = GetFreq(word);
var current = GetDeviders(stat);
result = Fact(l);
var inv = Inverse (current);
//Console.WriteLine ("inv fact {0}={1}", current, inv);
result = result * inv;
return result % _module;
}
static long Fact(int n)
{
if (_cache[n] == 0)
{
_cache[n] = (Fact(n-1) * n) % _module;
}
return _cache[n];
}
static long Inverse(long value)
{
_powersOf2 [0] = 1;
_powersOf2 [1] = value % _module;
//_powersOf2 [2] = (value * value) % _module;
for (var i = 2; i < _powersOf2.Length; i++)
{
_powersOf2 [i] = (_powersOf2 [i - 1] * _powersOf2 [i - 1]) % _module;
}
var pointer = _module - 2;
long result = 1;
// a inverse = a in power of m - 2
var bits = new BitArray (BitConverter.GetBytes (pointer));
for (var i = 0; i < _powersOf2.Length; i++)
{
if (bits [i])
{
result *= _powersOf2 [i];
result = result % _module;
}
}
return result % _module;
}
static long GetDeviders(Dictionary<char, int> stat)
{
long result = 1;
foreach (var item in stat)
{
if (item.Value == 1)
continue;
else
{
result = (result * Fact(item.Value)) % _module;
}
}
return result % _module;
}
}
}