You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.

You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to:

  * (i - 1, j + 1) — only if i > 1, 
  * (i, j + 1), or 
  * (i + 1, j + 1) — only if i < 3. 



However, there are n obstacles blocking your path. k-th obstacle is denoted by three integers ak, lk and rk, and it forbids entering any cell (ak, j) such that lk ≤ j ≤ rk.

You have to calculate the number of different paths from (2, 1) to (2, m), and print it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 104, 3 ≤ m ≤ 1018) — the number of obstacles and the number of columns in the matrix, respectively.

Then n lines follow, each containing three integers ak, lk and rk (1 ≤ ak ≤ 3, 2 ≤ lk ≤ rk ≤ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk ≤ j ≤ rk. Some cells may be blocked by multiple obstacles.

Output

Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0.

Example

Input

2 5
1 3 4
2 2 3


Output

2
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, m, a, l, r, id, ina, inb, inc, cna, cnb, cnc, now, tmpa, tmpb,
    tmpc;
struct data {
  long long r, p, ty;
} seg[20004];
struct matrix {
  long long mat[4][4];
};
bool cmp(data A, data B) {
  if (A.p != B.p)
    return A.p < B.p;
  else
    return A.ty < B.ty;
}
matrix multiply(matrix a, matrix b) {
  matrix c;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      c.mat[i][j] = 0;
      for (int k = 1; k <= 3; k++) {
        c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % mod;
      }
    }
  }
  return c;
}
matrix expo(long long wh, long long p) {
  matrix ret;
  ret.mat[1][1] = 1;
  ret.mat[1][2] = 0;
  ret.mat[1][3] = 0;
  ret.mat[2][1] = 0;
  ret.mat[2][2] = 1;
  ret.mat[2][3] = 0;
  ret.mat[3][1] = 0;
  ret.mat[3][2] = 0;
  ret.mat[3][3] = 1;
  matrix base;
  if (wh == 4) {
    base.mat[1][1] = 1;
    base.mat[1][2] = 1;
    base.mat[1][3] = 0;
    base.mat[2][1] = 1;
    base.mat[2][2] = 1;
    base.mat[2][3] = 0;
    base.mat[3][1] = 0;
    base.mat[3][2] = 0;
    base.mat[3][3] = 0;
  }
  if (wh == 6) {
    base.mat[1][1] = 0;
    base.mat[1][2] = 0;
    base.mat[1][3] = 0;
    base.mat[2][1] = 0;
    base.mat[2][2] = 1;
    base.mat[2][3] = 1;
    base.mat[3][1] = 0;
    base.mat[3][2] = 1;
    base.mat[3][3] = 1;
  }
  if (wh == 7) {
    base.mat[1][1] = 1;
    base.mat[1][2] = 1;
    base.mat[1][3] = 0;
    base.mat[2][1] = 1;
    base.mat[2][2] = 1;
    base.mat[2][3] = 1;
    base.mat[3][1] = 0;
    base.mat[3][2] = 1;
    base.mat[3][3] = 1;
  }
  while (p) {
    if (p & 1) ret = multiply(base, ret);
    base = multiply(base, base);
    p /= 2;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a >> l >> r;
    id++;
    seg[id].r = a;
    seg[id].p = l;
    seg[id].ty = 0;
    id++;
    seg[id].r = a;
    seg[id].p = r;
    seg[id].ty = 1;
  }
  id++;
  seg[id].p = m;
  sort(seg + 1, seg + id + 1, cmp);
  ina = 0;
  inb = 1;
  inc = 0;
  now = 1;
  for (int i = 1; i <= id; i++) {
    matrix ans;
    if (!cna && !cnb && cnc)
      ans = expo(4LL, seg[i].p - now);
    else if (cna && !cnb && !cnc)
      ans = expo(6LL, seg[i].p - now);
    else if (!cna && !cnb && !cnc)
      ans = expo(7LL, seg[i].p - now);
    else {
      now = seg[i].p;
      if (seg[i].r == 1) {
        if (seg[i].ty == 0)
          cna++;
        else
          cna--;
      }
      if (seg[i].r == 2) {
        if (seg[i].ty == 0)
          cnb++;
        else
          cnb--;
      }
      if (seg[i].r == 3) {
        if (seg[i].ty == 0)
          cnc++;
        else
          cnc--;
      }
      if (cna) ina = 0LL;
      if (cnb) inb = 0LL;
      if (cnc) inc = 0LL;
      continue;
    }
    tmpa =
        (ans.mat[1][1] * ina + ans.mat[1][2] * inb + ans.mat[1][3] * inc) % mod;
    tmpb =
        (ans.mat[2][1] * ina + ans.mat[2][2] * inb + ans.mat[2][3] * inc) % mod;
    tmpc =
        (ans.mat[3][1] * ina + ans.mat[3][2] * inb + ans.mat[3][3] * inc) % mod;
    ina = tmpa;
    inb = tmpb;
    inc = tmpc;
    now = seg[i].p;
    if (seg[i].r == 1) {
      if (seg[i].ty == 0)
        cna++;
      else
        cna--;
    }
    if (seg[i].r == 2) {
      if (seg[i].ty == 0)
        cnb++;
      else
        cnb--;
    }
    if (seg[i].r == 3) {
      if (seg[i].ty == 0)
        cnc++;
      else
        cnc--;
    }
    if (cna) ina = 0LL;
    if (cnb) inb = 0LL;
    if (cnc) inc = 0LL;
  }
  cout << inb << endl;
  return 0;
}
