import sys input = sys.stdin.readline mod=10**9+7 K,M,N=map(int,input().split()) CODE=[tuple(map(int,input().split())) for i in range(M)] S_CODE=set(CODE) # 行列の計算(numpyを使えないとき,modを使用) def prod(A,B,k,l,m):# A:k*l,B:l*m C=[[0 for i in range(m)] for j in range(k)] for i in range(k): for j in range(m): ANS=0 for pl in range(l): ANS=(ANS+A[i][pl]*B[pl][j])%mod C[i][j]=ANS return C def plus(A,B,k,l):# a,B:k*l C=[[0 for i in range(l)] for j in range(k)] for i in range(k): for j in range(l): C[i][j]=(A[i][j]+B[i][j])%mod return C A=[[0]*(K*K) for i in range(K*K)] for i in range(K*K): a,b=divmod(i,K) a+=1 b+=1 for j in range(K*K): c,d=divmod(j,K) c+=1 d+=1 if b==c and (a,b,d) in S_CODE: A[i][j]+=1 POWA=[A] for i in range(60): POWA.append(prod(POWA[-1],POWA[-1],K*K,K*K,K*K)) # ベキを求めて X=[[0]*(K*K)] for i in range(K): X[0][i]=1 #print(X) n=N-2 while n: X=prod(X,POWA[n.bit_length()-1],1,K*K,K*K) # n乗の場合 n-=1<<(n.bit_length()-1) ANS=0 for i in range(K*K): a,b=divmod(i,K) b+=1 if b==1: ANS+=X[0][i] print(ANS%mod)