Under construction

Still being built. Things may change

Albocede DNA (Small)

Time limit5sMemory limit512 MB

Summary
Count subsequences of S that split into blocks of the form a^i b^j c^i d^j with i and j at least 1, modulo 1e9+7.
Level

Medium7 of 10

Topics
Dynamic programming, String, Combinatorics
Solved
No attempts yet

Problem

The DNA of the alien species Albocede is made of four nucleotides: a, b, c, and d. Sequences differ between individuals, but every Albocede DNA sequence obeys all of these rules:

  • Each of a, b, c, and d appears at least once.
  • Every a comes before every b, every b comes before every c, and every c comes before every d.
  • The number of as equals the number of cs.
  • The number of bs equals the number of ds.

For example, abcd and aabbbccddd are valid Albocede DNA sequences. acbd, abc, and abbccd are not.

The Albocede-n is a species that evolved from the Albocede. An Albocede-n DNA sequence is one or more valid Albocede DNA sequences concatenated end to end. For example, abcd and aaabcccdaabbbccdddabcd are valid Albocede-n DNA sequences. A valid Albocede-n DNA sequence is not necessarily a valid Albocede DNA sequence.

An expedition brought back a sequence SS made only of a, b, c, and d. Count the subsequences of SS that are valid Albocede-n DNA sequences. A subsequence is formed by choosing positions of SS and keeping their order, and two different sets of positions count as two different subsequences even when they spell the same string. The answer can be very large, so report it modulo 109+710^9 + 7.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains a string SS made only of the characters a, b, c, and d.

Limits

  • 1≤T≤201 \le T \le 20
  • 1≤∣S∣≤501 \le |S| \le 50

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 answer for that test case.

Examples2

  1. Example 1

    Input
    5
    abcd
    aaaabcd
    aaaabbccd
    abcdabcdaabccd
    b
    
    Expected output
    Case #1: 1
    Case #2: 4
    Case #3: 28
    Case #4: 71
    Case #5: 0
    
  2. Example 2

    Input
    6
    dcba
    abdc
    acbd
    abc
    abbccd
    aabbccdd
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 0
    Case #4: 0
    Case #5: 4
    Case #6: 25