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;
struct event {
  long long time;
  long long type;
  long long row;
};
bool operator<(const event &a, const event &b) {
  if (a.time == b.time) return a.type < b.type;
  return a.time < b.time;
}
struct mat {
  long long a[3][3];
  mat() { memset(a, 0, sizeof(a)); }
  mat(long long a00, long long a01, long long a02, long long a10, long long a11,
      long long a12, long long a20, long long a21, long long a22) {
    a[0][0] = a00 % 1000000007;
    a[0][1] = a01 % 1000000007;
    a[0][2] = a02 % 1000000007;
    a[1][0] = a10 % 1000000007;
    a[1][1] = a11 % 1000000007;
    a[1][2] = a12 % 1000000007;
    a[2][0] = a20 % 1000000007;
    a[2][1] = a21 % 1000000007;
    a[2][2] = a22 % 1000000007;
  }
};
mat mult(const mat &a, const mat &b) {
  mat c;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        c.a[i][j] += a.a[i][k] * b.a[k][j];
        c.a[i][j] %= 1000000007;
      }
    }
  }
  return c;
}
mat exp(const mat &a, long long e) {
  mat c;
  if (e == 0) {
    c.a[0][0] = 1;
    c.a[1][1] = 1;
    c.a[2][2] = 1;
    return c;
  }
  if (e == 1) {
    return a;
  }
  if (e & 1) {
    return mult(exp(a, e - 1), a);
  }
  c = exp(a, e / 2);
  return mult(c, c);
}
long long n, m;
mat m000, m100, m010, m001, m110, m101, m011, m111;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  m000 = mat(1, 1, 0, 1, 1, 1, 0, 1, 1);
  m100 = mat(0, 0, 0, 1, 1, 1, 0, 1, 1);
  m010 = mat(1, 1, 0, 0, 0, 0, 0, 1, 1);
  m001 = mat(1, 1, 0, 1, 1, 1, 0, 0, 0);
  m110 = mat(0, 0, 0, 0, 0, 0, 0, 1, 1);
  m101 = mat(0, 0, 0, 1, 1, 1, 0, 0, 0);
  m011 = mat(1, 1, 0, 0, 0, 0, 0, 0, 0);
  m111 = mat(0, 0, 0, 0, 0, 0, 0, 0, 0);
  cin >> n >> m;
  vector<event> events;
  for (int i = 0; i < n; i++) {
    long long a, l, r;
    cin >> a >> l >> r;
    a--;
    events.push_back(event{l, 0, a});
    events.push_back(event{r + 1, 1, a});
  }
  sort(events.begin(), events.end());
  long long last = 1;
  long long a[3] = {0, 0, 0};
  long long x[3] = {0, 1, 0};
  long long y[3];
  mat cur = m000;
  for (int i = 0; i < 2 * n; i++) {
    bool doAction = false;
    if (i + 1 < 2 * n && events[i + 1].time == events[i].time) {
    } else {
      doAction = true;
    }
    if (i == 2 * n - 1) doAction = true;
    if (events[i].type == 0) {
      a[events[i].row]++;
    } else if (events[i].type == 1) {
      a[events[i].row]--;
    }
    if (doAction) {
      long long diff = events[i].time - last;
      long long e = diff - 1;
      mat p = exp(cur, e);
      y[0] =
          (p.a[0][0] * x[0] + p.a[0][1] * x[1] + p.a[0][2] * x[2]) % 1000000007;
      y[1] =
          (p.a[1][0] * x[0] + p.a[1][1] * x[1] + p.a[1][2] * x[2]) % 1000000007;
      y[2] =
          (p.a[2][0] * x[0] + p.a[2][1] * x[1] + p.a[2][2] * x[2]) % 1000000007;
      x[0] = y[0];
      x[1] = y[1];
      x[2] = y[2];
      int val = (a[0] >= 1) * 4 + (a[1] >= 1) * 2 + (a[2] >= 1);
      if (val == 0) {
        cur = m000;
      } else if (val == 1) {
        cur = m001;
      } else if (val == 2) {
        cur = m010;
      } else if (val == 3) {
        cur = m011;
      } else if (val == 4) {
        cur = m100;
      } else if (val == 5) {
        cur = m101;
      } else if (val == 6) {
        cur = m110;
      } else if (val == 7) {
        cur = m111;
      } else {
      }
      p = cur;
      y[0] =
          (p.a[0][0] * x[0] + p.a[0][1] * x[1] + p.a[0][2] * x[2]) % 1000000007;
      y[1] =
          (p.a[1][0] * x[0] + p.a[1][1] * x[1] + p.a[1][2] * x[2]) % 1000000007;
      y[2] =
          (p.a[2][0] * x[0] + p.a[2][1] * x[1] + p.a[2][2] * x[2]) % 1000000007;
      x[0] = y[0];
      x[1] = y[1];
      x[2] = y[2];
      last = events[i].time;
    }
  }
  long long diff = m - last;
  mat p = exp(m000, diff);
  y[0] = (p.a[0][0] * x[0] + p.a[0][1] * x[1] + p.a[0][2] * x[2]) % 1000000007;
  y[1] = (p.a[1][0] * x[0] + p.a[1][1] * x[1] + p.a[1][2] * x[2]) % 1000000007;
  y[2] = (p.a[2][0] * x[0] + p.a[2][1] * x[1] + p.a[2][2] * x[2]) % 1000000007;
  x[0] = y[0];
  x[1] = y[1];
  x[2] = y[2];
  cout << x[1] << endl;
  return 0;
}
