Eunjin likes playing dominoes. A domino is a rectangle divided into two squares, and each square contains one integer from 0 through 9. The two numbers on a piece are always different, so there are 45 possible pieces. A piece may be flipped; for example, the piece containing 1 and 2 is the same piece in both orientations below.
Some of the pieces have been taken away. With the remaining pieces, Eunjin wants to know how many cycle collections can be made.
A cycle collection is a set of one or more cycles that do not share pieces. In a cycle, the left number of each placed piece is equal to the right number of the previous placed piece. The left number of the first piece must also be equal to the right number of the last piece. Pieces may be flipped before being placed.
The diagram above shows three different cycle collections that can be made from the same set of pieces.
A piece is connected to the two pieces surrounding it. The first and last pieces of a cycle are also connected.
Two cycles are the same if every piece is connected to the same pieces in both cycles. Two cycle collections are the same if they contain the same set of cycles.
Given the remaining pieces, write a program that counts how many cycle collections can be made. Every given piece must be used.