#P6090. Cube Filling Words

    ID: 19314 Type: Default 1000ms 256MiB

Cube Filling Words

Cube Filling Words

In this problem, you are given an integer aa (with a2a\ge2) and a list of nn valid words, each of length aa. You are asked to construct a cube of side length aa, which is composed of a3a^3 unit cubes. Then, remove all unit cubes that do not lie on an edge of the cube. (Recall that a cube has 12 edges; the only remaining unit cubes are those that lie exactly on these edges; note that the corner cubes are shared by three edges.)

After the removal, each remaining unit cube is filled with a letter. For each of the 12 edges of the cube, when you read off the letters along the edge in one of the two possible directions, you must obtain one of the given valid words. (It is enough that one direction yields a valid word.)

More formally, consider the 8 corner unit cubes as vertices of a cube and the 12 edges connecting them. For each edge connecting vertices uu and vv, assign an ordered word w=w1w2waw = w_1w_2\cdots w_a (which is either one of the words in the input or its reverse) so that the letter in the unit cube at uu (one endpoint) is w1w_1 and at vv is waw_a. Note that different edges share the corner cubes; hence, the letter assigned to a corner must be consistent with all edges incident to that vertex.

Let f(x,y)f(x,y) be the number of ways a word (or its reverse) in the given list can have its first letter equal to the letter corresponding to xx and its last letter equal to the letter corresponding to yy. Then the answer is the sum over all assignments of letters to the 4 even (or one part of the bipartition) vertices of the cube of the product

$$\prod_{\text{odd vertex } v} \Bigl(\sum_{x=0}^{25} \prod_{u\in N(v)} f(\ell(u), x)\Bigr), $$

where N(v)N(v) is the set of its 3 neighbors and (u)\ell(u) is the letter assigned to vertex uu. Two cubes differing by any rotation or reflection are considered different.

Your task is to compute the number of distinct cubes modulo 998244353998244353.

inputFormat

The first line contains two integers aa and nn, where aa is the side length of the cube and nn is the number of valid words. Each of the following nn lines contains a word of length aa. It is guaranteed that a2a \ge 2 and that all words consist of uppercase English letters.

outputFormat

Output a single integer – the number of distinct cubes (i.e. valid letter assignments on the edges) modulo 998244353998244353.

sample

2 1
AA
1