CSES - Shared codeLink to this code:
https://cses.fi/paste/1a082c285b3dd58a697fda/
#include<cstdio>
long long dp[1<<20][20];
int edges[20][20];
int two[21];
int main(){
int n,m,len,a,b,num;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&a,&b);
a--,b--;
edges[a][b]++;
}
two[0]=1;
for(int i=1;i<=20;i++)two[i]=two[i-1]<<1;
len=two[n];
long long mod=1e9+7;
dp[1][0]=1;
for(int i=1,j,k;i<len;i++){
if(i!=(1<<n)-1&&((i>>(n-1))&1))continue;
for(j=0;j<n;j++){
if(i&two[j]){
num=i^two[j];
for( k=0;k<n;k++){
if(edges[k][j] and num&two[k])
dp[i][j]=(dp[i][j]+edges[k][j]*dp[num][k])%mod;
}
}
}
}
printf("%lld",dp[len-1][n-1]);
}