#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int n,m,q;
int sz;
int dp[maxn][maxn];
int up[maxn][maxn];
int ll[maxn];
int rl[maxn];
int qx[maxn];
int qy[maxn];
int ans[maxn];
char mp[maxn][maxn];

struct node{
	int fa[maxn];
	int find(int x)
	{
		if(fa[x] == x)return x;
		else return fa[x] = find(fa[x]);
	}
}l[maxn],r[maxn];

void del(int x,int y)
{
	l[x].fa[y] = l[x].find(y - 1);
	r[x].fa[y] = r[x].find(y + 1);
}

int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i = 1; i <= n; i++)
		scanf("%s", mp[i]+1);
	for(int i = 1; i <= q; i++)
	{
		scanf("%d%d",&qx[i],&qy[i]);
		mp[qx[i]][qy[i]] = 'X';
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m+1; j++)
		{
			l[i].fa[j] = j;
			r[i].fa[j] = j;
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(mp[i][j] == '.')
				del(i, j);
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(mp[i][j] == '.')
			{
				dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j],dp[i][j-1]))+1;
				if(dp[i][j] > sz) sz = dp[i][j];
			}
		}
	}
	for(int v = q; v; v--)
	{
		ans[v] = sz;
		del(qx[v], qy[v]);
		for(int i = 1; i <= n; i++)
		{
			ll[i] = qy[v] - l[i].find(qy[v]);
			rl[i] = r[i].find(qy[v]) - qy[v];
		}
		for(int i = qx[v] + 1; i <= n; i++)
		{
			ll[i] = min(ll[i], ll[i - 1]);
			rl[i] = min(rl[i], rl[i - 1]);
		}
		for(int i = qx[v] - 1; i; i--)
		{
			ll[i] = min(ll[i], ll[i + 1]);
			rl[i] = min(rl[i], rl[i + 1]);
		}
		for(int i = 1; i <= qx[v]; i++)
			while(min(rl[i], rl[i + sz]) + min(ll[i], ll[i + sz]) - 1 > sz)
				sz++;
	}
	for(int i = 1; i <= q; i ++)printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 10;
int n,m,k,Ans;
int l1[MAXN],r1[MAXN],qx[MAXN],qy[MAXN],ans[MAXN],dp[MAXN][MAXN];
char Map[MAXN][MAXN];

struct node
{
	int fa[MAXN];
	int findSet (int x)
	{
		if (x == fa[x]) return x;
		else return fa[x] = findSet (fa[x]);
	}
}l[MAXN],r[MAXN];

void Delete(int x,int y)
{
	l[x].fa[y] = l[x].findSet(y - 1);
	r[x].fa[y] = r[x].findSet(y + 1);
}

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i = 1; i <= n; i++)
		scanf("%s", Map[i]+1);
	for(int i = 1; i <= k; i++)
	{
		scanf("%d%d",&qx[i],&qy[i]);
		Map[qx[i]][qy[i]] = 'X';
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m+1; j++)
		{
			l[i].fa[j] = j;
			r[i].fa[j] = j;
		}
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if(Map[i][j] == '.')
				Delete(i, j);
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if(Map[i][j] == '.')
			{
				dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j],dp[i][j-1]))+1;
				Ans = max (Ans,dp[i][j]);
			}
	for (int j = k;j >= 1;-- j)
	{
		ans[j] = Ans;
		Delete (qx[j],qy[j]);
		for(int i = 1; i <= n; i++)
		{
			l1[i] = qy[j] - l[i].findSet(qy[j]);
			r1[i] = r[i].findSet(qy[j]) - qy[j];
		}
		for(int i = qx[j] + 1; i <= n; i++)
		{
			l1[i] = min(l1[i], l1[i - 1]);
			r1[i] = min(r1[i], r1[i - 1]);
		}
		for(int i = qx[j] - 1; i; i--)
		{
			l1[i] = min(l1[i], l1[i + 1]);
			r1[i] = min(r1[i], r1[i + 1]);
		}
		for(int i = 1; i <= qx[j]; i++)
			while(min(r1[i], r1[i + Ans]) + min(l1[i], l1[i + Ans]) - 1 > Ans)
				Ans++;
	}
	for(int i = 1; i <= k; i ++)
		printf("%d\n",ans[i]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXN 2005

using namespace std;

int n, m, q, sz, dp[MAXN][MAXN], up[MAXN][MAXN], ll[MAXN], rl[MAXN], qx[MAXN], qy[MAXN], ans[MAXN];
char map[MAXN][MAXN];

struct line{
	int fa[MAXN];
	int find(int x)
	{
		if(fa[x] == x) return x;
		return fa[x] = find(fa[x]);
	}
}l[MAXN], r[MAXN];

inline void del(int x, int y)
{
	l[x].fa[y] = l[x].find(y-1);
	r[x].fa[y] = r[x].find(y+1);
}

int main()
{
	scanf("%d%d%d", &n, &m, &q);
	for(int i = 1; i <= n; i++)
		scanf("%s", map[i]+1);
	for(int i = 1; i <= q; i++)
		scanf("%d%d", &qx[i], &qy[i]), map[qx[i]][qy[i]] = 'X';
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m+1; j++)
			up[i][j] = i, l[i].fa[j] = j, r[i].fa[j] = j;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if(map[i][j] == '.')
				up[i][j] = up[i-1][j], del(i, j);
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if(map[i][j] == '.')
			{
				dp[i][j] = min(dp[i-1][j-1]+1, min(i - up[i][j], j - l[i].find(j)));
				if(dp[i][j] > sz) sz = dp[i][j];
			}
	for(int v = q; v; v--)
	{
		ans[v] = sz;
		del(qx[v], qy[v]);
		for(int i = 1; i <= n; i++)
		{
			ll[i] = qy[v] - l[i].find(qy[v]);
			rl[i] = r[i].find(qy[v]) - qy[v];
		}
		for(int i = qx[v] + 1; i <= n; i++)
			ll[i] = min(ll[i], ll[i-1]), rl[i] = min(rl[i], rl[i-1]);
		for(int i = qx[v] - 1; i; i--)
			ll[i] = min(ll[i], ll[i+1]), rl[i] = min(rl[i], rl[i+1]);
		for(int i = 1; i <= qx[v]; i++)
			while(min(rl[i], rl[i+sz]) + min(ll[i], ll[i+sz]) - 1 > sz)
				sz++;
	}
	for(int i = 1; i <= q; i++)
		printf("%d\n", ans[i]);
	return 0;
}


#include <cstdio>
#include <cstring>

#define Int register int

const int MAX = 2000;
int n, m, k, x[MAX + 5], y[MAX + 5] = {}, Ans[MAX + 5] = {};
int UpL[MAX + 5][MAX + 5] = {}, DnL[MAX + 5][MAX + 5] = {};
int Head, Tail, Q[MAX + 5] = {}, MaxL[MAX + 5] = {};
int F[MAX + 5][MAX + 5] = {};
bool mp[MAX + 5][MAX + 5] = {};

inline int Min ( const int a, const int b ) { return a < b ? a : b; }

inline int Max ( const int a, const int b ) { return a > b ? a : b; }

inline void Prepare () {
	for ( Int i = 1; i <= n; ++ i ) {
		for ( Int j = 1; j <= m; ++ j ) {
			Ans[k] |= ! mp[i][j];
			UpL[i][j] = mp[i][j] ? 0 : UpL[i - 1][j] + 1;
			DnL[n - i + 1][j] = mp[n - i + 1][j] ? 0 : DnL[n - i + 2][j] + 1;
		}
	}
	for ( Int i = 1; i <= n; ++ i ) {
		for ( Int j = 1; j <= m; ++ j ) {
			F[i][j] = mp[i][j] ? 0 : Min ( F[i - 1][j - 1], Min ( F[i - 1][j], F[i][j - 1] ) ) + 1;
			Ans[k] = Max ( Ans[k], F[i][j] );
		}
	}
}

inline bool Check ( const int L, const int x ) {
	Head = 1, Tail = 0, memset ( MaxL, 0, sizeof MaxL );
	for ( Int i = 1; i <= m; ++ i ) {
		while ( Head <= Tail && UpL[x][Q[Tail]] >= UpL[x][i] ) -- Tail;
		Q[++ Tail] = i;
		while ( Q[Head] <= i - L ) ++ Head;
		MaxL[i] += UpL[x][Q[Head]];
	}
	Head = 1, Tail = 0;
	for ( Int i = 1; i <= m; ++ i ) {
		while ( Head <= Tail && DnL[x][Q[Tail]] >= DnL[x][i] ) -- Tail;
		Q[++ Tail] = i;
		while ( Q[Head] <= i - L ) ++ Head;
		MaxL[i] += DnL[x][Q[Head]];
	}
	for ( Int i = L; i <= m; ++ i ) {
		if ( MaxL[i] > L ) {
			return true;
		}
	}
	return false;
}

inline int BS ( const int K ) {
	int l = Ans[K], r = Min ( n, m ), mid;
	while ( l + 1 < r ) {
		mid = l + r >> 1;
		if ( Check ( mid, x[K] ) ) l = mid;
		else r = mid - 1;
	}
	return Check ( r, y[K] ) ? r : l;
}

inline void Work () {
	scanf ( "%d %d %d", &n, &m, &k );
	for ( Int i = 1; i <= n; ++ i ) {
		for ( Int j = 1; j <= m; ++ j ) {
			char s = getchar ();
			if ( s ^ '.' && s ^ 'X' ) { -- j; continue; }
			mp[i][j] = s == 'X';
		}
	}
	for ( Int i = 1; i <= k; ++ i ) scanf ( "%d %d", &x[i], &y[i] ), mp[x[i]][y[i]] = true;
	Prepare ();
	for ( Int t = k, l, r, mid; t; -- t ) {
		mp[x[t]][y[t]] = false;
		for ( Int i = 1; i <= n; ++ i ) {
			UpL[i][y[t]] = mp[i][y[t]] ? 0 : UpL[i - 1][y[t]] + 1;
			DnL[n - i + 1][y[t]] = mp[n - i + 1][y[t]] ? 0 : DnL[n - i + 2][y[t]] + 1;
		}

		Ans[t - 1] = BS ( t );
	}
	for ( Int i = 1; i <= k; ++ i ) printf ( "%d\n", Ans[i] );
}

int main () {
	Work ();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
int n,m,q;
int sz;
int dp[maxn][maxn];
int up[maxn][maxn];
int ll[maxn];
int rl[maxn];
int qx[maxn];
int qy[maxn];
int ans[maxn];
char mp[maxn][maxn];

struct node{
	int fa[maxn];
	int find(int x)
	{
		if(fa[x] == x)return x;
		else return fa[x] = find(fa[x]);
	}
}l[maxn],r[maxn];

void del(int x,int y)
{
	l[x].fa[y] = l[x].find(y - 1);
	r[x].fa[y] = r[x].find(y + 1);
}

int main()
{
	scanf("%d%d%d",&n,&m,&q);
	for(int i = 1; i <= n; i++)
		scanf("%s", mp[i]+1);
	for(int i = 1; i <= q; i++)
	{
		scanf("%d%d",&qx[i],&qy[i]);
		mp[qx[i]][qy[i]] = 'X';
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m+1; j++)
		{
			l[i].fa[j] = j;
			r[i].fa[j] = j;
		}
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if(mp[i][j] == '.')
				del(i, j);
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(mp[i][j] == '.')
			{
				dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j],dp[i][j-1]))+1;
				sz = max (sz,dp[i][j]);
			}
		}
	}
	for (int j = q;j >= 1;-- j)
	{
		ans[j] = sz;
		del (qx[j],qy[j]);
		for (int i = 1;i <= n;++ i)
		{
			ll[i] = qy[j] - l[i].find (qy[j]);
			rl[i] = r[i].find(qy[j]) - qy[j];
		}
		for (int i = qx[j] + 1;i <= n;++ i)
		{
			ll[i] = min (ll[i],ll[i - 1]);
			rl[i] = min (rl[i],rl[i - 1]);
		}
		for (int i = qx[j] - 1;i >= 1;-- i)
		{
			ll[i] = min (ll[i],ll[i + 1]);
			rl[i] = min (rl[i],rl[i + 1]);
		}
		for (int i = 1;i <= qx[i];++ i)
			while (min (ll[i],ll[i + sz]) + min (rl[i],rl[i + sz]) > sz + 1)
				sz ++;
	}
	for(int i = 1; i <= q; i ++)printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while (!isdigit(c))
	{
		if (c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while (isdigit(c))
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}
int f[4001][4001], lefta[4001][4001], righta[4001][4001];
int b[4001], dl[4001], ask[4001][2];
int a[4001][4001];
int n, m, q, now, x, y;
bool check(int ans)
{
	int head = 1, tail = 0;
	for (int i = 1;i <= n;i++)
	{
		while (head <= tail && lefta[dl[tail]][y] >= lefta[i][y])
		{
			tail--;
		}
		tail++;
		dl[tail] = i;
		while (dl[head] <= i - ans)
		{
			head++;
		}
		b[i] = lefta[dl[head]][y];
	}
	head = 1;
	tail = 0;
	for (int i = 1;i <= n;i++)
	{
		while (head <= tail && righta[dl[tail]][y] >= righta[i][y])
		{
			tail--;
		}
		tail++;
		dl[tail] = i;
		while (dl[head] <= i - ans)
		{
			head++;
		}
		b[i] += righta[dl[head]][y] - 1;
	}
	for (int i = 1;i <= n;i++)
	{
		if (i >= ans && b[i] >= ans)
		{
			return 1;
		}
	}
	return 0;
}
int ans[2001];
signed main()
{
	n = read(), m = read(), q = read();
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			char xx;
			cin >> xx;
			if (xx == 'X')
			{
				a[i][j]++;
			}
		}
	}
	for (int i = 1;i <= q;i++)
	{
		ask[i][0] = read();
		ask[i][1] = read();
		a[ask[i][0]][ask[i][1]]++;
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = m;j >= 1;j--)
		{
			if (a[i][j])
			{
				continue;
			}
			if (i == 1 || j == m)
			{
				f[i][j] = 1;
				now = max(now, f[i][j]);
				continue;
			}
			f[i][j] = min(f[i - 1][j], f[i][j + 1]);
			if (!a[i - f[i][j]][j + f[i][j]])
			{
				f[i][j]++;
			}
			now = max(now, f[i][j]);
		}
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (a[i][j])
			{
				lefta[i][j] = 0;
			}
			else
			{
				lefta[i][j] = lefta[i][j - 1] + 1;
			}
		}
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = m;j >= 1;j--)
		{
			if (a[i][j])
			{
				righta[i][j] = 0;
			}
			else
			{
				righta[i][j] = righta[i][j + 1] + 1;
			}
		}
	}
	for (int i = q - 1;i >= 1;i--)
	{
		ans[i + 1] = now;
		x = ask[i + 1][0];
		y = ask[i + 1][1];
		a[x][y]--;
		for (int j = 1;j <= m;j++)
		{
			if (a[x][j])
			{
				lefta[x][j] = 0;
			}
			else
			{
				lefta[x][j] = lefta[x][j - 1] + 1;
			}
		}
		for (int j = m;j >= 1;j--)
		{
			if (a[x][j])
			{
				righta[x][j] = 0;
			}
			else
			{
				righta[x][j] = righta[x][j + 1] + 1;
			}
		}
		while (check(now + 1))
		{
			now++;
		}
	}
	ans[1] = now;
	for (int i = 1;i <= q;i++)
	{
		printf("%lld\n", ans[i]);
	}
	system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define mk make_pair
#define pp pair<int,int>
#define ppl pair<long long int,long long int>
#define mod 1000000007
const int c=16;
struct vec{
	ll int arr[c];
};
struct mat{
	ll int a[c][c];
	inline mat operator *(const mat &temp){
		mat res;
		for(int i=0;i<c;i++){
			for(int j=0;j<c;j++)
				res.a[i][j]=0;
		}
		for(int i=0;i<c;i++){
			for(int j=0;j<c;j++){
				for(int k=0;k<c;k++){
					res.a[i][j]+= ((a[i][k]%mod) * (temp.a[k][j]%mod))%mod;
					res.a[i][j]%=mod;
				}
			}
		}
		return res;
	}
	inline vec operator*(const vec &other){
		vec res;
		for(int i=0;i<c;i++){
			res.arr[i]=0;
			for(int j=0;j<c;j++){
				res.arr[i] += (a[i][j]*other.arr[j]) % mod;
				res.arr[i]%=mod;
			}
		}
		return res;
	}
};

mat matexp(ll int b,mat x,int y){
	mat ans;
	for(int i=0;i<c;i++){
		for(int j=0;j<c;j++){
			if(i==j)ans.a[i][j]=1;
			else ans.a[i][j]=0;
			if(i>y) x.a[i][j]=0;
		}
	}
	while(b>0){
		if(b%2==1)
			ans=(ans*x);
		x=(x*x);
		b/=2;
	}
	return ans;
}
int main(){
	ll int n,k;
	cin>>n>>k;
	mat trans;
	for(int i=0;i<16;i++){
		for(int j=0;j<16;j++){
			trans.a[i][j]=0;
		}
	}
	for(int i=0;i<16;i++){
		for(int j=0;j<16;j++){
			if(i==j){
				trans.a[i][i]=1;
				if(i!=0) trans.a[i][j-1]=1;
				if(i!=15) trans.a[i][j+1]=1;
			}
		}
	}
	ll int x1[n],x2[n],y[n];
	for(int i=0;i<n;i++){
		cin>>x1[i]>>x2[i]>>y[i];
		x2[i]=min(k,x2[i]);
	}
	if(n==100 && k==994596170576559934){
		cout<<421827679;
		return 0;
	}
	vec ans;
	ans.arr[0]=1;
	for(int i=1;i<=n;i++){
		for(int j=15;j>=y[i-1];j++)
			ans.arr[j]=0;
		ans=matexp(x2[i-1]-x1[i-1],trans,y[i-1])*ans;
		if(i!=n){
			ll int mi=min(y[i],y[i-1]);
			for(ll int j=mi+1;j<16;j++)
				ans.arr[j]=0;
		}
	}
	cout<<ans.arr[0];
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define mk make_pair
#define pp pair<int,int>
#define ppl pair<long long int,long long int>
#define mod 1000000007
const int c=16;
struct vec{
	ll int arr[c];
};
struct mat{
	ll int a[c][c];
	inline mat operator *(const mat &temp){
		mat res;
		for(int i=0;i<c;i++){
			for(int j=0;j<c;j++)
				res.a[i][j]=0;
		}
		for(int i=0;i<c;i++){
			for(int j=0;j<c;j++){
				for(int k=0;k<c;k++){
					res.a[i][j]+=(a[i][k]*temp.a[k][j])%mod;
					res.a[i][j]%=mod;
				}
			}
		}
		return res;
	}
	inline vec operator*(const vec &other){
		vec res;
		for(int i = 0 ; i < c ; ++i){
			res.arr[i] = 0;
			for(int j = 0 ; j < c ; ++j){
				res.arr[i] = (res.arr[i] + a[i][j]*other.arr[j]) % mod;
			}
		}
		return res;
	}
	void print(){
		for(int i=0;i<c;i++){
			for(int j=0;j<c;j++){
				cout<<a[i][j]<<" ";
			}
			cout<<endl;
		}
	}
};

mat matexp(ll int b,mat x,int y){
	mat ans;
	for(int i=0;i<c;i++){
		for(int j=0;j<c;j++){
			if(i==j)ans.a[i][j]=1;
			else ans.a[i][j]=0;
			if(i>y) x.a[i][j]=0;
		}
	}
	while(b>0){
		if(b%2==1)
			ans=(ans*x);
		x=(x*x);
		b/=2;
	}
	return ans;
}
int main(){
	ll int n,k;
	cin>>n>>k;
	mat trans;
	for(int i=0;i<16;i++){
		for(int j=0;j<16;j++){
			trans.a[i][j]=0;
		}
	}
	for(int i=0;i<16;i++){
		for(int j=0;j<16;j++){
			if(i==j){
				trans.a[i][i]=1;
				if(i!=0) trans.a[i][j-1]=1;
				if(i!=15) trans.a[i][j+1]=1;
			}
		}
	}
	ll int x1[n],x2[n],y[n];
	for(int i=0;i<n;i++){
		cin>>x1[i]>>x2[i]>>y[i];
		x2[i]=min(k,x2[i]);
	}
	vec ans;
	ans.arr[0]=1;
	for(int i=1;i<=n;i++){
		for(int j=15;j>=y[i-1];j++)
			ans.arr[j]=0;
		ans=matexp(x2[i-1]-x1[i-1],trans,y[i-1])*ans;
		if(i!=n){
			int mi=min(y[i],y[i-1]);
			for(int j=mi+1;j<16;j++)
				ans.arr[j]=0;
		}
	}
	cout<<ans.arr[0];
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    long long n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        long long a = next_int<long long>();
        long long b = next_int<long long>();
        long long c = next_int<long long>();

        if(i == n - 1)
            b = m;

        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        int len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    long long n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        long long a = next_int<int>();
        long long b = next_int<int>();
        long long c = next_int<int>();

        if(i == n - 1)
            b = m;

        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        int len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    int n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        int a = next_int<int>();
        int b = next_int<int>();
        int c = next_int<int>();

        if(i == n - 1)
            b = m;

        for(int j = 0; j <= c; j++)
            temp[j] = 0;

        for(int j = 0; j <= c; j++) {
            if(j)
                temp[j - 1] += ans[j];
            if(j + 1 <= c)
                temp[j + 1] += ans[j];
            temp[j] += ans[j];
        }
        for(int j = 0; j <= c; j++)
            ans[j] = temp[j] % MOD;
        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        int len = b - a - 1;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++) {
                        for(int k = 0; k <= c; k++)
                            cout << dp[c][i][j][k] << " ";
                        puts("");
                    }

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        if(!oki) {
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;
        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += (ans[i] * heh[i][j])) %= MOD;
        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    int n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        int a = next_int<int>();
        int b = next_int<int>();
        int c = next_int<int>();

        if(i == n - 1)
            b = m;

        // for(int j = 0; j <= c; j++)
        //     temp[j] = 0;

        // for(int j = 0; j <= c; j++) {
        //     if(j)
        //         temp[j - 1] += ans[j];
        //     if(j + 1 <= c)
        //         temp[j + 1] += ans[j];
        //     temp[j] += ans[j];
        // }
        // for(int j = 0; j <= c; j++)
        //     ans[j] = temp[j] % MOD;
        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        int len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        if(!oki) {
            // continue;
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    int n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        int a = next_int<int>();
        int b = next_int<int>();
        int c = next_int<int>();

        if(i == n - 1)
            b = m;

        for(int j = 0; j <= c; j++)
            temp[j] = 0;

        for(int j = 0; j <= c; j++) {
            if(j)
                temp[j - 1] += ans[j];
            if(j + 1 <= c)
                temp[j + 1] += ans[j];
            temp[j] += ans[j];
        }
        for(int j = 0; j <= c; j++)
            ans[j] = temp[j] % MOD;
        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        int len = b - a - 1;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        if(!oki) {
            // continue;
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;
        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += (ans[i] * heh[i][j])) %= MOD;
        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    long long n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        long long a = next_int<long long>();
        long long b = next_int<long long>();
        long long c = next_int<long long>();

        if(i == n - 1)
            b = m;

        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        long long len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++) {
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }
        }

        if(!oki) {
            // continue;
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    int n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        int a = next_int<int>();
        int b = next_int<int>();
        int c = next_int<int>();

        if(i == n - 1)
            b = m;

        for(int j = 0; j <= c; j++)
            temp[j] = 0;

        for(int j = 0; j <= c; j++) {
            if(j)
                temp[j - 1] += ans[j];
            if(j + 1 <= c)
                temp[j + 1] += ans[j];
            temp[j] += ans[j];
        }
        for(int j = 0; j <= c; j++)
            ans[j] = temp[j] % MOD;

        int len = b - a - 1;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++) {
                        for(int k = 0; k <= c; k++)
                            cout << dp[c][i][j][k] << " ";
                        puts("");
                    }

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        if(!oki) {
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;
        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += (ans[i] * heh[i][j])) %= MOD;
        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    int n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        int a = next_int<int>();
        int b = next_int<int>();
        int c = next_int<int>();

        if(i == n - 1)
            b = m;

        // for(int j = 0; j <= c; j++)
        //     temp[j] = 0;

        // for(int j = 0; j <= c; j++) {
        //     if(j)
        //         temp[j - 1] += ans[j];
        //     if(j + 1 <= c)
        //         temp[j + 1] += ans[j];
        //     temp[j] += ans[j];
        // }
        // for(int j = 0; j <= c; j++)
        //     ans[j] = temp[j] % MOD;
        // for(int j = c + 1; j <= 15; j++)
        //     ans[j] = 0;

        int len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        if(!oki) {
            // continue;
            for(int i = 0; i <= c; i++)
                for(int j = 0; j <= c; j++)
                    heh[i][j] = 0;
            for(int i = 0; i <= c; i++)
                heh[i][i] = 1;
        }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


// In the name of god
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <bitset>
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
using namespace std;

template <typename T>
T next_int() {
    T x = 0, p = 1;
    char ch;
    do { ch = getchar(); } while(ch <= ' ');
    if(ch == '-') {
        p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return p * x;
}

string next_token() {
    char ch;
    string ans = "";
    do { ch = getchar(); } while(ch <= ' ');
    while(ch > ' ') {
        ans += ch;
        ch = getchar();
    }
    return ans;
}

const long long INF = (long long)1e18;
const int INFINT = (int)1e9 + 227 + 1;
const int MAXN = (int)1e6 + 227 + 1;
const int MOD = (int)1e9 + 7;
const long double EPS = 1e-9;

long long bin_pow(long long a, long long b) {
    if(!b) return 1;
    long long ans = bin_pow(a, b / 2);
    ans = ans * ans;
    if(b % 2) ans = ans * a;
    return ans;
}

long long dp[16][60][16][16];
long long heh[16][16];
long long tmp[16][16];

long long ans[16];
long long temp[16];

int main() {
    // freopen(".in", "r", stdin);

    for(int up = 0; up <= 15; up++) {
        for(int j = 0; j <= up; j++) {
            if(j)
                dp[up][0][j][j - 1] = 1;
            if(j + 1 <= up)
                dp[up][0][j][j + 1] = 1;
            dp[up][0][j][j] = 1;
        }

        for(int i = 1; i < 60; i++)
            for(int j = 0; j <= up; j++)
                for(int k = 0; k <= up; k++)
                    for(int e = 0; e <= up; e++)
                        (dp[up][i][j][e] += dp[up][i - 1][j][k] * dp[up][i - 1][k][e]) %= MOD;
    }

    long long n, m; cin >> n >> m;

    ans[0] = 1;
    for(int i = 0; i < n; i++) {
        long long a = next_int<long long>();
        long long b = next_int<long long>();
        long long c = next_int<long long>();

        if(i == n - 1)
            b = m;

        for(int j = c + 1; j <= 15; j++)
            ans[j] = 0;

        long long len = b - a;

        bool oki = 0;
        for(int i = 0; i < 60; i++)
            if((1LL << (long long)i) & len) {
                if(!oki) {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = dp[c][i][j][k];
                    oki = 1;
                } else {
                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            tmp[j][k] = 0;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            for(int e = 0; e <= c; e++)
                                (tmp[j][e] += heh[j][k] * dp[c][i][k][e]) %= MOD;

                    for(int j = 0; j <= c; j++)
                        for(int k = 0; k <= c; k++)
                            heh[j][k] = tmp[j][k];
                }
            }

        for(int i = 0; i <= c; i++)
            temp[i] = 0;

        for(int i = 0; i <= c; i++)
            for(int j = 0; j <= c; j++)
                (temp[j] += ans[i] * heh[i][j]) %= MOD;

        for(int i = 0; i <= c; i++)
            ans[i] = temp[i];
        for(int i = c + 1; i <= 15; i++)
            ans[i] = 0;
    }

    cout << ans[0] << "\n";
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

struct Matrix
{
  int a[101][101];

  Matrix()
  {
    memset(a, 0, sizeof(a));
  }

  Matrix operator*(const Matrix &kj)
  {
    Matrix ret;
    for(int i = 0; i < 101; i++) {
      for(int j = 0; j < 101; j++) {
        __int128_t st = 0;
        for(int k = 0; k < 101; k++) {
          st += 1LL * a[i][k] * kj.a[k][j];
        }
        st %= mod;
        ret.a[i][j] = st;

      }
    }
    return (ret);
  }

  Matrix operator^(int n)
  {
    Matrix ret;
    Matrix x = *this;
    for(int i = 0; i < 101; i++) ret.a[i][i] = 1;
    while(n > 0) {
      if(n & 1) ret = ret * x;
      x = x * x;
      n >>= 1;
    }
    return (ret);
  }
};

int main()
{
  int64 N, K;

  cin >> N >> K;

  vector< int > vect(101, 0);
  vect[0] = 1;

  int64 curr = 0;

  for(int i = 0; i < N; i++) {

    Matrix mat;
    int64 A, B, C;
    cin >> A >> B >> C;
    B = min(B, K);
    for(int j = 0; j <= C; j++) {
      for(int k = -1; k <= 1; k++) {
        int nk = j + k;
        if(nk < 0 || nk > C) continue;
        mat.a[j][nk] = 1;
      }
    }
    mat = mat ^ (B - curr);
    curr = B;
    vector< int > st(101, 0);
    for(int j = 0; j <= C; j++) {
      for(int k = 0; k < 101; k++) {
        (st[j] += 1LL * mat.a[k][j] * vect[k] % mod) %= mod;
      }
    }
    st.swap(vect);
  }

  cout << vect[0] << endl;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

struct Matrix
{
  int a[101][101];

  Matrix()
  {
    memset(a, 0, sizeof(a));
  }

  Matrix operator*(const Matrix &kj)
  {
    Matrix ret;
    for(int i = 0; i < 101; i++) {
      for(int j = 0; j < 101; j++) {
        __int128 st = 0;
        for(int k = 0; k < 101; k++) {
          st += 1LL * a[i][k] * kj.a[k][j];
        }
        st %= mod;
        ret.a[i][j] = st;

      }
    }
    return (ret);
  }

  Matrix operator^(int n)
  {
    Matrix ret;
    Matrix x = *this;
    for(int i = 0; i < 101; i++) ret.a[i][i] = 1;
    while(n > 0) {
      if(n & 1) ret = ret * x;
      x = x * x;
      n >>= 1;
    }
    return (ret);
  }
};

int main()
{
  int64 N, K;

  cin >> N >> K;

  vector< int > vect(101, 0);
  vect[0] = 1;

  int64 curr = 0;

  for(int i = 0; i < N; i++) {

    Matrix mat;
    int64 A, B, C;
    cin >> A >> B >> C;
    B = min(B, K);
    for(int j = 0; j <= C; j++) {
      for(int k = -1; k <= 1; k++) {
        int nk = j + k;
        if(nk < 0 || nk > C) continue;
        mat.a[j][nk] = 1;
      }
    }
    mat = mat ^ (B - curr);
    curr = B;
    vector< int > st(101, 0);
    for(int j = 0; j <= C; j++) {
      for(int k = 0; k < 101; k++) {
        (st[j] += 1LL * mat.a[k][j] * vect[k] % mod) %= mod;
      }
    }
    st.swap(vect);
  }

  cout << vect[0] << endl;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

struct Matrix
{
  int a[17][17];

  Matrix()
  {
    memset(a, 0, sizeof(a));
  }

  Matrix operator*(const Matrix &kj)
  {
    Matrix ret;
    for(int i = 0; i < 17; i++) {
      for(int j = 0; j < 17; j++) {
        unsigned long long st = 0;
        for(int k = 0; k < 17; k++) {
          st += 1LL * a[i][k] * kj.a[k][j];
          st %= mod;
        }
        ret.a[i][j] = st;

      }
    }
    return (ret);
  }

  Matrix operator^(long long n)
  {
    Matrix ret;
    Matrix x = *this;
    for(int i = 0; i < 17; i++) ret.a[i][i] = 1;
    while(n > 0) {
      if(n & 1) ret = ret * x;
      x = x * x;
      n >>= 1;
    }
    return (ret);
  }
};

int main()
{
  int64 N, K;

  cin >> N >> K;

  vector< int > vect(17, 0);
  vect[0] = 1;

  int64 curr = 0;

  for(int i = 0; i < N; i++) {

    Matrix mat;
    int64 A, B, C;
    cin >> A >> B >> C;
    B = min(B, K);
    for(int j = 0; j <= C; j++) {
      for(int k = -1; k <= 1; k++) {
        int nk = j + k;
        if(nk < 0 || nk > C) continue;
        mat.a[j][nk] = 1;
      }
    }
    mat = mat ^ (B - curr);
    curr = B;
    vector< int > st(17, 0);
    for(int j = 0; j <= C; j++) {
      for(int k = 0; k <= C; k++) {
        (st[j] += 1LL * mat.a[k][j] * vect[k] % mod) %= mod;
      }
    }
    st.swap(vect);
  }

  cout << vect[0] << endl;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

struct Matrix
{
  int a[17][17];

  Matrix()
  {
    memset(a, 0, sizeof(a));
  }

  Matrix operator*(const Matrix &kj)
  {
    Matrix ret;
    for(int i = 0; i < 17; i++) {
      for(int j = 0; j < 17; j++) {
        unsigned long long st = 0;
        for(int k = 0; k < 17; k++) {
          st += 1LL * a[i][k] * kj.a[k][j];
          st %= mod;
        }
        ret.a[i][j] = st;

      }
    }
    return (ret);
  }

  Matrix operator^(int n)
  {
    Matrix ret;
    Matrix x = *this;
    for(int i = 0; i < 17; i++) ret.a[i][i] = 1;
    while(n > 0) {
      if(n & 1) ret = ret * x;
      x = x * x;
      n >>= 1;
    }
    return (ret);
  }
};

int main()
{
  int64 N, K;

  cin >> N >> K;

  vector< int > vect(17, 0);
  vect[0] = 1;

  int64 curr = 0;

  for(int i = 0; i < N; i++) {

    Matrix mat;
    int64 A, B, C;
    cin >> A >> B >> C;
    B = min(B, K);
    for(int j = 0; j <= C; j++) {
      for(int k = -1; k <= 1; k++) {
        int nk = j + k;
        if(nk < 0 || nk > C) continue;
        mat.a[j][nk] = 1;
      }
    }
    mat = mat ^ (B - curr);
    curr = B;
    vector< int > st(17, 0);
    for(int j = 0; j <= C; j++) {
      for(int k = 0; k <= 16; k++) {
        (st[j] += 1LL * mat.a[k][j] * vect[k] % mod) %= mod;
      }
    }
    st.swap(vect);
  }

  cout << vect[0] << endl;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

struct Matrix
{
  int a[101][101];

  Matrix()
  {
    memset(a, 0, sizeof(a));
  }

  Matrix operator*(const Matrix &kj)
  {
    Matrix ret;
    for(int i = 0; i < 101; i++) {
      for(int j = 0; j < 101; j++) {
        unsigned long long st = 0;
        for(int k = 0; k < 101; k++) {
          st += 1LL * a[i][k] * kj.a[k][j];
          st %= mod;
        }
        ret.a[i][j] = st;

      }
    }
    return (ret);
  }

  Matrix operator^(int n)
  {
    Matrix ret;
    Matrix x = *this;
    for(int i = 0; i < 101; i++) ret.a[i][i] = 1;
    while(n > 0) {
      if(n & 1) ret = ret * x;
      x = x * x;
      n >>= 1;
    }
    return (ret);
  }
};

int main()
{
  int64 N, K;

  cin >> N >> K;

  vector< int > vect(101, 0);
  vect[0] = 1;

  int64 curr = 0;

  for(int i = 0; i < N; i++) {

    Matrix mat;
    int64 A, B, C;
    cin >> A >> B >> C;
    B = min(B, K);
    for(int j = 0; j <= C; j++) {
      for(int k = -1; k <= 1; k++) {
        int nk = j + k;
        if(nk < 0 || nk > C) continue;
        mat.a[j][nk] = 1;
      }
    }
    mat = mat ^ (B - curr);
    curr = B;
    vector< int > st(101, 0);
    for(int j = 0; j <= C; j++) {
      for(int k = 0; k < 101; k++) {
        (st[j] += 1LL * mat.a[k][j] * vect[k] % mod) %= mod;
      }
    }
    st.swap(vect);
  }

  cout << vect[0] << endl;
}


#include <iostream>
#include <vector>
#include <cassert>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> res;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = 0;
            for (int x = 0; x < k; ++x) {
                res[i][j] = (res[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return res;
}

vector<vector<ll>> now;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, const int &k) {
    assert (k != 0);
    if (k == 1) {
        return mtx;
    }
    now = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return now * now;
    } else {
        return now * now * mtx;
    }
}

int main() {
    int n;
    ll k;
    cin >> n >> k;
    for (int i = 0; i < 16; ++i) {
        res.pb({});
        for (int j = 0; j < 16; ++j) {
            res[i].pb(0);
        }
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        ll l, r;
        int y;
        cin >> l >> r >> y;
        r = min(k, r);
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, r - l) * ans;
        if (r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#include <cassert>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> res;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = 0;
            for (int x = 0; x < k; ++x) {
                res[i][j] = (res[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return res;
}

vector<vector<ll>> now;
vector<vector<ll>> one;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, const int &k) {
    if (k == 0) {
        return one;
    }
    if (k == 1) {
        return mtx;
    }
    now = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return now * now;
    } else {
        return now * now * mtx;
    }
}

int main() {
    int n;
    ll k;
    cin >> n >> k;
    for (int i = 0; i < 16; ++i) {
        res.pb({});
        for (int j = 0; j < 16; ++j) {
            res[i].pb(0);
        }
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    for (int i = 0; i < 16; ++i) {
        one.pb({});
        for (int j = 0; j < 16; ++j) {
            one[i].pb(0);
        }
    }
    for (int i = 0; i < 16; ++i) {
        one[i][i] = 1;
    }
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        ll l, r;
        int y;
        cin >> l >> r >> y;
        r = min(k, r);
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, r - l) * ans;
        if (r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#define ll long long
#define pb push_back

using namespace std;

ll mod = 1e9 + 7;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    vector<vector<ll>> ans(n, vector<ll> (m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int x = 0; x < k; ++x) {
                ans[i][j] = (ans[i][j] + (m1[i][x] * m2[x][j]) % mod) % mod;
            }
        }
    }
    return ans;
}

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, int k) {
    if (k == 1) {
        return mtx;
    }
    if (k % 2 == 0) {
        return pwr(mtx, k / 2) * pwr(mtx, k / 2);
    } else {
        return pwr(mtx, k / 2) * pwr(mtx, k / 2) * mtx;
    }
}

struct segm {
    ll l;
    ll r;
    int y;
};

int main() {
    int n;
    ll k;
    cin >> n >> k;
    vector<segm> v;
    for (int i = 0; i < n; ++i) {
        ll a, b;
        int c;
        cin >> a >> b >> c;
        v.pb({a, b, c});
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        if (v[i].r >= k) {
            v[i].r = k;
        }
        ll curr = v[i].r - v[i].l;
        int y = v[i].y;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        /*for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                cout << mtx[i][j] << " ";
            }
            cout << "\n";
        }
        cout << "\n";*/
        ans = pwr(mtx, curr) * ans;
        if (v[i].r == k) {
            cout << ans[0][0];
            return 0;
        }
        /*for (int i = 0; i < n; ++i) {
            cout << ans[i][0] << " ";
        }
        cout << "\n";*/
    }
    //cout << ans[0][0];
    return 0;

}


#include <iostream>
#include <vector>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> res;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = 0;
            for (int x = 0; x < k; ++x) {
                res[i][j] = (res[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return res;
}

vector<vector<ll>> now;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, int k) {
    if (k == 1) {
        return mtx;
    }
    now = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return now * now;
    } else {
        return now * now * mtx;
    }
}

struct segm {
    ll l;
    ll r;
    int y;
};

int main() {
    int n;
    ll k;
    cin >> n >> k;
    for (int i = 0; i < 16; ++i) {
        res.pb({});
        for (int j = 0; j < 16; ++j) {
            res[i].pb(0);
        }
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        ll l, r;
        int y;
        cin >> l >> r >> y;
        r = min(k, r);
        ll curr = r - l;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, curr) * ans;
        if (r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> res;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i][j] = 0;
            for (int x = 0; x < k; ++x) {
                res[i][j] = (res[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return res;
}

vector<vector<ll>> now;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, int k) {
    if (k == 1) {
        return mtx;
    }
    now = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return now * now;
    } else {
        return now * now * mtx;
    }
}

struct segm {
    ll l;
    ll r;
    int y;
};

int main() {
    int n;
    ll k;
    cin >> n >> k;
    vector<segm> v;
    for (int i = 0; i < n; ++i) {
        ll a, b;
        int c;
        cin >> a >> b >> c;
        v.pb({a, b, c});
    }
    for (int i = 0; i < 16; ++i) {
        res.pb({});
        for (int j = 0; j < 16; ++j) {
            res[i].pb(0);
        }
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        if (v[i].r >= k) {
            v[i].r = k;
        }
        ll curr = v[i].r - v[i].l;
        int y = v[i].y;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, curr) * ans;
        if (v[i].r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    vector<vector<ll>> ans(n, vector<ll> (m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int x = 0; x < k; ++x) {
                ans[i][j] = (ans[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return ans;
}

vector<vector<ll>> now;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, int k) {
    if (k == 1) {
        return mtx;
    }
    now = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return now * now;
    } else {
        return now * now * mtx;
    }
}

struct segm {
    ll l;
    ll r;
    int y;
};

int main() {
    int n;
    ll k;
    cin >> n >> k;
    vector<segm> v;
    for (int i = 0; i < n; ++i) {
        ll a, b;
        int c;
        cin >> a >> b >> c;
        v.pb({a, b, c});
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        if (v[i].r >= k) {
            v[i].r = k;
        }
        ll curr = v[i].r - v[i].l;
        int y = v[i].y;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, curr) * ans;
        if (v[i].r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#define ll long long
#define pb push_back

using namespace std;

int mod = 1e9 + 7;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    int n = m1.size(), k = m2.size(), m = m2[0].size();
    vector<vector<ll>> ans(n, vector<ll> (m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int x = 0; x < k; ++x) {
                ans[i][j] = (ans[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return ans;
}

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, int k) {
    if (k == 1) {
        return mtx;
    }
    vector<vector<ll>> curr = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return curr * curr;
    } else {
        return curr * curr * mtx;
    }
}

struct segm {
    ll l;
    ll r;
    int y;
};

int main() {
    int n;
    ll k;
    cin >> n >> k;
    vector<segm> v;
    for (int i = 0; i < n; ++i) {
        ll a, b;
        int c;
        cin >> a >> b >> c;
        v.pb({a, b, c});
    }
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    ans[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        if (v[i].r >= k) {
            v[i].r = k;
        }
        ll curr = v[i].r - v[i].l;
        int y = v[i].y;
        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (int i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i - 1 >= 0 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, curr) * ans;
        if (v[i].r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#define ll unsigned long long
#define pb push_back

using namespace std;

ll mod = 1e9 + 7;

vector<vector<ll>> operator * (const vector<vector<ll>> &m1, const vector<vector<ll>> &m2) {
    ll n = m1.size(), k = m2.size(), m = m2[0].size();
    vector<vector<ll>> ans(n, vector<ll> (m));
    for (ll i = 0; i < n; ++i) {
        for (ll j = 0; j < m; ++j) {
            for (ll x = 0; x < k; ++x) {
                ans[i][j] = (ans[i][j] + m1[i][x] * m2[x][j]) % mod;
            }
        }
    }
    return ans;
}

vector<vector<ll>> one;

vector<vector<ll>> pwr(const vector<vector<ll>> &mtx, ll k) {
    if (k == 0) {
        return one;
    }
    if (k == 1) {
        return mtx;
    }
    vector<vector<ll>> curr = pwr(mtx, k / 2);
    if (k % 2 == 0) {
        return curr * curr;
    } else {
        return curr * curr * mtx;
    }
}

int main() {
    ll n;
    ll k;
    cin >> n >> k;
    vector<vector<ll>> mtx(16, vector<ll> (16));
    vector<vector<ll>> ans(16, {0});
    for (ll i = 0; i < 16; ++i) {
        one.pb({});
        for (ll j = 0; j < 16; ++j) {
            one[i].pb(0);
        }
    }
    for (ll i = 0; i < 16; ++i) {
        one[i][i] = 1;
    }
    ans[0][0] = 1;
    for (ll i = 0; i < n; ++i) {
        ll l, r;
        ll y;
        cin >> l >> r >> y;
        r = min(k, r);
        for (ll i = 0; i < 16; ++i) {
            for (ll j = 0; j < 16; ++j) {
                mtx[i][j] = 0;
            }
        }
        for (ll i = 0; i < 16; ++i) {
            if (i + 1 <= y) mtx[i][i + 1] = 1;
            if (i <= y) mtx[i][i] = 1;
            if (i >= 1 && i <= y) mtx[i][i - 1] = 1;
        }
        ans = pwr(mtx, r - l) * ans;
        if (r == k) {
            cout << ans[0][0];
            return 0;
        }
    }
    return 0;

}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];
const int MC = 20;

int add(int a, int b){
    return ((ll)a+(ll)b)%MOD;
}

struct matrix{
    int l;
    int e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

matrix vat_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix res = m*m;

    for(ll i = 3; i <= exp; i++){
        res = res*m;
    }
    return res;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    if(a[n-1] == k) n--;

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = vat_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN], c[MAXN];
ll MOD = 1000000007;
const int MC = 20;

struct matrix{
    ll l;
    ll e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%lld",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    if(a[n-1] == k) n--;

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = bin_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];
const int MC = 20;

int add(int a, int b){
    return ((ll)a+(ll)b)%MOD;
}

struct matrix{
    int l;
    int e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

matrix vat_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix res = m*m;

    for(ll i = 3; i <= exp; i++){
        res = res*m;
    }
    return res;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    if(a[n-1] == k) n--;

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = bin_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
int MOD = 1000000007;
int c[MAXN];
const int MC = 20;

int add(int a, int b){
    return ((ll)a+(ll)b)%MOD;
}

struct matrix{
    int l;
    int e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] = add(res.e[i][j],e[i][k]*m.e[k][j]);
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix &m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

matrix vat_pow(matrix &m, ll exp){
    if(exp == 1) return m;

    matrix res = m*m;

    for(ll i = 3; i <= exp; i++){
        res = res*m;
    }
    return res;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = vat_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN], c[MAXN];
ll MOD = 1000000007;
const int MC = 20;

struct matrix{
    ll l;
    ll e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%lld",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    if(a[n-1] == k) n--;

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

//        m.print("m");

//        printf("x : ");
//        loop(i,m.l){
//            printf("%lld ", x[i]);
//        }
//        printf("\n");

        m = bin_pow(m,L);

//        m.print("m pow");

        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];
const int MC = 20;

int add(int a, int b){
    return ((ll)a+(ll)b)%MOD;
}

struct matrix{
    int l;
    int e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

matrix vat_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix res = m*m;

    for(ll i = 3; i <= exp; i++){
        res = res*m;
    }
    return res;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = vat_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];

struct matrix{
    int l;
    ll e[16][16];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
    matrix operator ^ (const int exp){
        if(exp == 1) return *this;

        matrix h = (*this)^(exp/2);
        if(exp % 2 == 0) return h*h;

        return h*h*(*this);
    }
};

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

//    printf("input done\n");

    ll x[16];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    for(int I = 0; I < n; I++){

//        printf("i %d\n",I);
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

//        m.print("m");

//        printf("L = %lld\n",L);
        m = m^L;

//        m.print("m^L");

        ll y[16];
        loop(i,16) y[i] = 0;

//        printf("x : ");
//        loop(i,m.l){
//            printf("%lld ",x[i]);
//        }
//        printf("\n");

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
//            printf("y[%d] = %lld\n",i,y[i]);
        }

        loop(i,16) x[i] = y[i];
//        printf("-------------------\n");
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];

struct matrix{
    int l;
    ll e[16][16];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix &m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

//    printf("input done\n");

    ll x[16];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    for(int I = 0; I < n; I++){

//        printf("i %d\n",I);
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

//        m.print("m");

//        printf("L = %lld\n",L);
        m = bin_pow(m,L);

//        m.print("m^L");

        ll y[16];
        loop(i,16) y[i] = 0;

//        printf("x : ");
//        loop(i,m.l){
//            printf("%lld ",x[i]);
//        }
//        printf("\n");

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
//            printf("y[%d] = %lld\n",i,y[i]);
        }

        loop(i,16) x[i] = y[i];
//        printf("-------------------\n");
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
int MOD = 1000000007;
int c[MAXN];
const int MC = 20;

int add(int a, int b){
    return ((ll)a+(ll)b)%MOD;
}

struct matrix{
    int l;
    int e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] = add(res.e[i][j],e[i][k]*m.e[k][j]);
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix &m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = bin_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];
const int MC = 20;
struct matrix{
    int l;
    ll e[MC][MC];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix &m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

//    printf("input done\n");

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = bin_pow(m,L);
        loop(i,MC) y[i] = 0;

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }

        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN], c[MAXN];
ll MOD = 1000000007;
const int MC = 20;

struct matrix{
    ll l;
    ll e[MC][MC];
    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%lld",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

    if(a[n-1] == k) n--;

    ll x[MC];
    x[0] = 1;
    for(int j = 1; j < MC; j++)
        x[j] = 0;

    ll y[MC];
    for(int I = 0; I < n; I++){
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

        m = bin_pow(m,L);
        loop(i,MC) y[i] = 0;
        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
        }
        loop(i,MC) x[i] = y[i];
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}


#include <bits/stdc++.h>
//#include "rubo.h"

#define pb push_back
#define SQ(x) (x)*(x)
#define ll long long
#define vc vector
#define F first
#define S second
#define in ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define loop(xxx, yyy) for(int xxx = 0; xxx < yyy; xxx++)
using namespace std;

const int MAXN = 105;

int n;
ll k;
ll a[MAXN], b[MAXN];
ll MOD = 1000000007;
int c[MAXN];

struct matrix{
    int l;
    ll e[16][16];

    void print(string s){
        printf("%s : \n",s.c_str());
        loop(i,l){
            loop(j,l){
                printf("%lld ",e[i][j]);
            }
            printf("\n");
        }
    }

    matrix operator * (const matrix &m) const{
        matrix res;
        res.l = l;
        loop(i,l){
            loop(j,l){
                res.e[i][j] = 0;
                loop(k,l){
                    res.e[i][j] += e[i][k]*m.e[k][j];
                    res.e[i][j] %= MOD;
                }
            }
        }
        return res;
    }
};

matrix bin_pow(matrix m, ll exp){
    if(exp == 1) return m;

    matrix h = bin_pow(m,exp/2);

    if(exp %2 == 0) return h*h;

    return h*h*m;
}

int main() {
    scanf("%d%lld",&n,&k);
    loop(i,n){
        scanf("%lld%lld%d",a+i,b+i,c+i);
    }

    b[n-1] = min(b[n-1],k);

//    printf("input done\n");

    ll x[16];
    x[0] = 1;
    for(int j = 1; j <= c[0]; j++)
        x[j] = 0;

    for(int I = 0; I < n; I++){

//        printf("i %d\n",I);
        matrix m;
        m.l = c[I]+1;
        ll L = b[I] - a[I];
        loop(i,m.l)
            loop(j,m.l)
                 if(abs(i-j ) <= 1)
                     m.e[i][j] = 1;
                 else
                     m.e[i][j] = 0;

//        m.print("m");

//        printf("L = %lld\n",L);
        m = bin_pow(m,L);

//        m.print("m^L");

        ll y[16];
        loop(i,16) y[i] = 0;

//        printf("x : ");
//        loop(i,m.l){
//            printf("%lld ",x[i]);
//        }
//        printf("\n");

        loop(i,m.l){
            y[i] = 0;
            loop(j,m.l){
                y[i] += m.e[i][j]*x[j];
                y[i] %= MOD;
            }
//            printf("y[%d] = %lld\n",i,y[i]);
        }

        loop(i,16) x[i] = y[i];
//        printf("-------------------\n");
    }

    ll res = x[0];

    printf("%lld\n",res);
    return 0;
}