#include <bits/stdc++.h>
using namespace std;
const int K = 55, N = 201000;
const int mod = 1000000007;
vector<int> conc[N], lp, rp;
vector<pair<int, int> > e[K];
long long bi[K][K], pe[K][K], fac[N];
int x[K], y[K], w[K], vis[N], cis[N];
int n, k, c, f[N];
int l, r, ans, a[K][K];
vector<int> dp[K][K];
map<pair<int, int>, int> hs;
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
inline void upd(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
struct info {
  int l, r;
  int dp[K];
  info() {}
  info(int _l, int _r) {
    l = _l;
    r = _r;
    memset(dp, 0, sizeof(dp));
  }
  void _swap() { swap(l, r); }
};
info ap, cp[K], pd[K][2];
info operator*(const info &a, const info &b) {
  info c(a.l + b.l, a.r + b.r);
  for (int j = 0; j <= a.l && j <= a.r; j++)
    for (int k = 0; k <= b.l && k <= b.r; k++)
      for (int pl = 0; pl + k <= b.l && pl + j <= a.r; pl++)
        for (int pr = 0; pr + k <= b.r && pr + j <= a.l; pr++) {
          upd(c.dp[j + k + pl + pr],
              1ll * a.dp[j] * b.dp[k] % mod * bi[b.l - k][pl] % mod *
                  pe[a.r - j][pl] % mod * bi[b.r - k][pr] % mod *
                  pe[a.l - j][pr] % mod);
        }
  return c;
}
info mul(const info &a, const long long &b) {
  info c(a.l, a.r + 1);
  for (int j = 0; j <= a.l && j <= a.r; j++)
    c.dp[j + 1] = a.dp[j] * (b + a.l - 1 - j) % mod;
  return c;
}
info mul(const info &a) {
  info c(a.l, a.r + 1);
  for (int j = 0; j <= a.l && j <= a.r; j++)
    c.dp[j + 1] = 1ll * a.dp[j] * (a.l - j) % mod;
  return c;
}
info operator+(const info &a, const info &b) {
  info c(max(a.l, b.l), max(a.r, b.r));
  for (int j = 0; j <= c.l && j <= c.r; j++)
    c.dp[j] = (a.dp[j] + b.dp[j]) % mod;
  return c;
}
void upd(int &a, long long b) { a = (a + b) % mod; }
int pos[K], p[K], od[K], id[K][K];
long long cost[K];
vector<int> _e[K], pnt[K];
long long calc(int t = 0) {
  for (int i = 0; i < l + r; i++) pos[p[i]] = i, cost[i] = 1;
  for (int i = 0; i < l + r; i++) {
    int ms = i;
    for (int j = 0; j < ((int)(_e[p[i]]).size()); j++)
      ms = max(ms, pos[_e[p[i]][j]]);
    for (int j = i; j < ms + 1; j++) cost[j] *= 2;
  }
  long long ret = 0;
  for (int i = 0; i < l + r; i++) ret += cost[i];
  return ret;
}
void solve() {
  for (int i = 0; i < l + r; i++) _e[i].clear();
  for (int i = 0; i < l; i++)
    for (int j = 0; j < r; j++)
      if (a[i][j] != 1) _e[i].push_back(j + l), _e[j + l].push_back(i);
  for (int i = 0; i < l + r; i++) p[i] = i, od[i] = i;
  long long ans = calc();
  for (int k = 0; k < 500 * (l + r); k++) {
    int u = rand() % (l + r), v = rand() % (l + r);
    for (int i = 0; i < l + r; i++) p[i] = od[i];
    swap(p[u], p[v]);
    long long c = calc();
    if (c < ans) {
      ans = c;
      for (int i = 0; i < l + r; i++) od[i] = p[i];
    }
  }
  int cl = 0, cr = 0;
  for (int i = 0; i < l + r; i++) p[i] = od[i], pnt[i].clear();
  ;
  for (int i = 0; i < l + r; i++) pos[p[i]] = i;
  for (int i = 0; i < l + r; i++)
    for (int j = 0; j < l + r; j++) id[i][j] = -1;
  for (int i = 0; i < l + r; i++) {
    int ms = i;
    for (int j = 0; j < ((int)(_e[p[i]]).size()); j++)
      ms = max(ms, pos[_e[p[i]][j]]);
    for (int j = i; j < ms + 1; j++) pnt[j].push_back(i);
  }
  for (int i = 0; i < l + r; i++)
    for (int j = 0; j < ((int)(pnt[i]).size()); j++) id[i][pnt[i][j]] = j;
  dp[0][0].clear();
  dp[0][0].resize(2);
  dp[0][0][0] = 1;
  if (p[0] < l)
    cl++;
  else
    cr++;
  for (int i = 1; i < l + r; i++) {
    int u = p[i];
    for (int k = 0; k < min(cl, cr) + 2; k++) {
      dp[i][k].clear();
      dp[i][k].resize((1 << (((int)(pnt[i]).size()))));
    }
    for (int oS = 0; oS < (1 << (((int)(pnt[i - 1]).size()))); oS++) {
      int S = 0, pl = cl, pr = cr;
      for (int w = 0; w < ((int)(pnt[i - 1]).size()); w++)
        if (oS & (1 << (w))) {
          int ps = id[i][pnt[i - 1][w]];
          if (ps != -1) S |= 1 << ps;
        }
      for (int w = 0; w < ((int)(pnt[i]).size()) - 1; w++)
        if (!(S & (1 << (w)))) {
          int v = p[pnt[i][w]];
          if (v < l)
            pl--;
          else
            pr--;
        }
      for (int k = 0; k < min(cl, cr) + 1; k++)
        if (dp[i - 1][k][oS]) {
          long long ret = dp[i - 1][k][oS];
          upd(dp[i][k][S], ret);
          for (int w = 0; w < ((int)(pnt[i]).size()) - 1; w++)
            if (!(S & (1 << (w)))) {
              int v = p[pnt[i][w]];
              if (((u < l) ^ (v < l)) == 0) continue;
              upd(dp[i][k + 1]
                    [S | (1 << (((int)(pnt[i]).size()) - 1)) | (1 << (w))],
                  ret * ((u < l) ? a[u][v - l] : a[v][u - l]));
            }
          if (u < l)
            upd(dp[i][k + 1][S | (1 << (((int)(pnt[i]).size()) - 1))],
                ret * (pr - k));
          else
            upd(dp[i][k + 1][S | (1 << (((int)(pnt[i]).size()) - 1))],
                ret * (pl - k));
        }
    }
    if (p[i] < l)
      cl++;
    else
      cr++;
  }
  for (int j = 0; j < min(cl, cr) + 1; j++)
    for (int k = 0; k < (1 << (((int)(pnt[l + r - 1]).size()))); k++)
      upd(cp[c].dp[j], dp[l + r - 1][j][k]);
}
long long perm(int a, int b) {
  long long s = 1;
  for (int j = 1; j < b + 1; j++) s = s * (a + 1 - j) % mod;
  return s;
}
void init() {
  for (int i = 0; i < k + 1; i++) {
    bi[i][0] = bi[i][i] = 1;
    for (int j = 1; j < i; j++)
      bi[i][j] = (bi[i - 1][j - 1] + bi[i - 1][j]) % mod;
    pe[i][0] = 1;
    for (int j = 1; j < i + 1; j++) pe[i][j] = pe[i][j - 1] * (i + 1 - j) % mod;
  }
  fac[0] = 1;
  for (int i = 1; i < n + 1; i++) fac[i] = fac[i - 1] * i % mod;
}
void read() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < 2 * n; i++) f[i] = i;
  for (int i = 0; i < k; i++) {
    scanf("%d%d%d", x + i, y + i, w + i);
    if (w[i] == 1) {
      --i;
      --k;
      continue;
    }
    --x[i];
    --y[i];
    hs[make_pair(x[i], y[i])] = w[i];
    y[i] += n;
    f[find(x[i])] = find(y[i]);
  }
  for (int i = 0; i < 2 * n; i++) conc[find(i)].push_back(i);
}
void step1() {
  c = 0;
  for (int i = 0; i < 2 * n; i++)
    if (((int)(conc[i]).size()) > 1) {
      lp.clear();
      rp.clear();
      for (int j = 0; j < ((int)(conc[i]).size()); j++) {
        int u = conc[i][j];
        if (u < n)
          lp.push_back(u);
        else
          rp.push_back(u - n);
      }
      cp[c] = info(((int)(lp).size()), ((int)(rp).size()));
      for (int j = 0; j < ((int)(lp).size()); j++)
        for (int k = 0; k < ((int)(rp).size()); k++)
          if (hs.count(make_pair(lp[j], rp[k])))
            a[j][k] = hs[make_pair(lp[j], rp[k])];
          else
            a[j][k] = 1;
      l = ((int)(lp).size());
      r = ((int)(rp).size());
      solve();
      ++c;
    }
}
void step2() {
  ap = info(0, 0);
  ap.dp[0] = 1;
  for (int i = 0; i < c; i++) ap = ap * cp[i];
  for (int j = 0; j <= ap.l && j <= ap.r; j++) {
    if (ap.l + ap.r - j > n) continue;
    upd(ans, ap.dp[j] * perm(n - ap.r, ap.l - j) % mod *
                 perm(n - ap.l, ap.r - j) % mod * fac[n - ap.r - ap.l + j] %
                 mod);
  }
  printf("%d\n", ans);
}
int main() {
  read();
  init();
  step1();
  step2();
}
