#include <bits/stdc++.h>
using namespace std;
#define int long long
const int m = 1e9 + 7;
int n, dp[100005];
pair<int, int> a[100005];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
unordered_map<int, int> rem[100005];
int32_t main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
	dp[1] = 1;
	
	if (a[1].first == 0 || a[1].second == 0) {
		cout << 1;
		return 0;
	}
	
	bool b = 1;
	for (int i = 1; i <= n; i++) if (a[i].first != 1) b = 0;
	
	if (n <= 10000) {
		for (int i = 2; i <= n; i++) {
			for (int j = 1; j < i; j++) {
				if (a[j].first == 0) continue;
				int d = i - j;
				if (d % a[j].first == 0
					&& (d / a[j].first) <= a[j].second)
					dp[i] = (dp[i] + dp[j]) % m;
			}
		}
	} else if (b) {
		int sum = 1;
		pq.emplace(a[1].second + 1, 1);
		for (int i = 2; i <= n; i++) {
			while (!pq.empty() && pq.top().first < i) {
				sum = (sum + m - pq.top().second) % m;
				pq.pop();
			}
			dp[i] = sum;
			sum = (sum + dp[i]) % m;
			pq.emplace(i + a[i].second, dp[i]);
		}
	} else {
		vector<int> v;
		unordered_map<int, int> tmp;
		tmp[a[1].first] = 0;
		v.push_back(a[1].first);
		rem[0][1 % a[1].first] = 1;
		pq.emplace(a[1].second * a[1].first + 1, 1);
		for (int i = 2; i <= n; i++) {
			while (!pq.empty() && pq.top().first < i) {
				int idx = pq.top().second;
				int k = tmp[a[idx].first];
				int r = idx % a[idx].first;
				rem[k][r] = (rem[k][r] - dp[idx] + m) % m;
				pq.pop();
			}
			
			for (size_t j = 0; j < v.size(); j++) {
				int r = i % v[j];
				dp[i] = (dp[i] + rem[j][r]) % m;
			}
			if (a[i].first > 0 && a[i].second > 0) {
				if (tmp.find(a[i].first) == tmp.end()) {
					tmp[a[i].first] = v.size();
					v.push_back(a[i].first);
				}
				int idx = tmp[a[i].first];
				int r = i % a[i].first;
				rem[idx][r] = (rem[idx][r] + dp[i]) % m;
				pq.emplace(a[i].second * a[i].first + i, i);
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = (ans + dp[i]) % m;
	cout << ans;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |