Runs (Large)

Count distinct rearrangements of the letters of S whose number of maximal equal-character blocks equals that of S, modulo 1000003.

Hard8CombinatoricsDynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

You are given a string SS made only of the lowercase letters 'a' to 'z'. A maximal block of equal consecutive characters is called a run. For example, "bookkeeper" has 7 runs. How many distinct rearrangements of SS have exactly as many runs as SS?

Two rearrangements aa and bb are different when some index ii satisfies a[i]b[i]a[i] \ne b[i].

Input

The first line contains the number of test cases TT. Each of the next TT lines contains one non-empty string SS of lowercase letters.

Limits

  • 1T1001 \le T \le 100
  • SS is at least 1 and at most 450000 characters long.
  • SS has at most 100 runs.
  • The input file is at most 1 megabyte in size.

Output

For each test case print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of distinct rearrangements of SS whose run count equals the run count of SS, taken modulo 1000003.