import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class cactus_gk_mod {
    public static final Number ONE = new Number(1);
    static Scanner in;
    static PrintWriter out;

    static final long HASH_A = 438533872502385038L;
    static final long HASH_X = 373465329856935435L;
    static final long HASH_INV_X = BigInteger.valueOf(HASH_X).modInverse(BigInteger.valueOf(2).pow(64)).longValue();

    static final long MOD = 987654323;
    static final BigInteger MOD_BI = BigInteger.valueOf(MOD);

    class Edge {
        int id;
        Vertex to;

        Edge(int id, Vertex to) {
            this.id = id;
            this.to = to;
        }
    }

    class Cycle extends Feature {
        List<Vertex> cycle;
        Map<Feature, Integer> indexes = new HashMap<>();
        Hash[] hashes;
        Number variants;
        long[] fhs;
        long[] rhs;
        int state;
        int missing = -1;

        Cycle(List<Vertex> cycle) {
            this.cycle = cycle;
            fhs = new long[cycle.size() + 1];
            rhs = new long[cycle.size() + 1];
            hashes = new Hash[cycle.size()];
            for (int i = 0; i < cycle.size(); i++) {
                indexes.put(cycle.get(i), i);
            }
        }

        Hash hash(Feature up) {
            int n = cycle.size();
            if (state == 0) {
                for (int i = 0; i < n; i++) {
                    if (cycle.get(i) != up) {
                        hashes[i] = cycle.get(i).hash(this);
                    } else {
                        hashes[i] = new Hash(0, ONE);
                        missing = i;
                    }
                }
            } else if (state == 1) {
                if (cycle.get(missing) == up) {
                    state = 0;
                } else {
                    hashes[missing] = cycle.get(missing).hash(this);
                }
            }
            if (state <= 1) {
                for (int i = 1; i <= n; i++) {
                    fhs[i] = fhs[i - 1] + hashPowers[i - 1] * hashes[i - 1].hash;
                    rhs[i] = rhs[i - 1] + hashPowers[n - i] * hashes[i - 1].hash;
                }
                variants = new Number();
                for (int i = 0; i < n; i++) {
                    variants.multiply(hashes[i].variants);
                }
                state++;
            }


            int i = indexes.get(up);
            Number vars = variants.divide(hashes[i].variants.value);
            long fh = (fhs[n] - fhs[i + 1]) * inversePowers[i + 1] + fhs[i] * hashPowers[n - i - 1];
            long rh = (rhs[n] - rhs[i + 1]) * hashPowers[i]        + rhs[i] * inversePowers[n - i];
            return new Hash(magic(fh + rh), rh == fh && n != 2 ? vars.multiply(2) : vars);
        }
    }

    static class Number {
        long value;

        Number(long value) {
            this.value = value;
        }

        public Number() {
            this(1);
        }

        public Number multiply(Number that) {
            return multiply(that.value);
        }

        public Number multiply(long c) {
            value = (value * c) % MOD;
            return this;
        }

        public Number divide(long c) {
            return new Number(BigInteger.valueOf(value).multiply(BigInteger.valueOf(c).modInverse(MOD_BI)).longValue());
        }
    }

    class Hash {
        long hash;
        Number variants;

        Hash(long hash, Number variants) {
            this.hash = hash;
            this.variants = variants;
        }
    }

    class Vertex extends Feature {
        Map<Feature, Hash> cache = new HashMap<>();

        Hash cachedHash(Feature feature) {
            if (feature instanceof Cycle) {
                return feature.hash(this);
            }
//            return feature.hash(this);
            Hash hash = cache.get(feature);
            if (hash == null) {
                hash = feature.hash(this);
                cache.put(feature, hash);
            }
            return hash;
        }

        int state;
        List<Edge> edges = new ArrayList<>();
        List<Feature> features = new ArrayList<>();
        Map<Long, Integer> counts = new HashMap<>();
        Feature missing = null;
        Number variants = new Number();
        long h = HASH_A;

        public int dfs(List<Vertex> stack, int eid) {
            stack.add(this);
            state = 1;
            int top = stack.size();
            for (Edge e : edges) {
                if (e.to.state == 0) {
                    int ntop = e.to.dfs(stack, e.id);
                    if (ntop > stack.size()) {
//                        List<Vertex> vertices = new ArrayList<>(2);
//                        vertices.add(this);
//                        vertices.add(e.to);
//                        Cycle cycle = new Cycle(vertices);
//                        for (Vertex vertex : vertices) {
//                            vertex.features.add(cycle);
//                        }
                        features.add(e.to);
                        e.to.features.add(this);
                    }
                    top = Math.min(top, ntop);
                } else if (e.to.state == 1 && e.id != eid) {
                    List<Vertex> vertices = new ArrayList<>();
                    for (int k = stack.size() - 1; stack.get(k) != e.to; k--) {
                        vertices.add(stack.get(k));
                        top = Math.min(top, k);
                    }
                    vertices.add(e.to);

                    Cycle cycle = new Cycle(vertices);
                    for (Vertex vertex : vertices) {
                        vertex.features.add(cycle);
                    }
                }
            }
            stack.remove(stack.size() - 1);
            state = 2;
            return top;
        }

        public Hash hash(Feature up) {
            if (state == 2) {
                for (Feature feature : features) {
                    if (feature != up) {
                        update(feature);
                    }
                }
                if (up == null) {
                    state = 3;
                }
                missing = up;
            } else if (state == 3) {
                update(missing);
            }
            state++;

            Number vars;
            long lh = h;
            if (up != null && missing != up) {
                Hash hash = cachedHash(up);
//                lh = (lh - hash.hash) ^ hash.hash;
                lh -= hash.hash;
                int c = counts.get(hash.hash);
                vars = variants.divide(c).divide(hash.variants.value);
            } else {
                vars = new Number(variants.value);
            }
            return new Hash(magic(lh), vars);
        }

        private void update(Feature feature) {
            Hash hash = cachedHash(feature);
            Integer count = counts.get(hash.hash);
            int c = (count == null ? 0 : count) + 1;
            counts.put(hash.hash, c);
            variants.multiply(hash.variants);
            variants.multiply(c);
//            h = (h ^ hash.hash) + hash.hash;
            h += hash.hash;
        }
    }

    private long magic(long hash) {
        return /*HASH_A + hash * HASH_X +*/ hash ^ HASH_X; // + (hash & HASH_X);
    }

    abstract static class Feature {
        abstract Hash hash(Feature up);
    }

    long[] hashPowers;
    long[] inversePowers;

    void run() throws Exception {
        int n = in.nextInt();

        hashPowers = new long[n + 1];
        inversePowers = new long[n + 1];
        hashPowers[0] = 1;
        inversePowers[0] = 1;
        for (int i = 1; i <= n; i++) {
            hashPowers[i] = hashPowers[i - 1] * HASH_X;
            inversePowers[i] = inversePowers[i - 1] * HASH_INV_X;
        }

        List<Vertex> vertices = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            vertices.add(new Vertex());
        }

        int id = 0;
        int m = in.nextInt();
        for (int i = 0; i < m; i++) {
            int k = in.nextInt();
            Vertex prev = vertices.get(in.nextInt() - 1);
            for (int j = 1; j < k; j++) {
                Vertex next = vertices.get(in.nextInt() - 1);
                prev.edges.add(new Edge(id, next));
                next.edges.add(new Edge(id, prev));
                id++;
                prev = next;
            }
        }

        vertices.get(0).dfs(new ArrayList<Vertex>(), -1);

        Hash hash = vertices.get(0).hash(null);
        Number variants = hash.variants;
        int c = 0;
        for (Vertex vertice : vertices) {
            if (hash.hash == vertice.hash(null).hash) {
                c++;
            }
        }
        variants.multiply(c);

        out.println(variants.value);
    }


    static class Scanner {
        final BufferedReader reader;
        StringTokenizer tokenizer = new StringTokenizer("");

        public Scanner(File file) throws Exception {
            reader = new BufferedReader(new FileReader(file));
        }

        public int nextInt() throws Exception {
            while (!tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return Integer.parseInt(tokenizer.nextToken());
        }

        public void close() {
        }
    }

    public static void main(String[] args) throws Exception {
        in = new Scanner(new File("cactus.in"));
        out = new PrintWriter("cactus.out");

        new cactus_gk_mod().run();

        in.close();
        out.close();
    }
}
