#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define BIT(i) (1LL << (i))
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;

const ll sz = 18;
ll N, M;
ll dp[BIT(sz)];
vector<pll> C[sz + 1];

bool check(ll bit){
    vector<ll> acc(N, 0);
    REP(i, N){
        if(bit & BIT(i)) acc[i]++;
    }
    ll j = accumulate(ALL(acc), 0LL);
    REP(i, N - 1) acc[i + 1] += acc[i];
    for(auto [y, z] : C[j]){
        if(z < acc[y]) return false;
    }
    return true;
}

signed main(){
    cin >> N >> M;
    ll X, Y, Z;
    REP(i, M){
        cin >> X >> Y >> Z;
        Y--;
        C[X].push_back({Y, Z});
    }
    dp[0] = 1;
    REP(i, BIT(N)){
        REP(j, N){
            if(i & BIT(j)) continue;
            if(check(i | BIT(j))){
                dp[i | BIT(j)] += dp[i];
            }
        }
    }
    cout << dp[BIT(N) - 1] << endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;

int n,m,k,tot,cas,ans;
int a[maxl];ll fac[maxl];
int cnt[maxl][20];
ll dp[20][maxl];
int x[maxl],y[maxl],z[maxl];
bool vis[maxl];
char s[maxl];

inline void prework()
{
	fac[0]=1;
	for(int i=1;i<=18;i++)
		fac[i]=fac[i-1]*i;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d%d",&x[i],&y[i],&z[i]);
	for(int s=1;s<(1<<n);s++)
	{
		for(int i=1;i<=n;i++)
		{
			if(s>>(i-1)&1)
				cnt[s][i]=1;
			cnt[s][i]+=cnt[s][i-1];
		}
	}
}

inline bool jug(int ind,int s)
{
	for(int i=1;i<=m;i++)
	if(x[i]==ind && cnt[s][y[i]]>z[i])
		return false;
	return true;
}


inline void mainwork()
{
	dp[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int s=1;s<(1<<n);s++)
		if(cnt[s][n]==i && jug(i,s))
		{
			for(int j=1;j<=n;j++)
			if(s>>(j-1)&1)
				dp[i][s]+=dp[i-1][s^(1<<(j-1))];
		}
}

inline void print()
{
	printf("%lld",dp[n][(1<<n)-1]);	
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}