import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B

main :: IO ()
main = readLine >>= main'
    where
        main' [n, m] = readContents m >>= main'' n m
        main'' n m kss = readLine >>= print . solve n m kss

solve :: Int -> Int -> [[Int]] -> [Int] -> Int
solve n m kss ps = length $ filter ff cnds
    where
        cnds = replicateM n [0, 1] 
        vs = zip ps $ map tail kss

        ff xs = all check vs
            where
                check (p, ss) = (p == ) . count 0 ss $ zip [1..] xs
                count acc (s : ts) ((i, y) : ys)
                    | s == i = count (acc + y) ts ys
                    | otherwise = count acc (s : ts) ys
                count acc _ _ = acc `mod` 2 

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine