import Data.List
import Data.Maybe
import Control.Monad

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  xs <- replicateM n (map read . words <$> getLine) :: IO [[Int]]
  let ps = paths n (xs ++ map reverse xs)
  print $ trav 1 ps [1..n]

paths :: Int -> [[Int]] -> [(Int,[Int])]
paths 0 xs = []
paths n xs = 
  let 
    f = filter (\[a,_] -> a==n) xs
  in
    if null f 
       then paths (n-1) xs 
       else (n, (last $ transpose f)) : (paths (n-1) xs)

trav :: Int -> [(Int,[Int])] ->  [Int] -> Int
trav current paths dests
  | isNothing $ find (==current) dests = 0
  | otherwise = 
    let 
      rest = delete current dests
      f = filter (\(a,_) -> a==current) paths
    in
      if null rest
      then 1
      else if null f
      then 0
      else sum $ [x | d<-snd $ head f, let x = trav d paths rest]     