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 maxn = 20010, mod = 1e9 + 7;
long long n, cnt, vis[4], now, pos;
long long m;
struct node {
  long long type, id, pos;
} e[maxn];
bool cmp(node a, node b) { return a.pos < b.pos; }
struct Mat {
  long long a[4][4];
  void clear() { memset(a, 0, sizeof(a)); }
} p[10], zhuan[10], t, ans;
Mat operator*(Mat a, Mat b) {
  Mat c;
  c.clear();
  for (long long i = 1; i <= 3; i++)
    for (long long j = 1; j <= 3; j++)
      for (long long k = 1; k <= 3; k++)
        c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j] % mod) % mod;
  return c;
}
void init() {
  p[1].clear();
  p[1].a[1][1] = p[1].a[1][2] = p[1].a[2][1] = p[1].a[2][2] = p[1].a[2][3] =
      p[1].a[3][2] = p[1].a[3][3] = 1;
  p[2].clear();
  p[2].a[2][2] = p[2].a[2][3] = p[2].a[3][2] = p[2].a[3][3] = 1;
  p[3].clear();
  p[3].a[1][1] = p[3].a[3][3] = 1;
  p[4].clear();
  p[4].a[1][1] = p[4].a[1][2] = p[4].a[2][1] = p[4].a[2][2] = 1;
  p[5].clear();
  p[5].a[3][3] = 1;
  p[6].clear();
  p[6].a[2][2] = 1;
  p[7].clear();
  p[7].a[1][1] = 1;
  p[8].clear();
  zhuan[1].clear();
  zhuan[1].a[1][1] = zhuan[1].a[2][1] = zhuan[1].a[1][2] = zhuan[1].a[2][2] =
      zhuan[1].a[3][2] = zhuan[1].a[2][3] = zhuan[1].a[3][3] = 1;
  zhuan[2].clear();
  zhuan[2].a[1][2] = zhuan[2].a[2][2] = zhuan[2].a[3][2] = zhuan[2].a[2][3] =
      zhuan[2].a[3][3] = 1;
  zhuan[3].clear();
  zhuan[3].a[1][1] = zhuan[3].a[2][1] = zhuan[3].a[2][3] = zhuan[3].a[3][3] = 1;
  zhuan[4].clear();
  zhuan[4].a[1][1] = zhuan[4].a[2][1] = zhuan[4].a[1][2] = zhuan[4].a[2][2] =
      zhuan[4].a[3][2] = 1;
  zhuan[5].clear();
  zhuan[5].a[2][3] = zhuan[5].a[3][3] = 1;
  zhuan[6].clear();
  zhuan[6].a[1][2] = zhuan[6].a[2][2] = zhuan[6].a[3][2] = 1;
  zhuan[7].clear();
  zhuan[7].a[1][1] = zhuan[7].a[2][1] = 1;
  zhuan[8].clear();
  t.clear();
  for (long long i = 1; i <= 3; i++) t.a[i][i] = 1;
  ans.clear();
  ans.a[1][2] = 1;
}
long long get() {
  if (!vis[1] && !vis[2] && !vis[3]) return 1;
  if (vis[1] && !vis[2] && !vis[3]) return 2;
  if (!vis[1] && vis[2] && !vis[3]) return 3;
  if (!vis[1] && !vis[2] && vis[3]) return 4;
  if (vis[1] && vis[2] && !vis[3]) return 5;
  if (vis[1] && !vis[2] && vis[3]) return 6;
  if (!vis[1] && vis[2] && vis[3]) return 7;
  if (vis[1] && vis[2] && vis[3]) return 8;
}
Mat qpow(long long x, long long b) {
  Mat res;
  res.clear();
  for (long long i = 1; i <= 3; i++) res.a[i][i] = 1;
  Mat a = p[x];
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    long long a, l, r;
    scanf("%lld%lld%lld", &a, &l, &r);
    e[++cnt].type = 1;
    e[cnt].id = a;
    e[cnt].pos = l;
    e[++cnt].type = -1;
    e[cnt].id = a;
    e[cnt].pos = r + 1;
  }
  sort(e + 1, e + 1 + cnt, cmp);
  init();
  pos = 1;
  int cur;
  for (int i = 1; i <= cnt; i++) {
    if (e[i].pos == 1)
      vis[e[i].id]++;
    else {
      cur = i;
      break;
    }
  }
  long long j;
  now = get();
  for (long long i = cur; i <= cnt; i = j) {
    j = i;
    while (j <= cnt && e[j].pos == e[i].pos) j++;
    for (long long k = i; k < j; k++) {
      if (e[k].type == 1)
        vis[e[k].id]++;
      else
        vis[e[k].id]--;
    }
    long long temp = get();
    t = t * qpow(now, min(m, e[i].pos) - pos - 1);
    t = t * zhuan[temp];
    pos = e[i].pos;
    now = temp;
  }
  if (pos != n) t = t * qpow(now, m - pos);
  ans = ans * t;
  printf("%lld\n", ans.a[1][2]);
  return 0;
}
