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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include<algorithm>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define FORIT(it, m) for((it) = (m).begin(); (it) != (m).end(); ++(it))
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
using namespace std;
int debug=0;
#define dcout debug && cout
#define E107 1000000007
ull ccc=0;
class H
{
	public:
	int n;
	int height;
	int**tree;
	int*lengths;
	int (*agregator)(int, int);
	H(int n, int*base, int (*agregator)(int, int)):n(n),agregator(agregator){
		height = calculateHeight(n);
		lengths = calculateLengths(n,height);
		tree = new int*[height];
		tree[0]=base;
		FORI(i,1,height){
			tree[i]=new int[lengths[i]];
			FOR(j,lengths[i]){
				////dcout<<"take ("<<i-1<<","<<2*j<<") and ("<<i-1<<","<<min(2*j+1,lengths[i-1])<<")"<<endl;
				tree[i][j]=agregator(tree[i-1][2*j],tree[i-1][safeRange(i-1,2*j+1)]);
			}
		}
	}
	int getAgregated(int from, int to){
		//if(from!=3 || to!=4)debug=0;else debug=1;
		int mx=agregator(tree[0][from],tree[0][to]);
		int currentLevel=0;
		////dcout<<"level:"<<currentLevel<<",from:"<<from<<",to:"<<to<<",mx:"<<mx<<","<<endl;
		while(to!=from && from+1!=to){
			if((from&1)==0){
				mx = agregator(mx, tree[currentLevel][safeRange(currentLevel,from+1)] );
			}
			from/=2;
			if( to&1 ){
				mx = agregator(mx, tree[currentLevel][to-1] );
			}
			to/=2;
			++currentLevel;
			////dcout<<"level:"<<currentLevel<<",from:"<<from<<",to:"<<to<<",mx:"<<mx<<","<<endl;
		}
		return mx;
	}
	int findRightMostIndexWithValueLessOrEqualThan(int x){
		////dcout<<"Find not more that "<<x<<endl;
		if(x<tree[height-1][0]){
			//nie ma żadnych
			return -1;
		}
		////dcout<<"There is someting="<<tree[height-1][0]<<" less or equal than x="<<x<<endl;
		int headLevel=height-1;
		int headIndex = 0;
		while(headLevel > 0){
			--headLevel;
			//try to right node
			int rightChild = headIndex*2+1;
			if(rightChild<lengths[headLevel]){
				////dcout<<"I have "<<tree[headLevel][rightChild]<<" in right child at "<<rightChild<<", level="<<headLevel<<endl;
				if(x>=tree[headLevel][rightChild]){
					////dcout<<"I go right"<<endl;
					headIndex=rightChild;
					continue;
				}
			}
			////dcout<<"I go left"<<endl;
			int leftChild=2*headIndex;
			headIndex=leftChild;
		}
		return headIndex;
	}
	void update(int index, int value){
		//dcout<<"Tree update element "<<index<<" to "<<value<<endl;
		tree[0][index]=value;
		FORI(i,1,height){
			index/=2;
			int newValue = agregator(tree[i-1][2*index],get(i-1,2*index+1));
			if(newValue == tree[i][index])break;
			tree[i][index]=newValue;
		}
	}
	int get(int level, int index){
		return tree[level][safeRange(level,index)];
	}
	int safeRange(int level, int index){
		return min(index, lengths[level]-1);
	}
	int calculateHeight(int n){
		int i=1,higher;
		//dcout<<"FOR n="<<n;
		while(n>1){
			++i;
			n = (n-1)/2+1;
		}
		//dcout<<" computed height "<<i<<endl;
		return i;
	}
	int*calculateLengths(int n, int height){
		int*r=new int[height];
		r[0]=n;
		FORI(i,1,height){
			r[i]=(r[i-1]-1)/2+1;
		}
		return r;
	}
};
ostream& operator<<(ostream& os, const H& r)
{
	FOR(i,r.height){
		FOR(k,(1<<i)-1)os<<" ";
		FOR(j,r.lengths[i]){
			os<<r.tree[i][j];
			FOR(k,(1<<(i+1))-1)os<<" ";
		}
		os<<endl;
	}
	return os;
}
int minint(int a,int b){
	return min(a,b);
}
int maxint(int a,int b){
	return max(a,b);
}
class S
{
	public:
		S(int s,int d):start(s),validFromBoundry(d){}
	int start;
	int validFromBoundry;
};
ostream& operator<<(ostream& os, const S& r)
{
	os<<"[start:"<<r.start<<", validFromBoundry:"<<r.validFromBoundry<<"]";
	return os;
}
struct SCMP
{
	int operator()(const S a,const S b){
		return a.validFromBoundry > b.validFromBoundry;
	}
};
int main(){
	ios_base::sync_with_stdio(0);
	int n;
	cin>>n;
	int n1=n+1;
	int*mns= new int[n];
	int*mxs= new int[n];
	int*groupsTill=new int[n1];
	int*combinationsTill=new int[n1];
	int*verticalGroupsArray=new int[n];
	FOR(i,n){
		cin>>mns[i];
		cin>>mxs[i];
		groupsTill[i]=0;
		combinationsTill[i]=0;
		verticalGroupsArray[i]=n1;
	}
	groupsTill[n]=0;
	combinationsTill[n]=0;
	verticalGroupsArray[0]=0;
	combinationsTill[0]=1;
	H minimums(n,mns,maxint);
	H maximums(n,mxs,minint);
	H verticalGroupsTree(n,verticalGroupsArray,minint);
	//FOR(i,n){
	//	FORI(j,i,n){
	//		//dcout<<"max("<<i<<","<<j<<")="<<minimums.getMax(i,j)<<endl;
	//	}
	//}
	////dcout<<minimums.getMax(3,4)<<endl;
	map<int, priority_queue<S,vector<S>, SCMP> > m;
	m[0].push(S(0,0));
	ull oldccc=0;
	FOR(i,n){
		//dcout<<verticalGroupsTree;
		int success=0;
		int boundry=i+1;
		int combinations=0;
		while(!success){
			int groupsBranch = verticalGroupsTree.findRightMostIndexWithValueLessOrEqualThan(boundry);
			//if(i==5808)dcout<<"Let's "<<i<<" element at "<<boundry<<" boundry look to join with a boundry with "<<groupsBranch<<" groups"<<endl;
			//if(i==5808)dcout<<"First candidate is "<<m[groupsBranch].top()<<endl;
			if(groupsBranch==-1)break;
			vector<S> addBack;
			while(!m[groupsBranch].empty() && m[groupsBranch].top().validFromBoundry <= boundry){//if(i==54)return 0;
				++ccc;//if(ccc>20000000)return 0;
				S top = m[groupsBranch].top();
				//dcout<<"We have a candidate to join with: "<<top<<endl;
				m[groupsBranch].pop();
				int minInThisRange = minimums.getAgregated(top.start, i);
				int quantityInThisRange = i-top.start+1;
				//dcout<<"I try to join it to boundry in "<<top.start<<", so new group is from "<<top.start<<" to "<<boundry<<" boundries, that includes elements from "<<top.start<<" to "<<i<<" inclusive. ";
				//dcout<<"Their required minimum is "<<minInThisRange<<endl;
				if(minInThisRange<=quantityInThisRange){
					int maxInThisRange = maximums.getAgregated(top.start, i);
					if(maxInThisRange>=quantityInThisRange){
						//jest!
						//if(i==5808)dcout<<"We join to "<<top<<", so +"<<combinationsTill[top.start]<<" combinations"<<endl;
						combinations+=combinationsTill[top.start];
						combinations%=E107;
						success=1;
						//dodaj z powrotem
						addBack.push_back(top);
					}else{
						//if(i==5808)dcout<<"We don't join "<<top<<""<<endl;
						//czyli usuń z kolejki
					}
				}else{
					//if(i==5808)dcout<<"Let's put it back for later "<<top<<" -> ";
					top.validFromBoundry=minInThisRange+top.start;
					//if(i==5808)dcout<<top<<endl;
					m[groupsBranch].push(top);
				}
			}
			//dcout<<"Was the queue empty? "<<m[groupsBranch].empty();
			//if(!m[groupsBranch].empty())dcout<<" i had to be at list at "<<m[groupsBranch].top().validFromBoundry<<" boundry";
			//dcout<<endl;
			if(success){
				//czyli, że coś znalazł i teraz trzeba to z powrotem dorzucić do kolejki
				vector<S>::iterator it=addBack.begin();
				vector<S>::iterator ite=addBack.end();
				for(;it!=ite;++it){
					m[groupsBranch].push(*it);
				}
				groupsTill[boundry]=groupsBranch+1;
				combinationsTill[boundry]=combinations;
				//wstaw
				//dcout<<"And we insert our new member to "<<groupsBranch+1<<" branch with "<<S(boundry,0)<<endl;
				m[groupsBranch+1].push(S(boundry,0));//zmień zero na minimum
				//dcout<<"So now it's top is at "<<m[groupsBranch+1].top()<<endl;
				verticalGroupsTree.update(groupsBranch+1, m[groupsBranch+1].top().validFromBoundry);
			}
			//update drzewa pionowego
			if(m[groupsBranch].empty()){
				verticalGroupsTree.update(groupsBranch, n1);
			}else{
				verticalGroupsTree.update(groupsBranch, m[groupsBranch].top().validFromBoundry);
			}
		}
		//if(debug)FOR(j,n1)dcout<<groupsTill[j]<<" ";//dcout<<endl;
		//if(debug)FOR(j,n1)dcout<<combinationsTill[j]<<" ";//dcout<<endl;
			//if(i==5808)dcout<<"We move on. Max groups after item "<<i<<" is "<<groupsTill[i+1]<<" with "<<combinationsTill[i+1]<<" combinations. So far used "<<ccc-oldccc<<" iterations"<<endl;
			//oldccc=ccc;
			//if(i%10000==0)cout<<i<<" "<<ccc<<endl;
			//if(i==5808)return 0;
	}
	if(groupsTill[n]){
		cout<<groupsTill[n]<<" "<<combinationsTill[n]<<endl;
	}else{
		cout<<"NIE"<<endl;
	}

	return 0;
}