PUMaC 2008 Combinatrics A Problem 8

\mathrm{SET} cards have four characteristics: number, color, shape, and shading, each of which has 3 values. A \mathrm{SET} deck has 81 cards, one for each combination of these values. A \mathrm{SET} is three cards such that, for each characteristic, the values of the three cards for that characteristics are either all the same or all different. In how many ways can you replace each \mathrm{SET} card in the deck with another \mathrm{SET} card (possibly the same), with no card used twice, such that any three cards that were a \mathrm{SET} before are still a \mathrm{SET}?

(Alternately, a \mathrm{SET} card is an ordered 4-tuple of 0 \mathrm{s}, 1 \mathrm{s}, and 2 \mathrm{s}, and three cards form a \mathrm{SET} if their sum is (0,0,0,0) \bmod 3; for instance, (0,1,2,2),(1,0,2,1), and (2,2,2,0) form a \mathrm{SET}. How many permutations of the \mathrm{SET} cards maintain \mathrm{SET}-ness?)