1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <bits/stdc++.h>
using namespace std;

long long a[500007];
long long b[500007];
long long drz[1048590];
long long f1[1048590];
long long f2[1048590];

void ustaw(long long gdzie, long long co)
{
	gdzie += 524288;
	drz[gdzie] = co;
	gdzie /= 2;
	while(gdzie != 0)
	{
		drz[gdzie] = drz[2 * gdzie] + drz[2 * gdzie + 1];
		gdzie /= 2;
	}
}

long long suma(long long l, long long r, long long curl, long long curr, long long cur)
{
	if(l > curr || curl > r || l > r)
	{
		return 0;
	}
	else if(l <= curl && curr <= r)
	{
		return drz[cur];
	}
	else
	{
		return suma(l, r, curl, (curl + curr) / 2, 2 * cur) + suma(l, r, (curl + curr) / 2 + 1, curr, 2 * cur + 1);
	}
}

void ustaw2(long long gdzie, long long co1, long long co2)
{
	gdzie += 524288;
	if(co2 > 1)
	{
		f1[gdzie] = co2;
	}
	else
	{
		f2[gdzie] = co1;
	}
	gdzie /= 2;
	while(gdzie != 0)
	{
		f1[gdzie] = (f1[2 * gdzie] * f1[2 * gdzie + 1]) % 1000000007;
		f2[gdzie] = (f1[2 * gdzie + 1] * f2[2 * gdzie] + f2[2 * gdzie + 1]) % 1000000007;
		gdzie /= 2;
	}
}

long long start;

void funk(long long l, long long r, long long curl, long long curr, long long cur)
{
	if(l > curr || curl > r || l > r)
	{
		return;
	}
	else if(l <= curl && curr <= r)
	{
		start = (start * f1[cur] + f2[cur]) % 1000000007;
	}
	else
	{
		funk(l, r, curl, (curl + curr) / 2, 2 * cur);
		funk(l, r, (curl + curr) / 2 + 1, curr, 2 * cur + 1);
	}
}

int main() 
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, q;
	cin >> n >> q;
	vector<int> niejed;
	for(int i = 0; i < 1048580; i++)
	{
		f1[i] = 1;
	}
	for(int i = 0; i < n; i++)
	{
		cin >> a[i] >> b[i];
		ustaw(i, a[i]);
		ustaw2(i, a[i], b[i]);
		if(b[i] != 1)
		{
			niejed.push_back(i);
		}
	}
	while(q--) 
	{
        long long l, r;
        cin >> start >> l >> r;
        l--;
        auto it = lower_bound(niejed.begin(), niejed.end(), l + 1);
        while(l <= r - 1)
        {
            if(start >= 1000000007) 
            {
                start %= 1000000007;
                funk(l + 1, r - 1, 0, 524288 - 1, 1);
                l = r + 7;
            } 
            else 
            {
                if(it != niejed.end() && *it <= r - 1) 
                {
                    start += suma(l + 1, *it - 1, 0, 524288 - 1, 1);
                    if(start + a[*it] > start * b[*it]) 
                    {
                        start += a[*it];
                    } 
                    else 
                    {
                        start *= b[*it];
                    }
                    l = *it;
                    it++;
                } 
                else 
                {
                    start += suma(l + 1, r - 1, 0, 524288 - 1, 1);
                    l = r  + 7;
                }
            }
        }
        cout << start % 1000000007 << endl;
    }
	return 0;
}