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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MODULO 1000000007
#define MAXN (1<<20)

typedef long long slng;
#define VDEBUG(...) 
#define DEBUG(...) 
//#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout)
//#define VDEBUG(...) printf(__VA_ARGS__); fflush(stdout)

int N,C[MAXN],D[MAXN];

#define PARENT(i) ((i-1)/2)
#define LEFT(i) ((i) * 2 + 1)
#define RIGHT(i) ((i) * 2 + 2)
#define MAX(a,b) ((a > b) ? (a) : (b))
#define MIN(a,b) ((a < b) ? (a) : (b))
int rbase;
int c[MAXN], d[MAXN];
int sumn[2*MAXN];
int maxn[2*MAXN];
int maxc[2*MAXN];
int mind[2*MAXN];


void init_rangetree() {
	int i;
	for (rbase = 1; rbase <= N; rbase *= 2);
	rbase--;
	
	/*for(i = 0; i < N; i++)
		scanf("%d %d", &maxc[rbase + i], &mind[rbase + i]);
	for (i = rbase - 1; i >= 0; i--) {
		maxc[i] = MAX(maxc[LEFT(i)], maxc[RIGHT(i)]);
		mind[i] = MIN(mind[LEFT(i)], mind[RIGHT(i)]);
	}*/
	for(i = 0; i <= 2 * rbase; i++)
		mind[i] = MAXN;
}

void range_set_n(int pos, int n, slng sn) {
	int i;
	maxn[rbase + pos] = n;
	sumn[rbase + pos] = sn;
	VDEBUG("maxn[%d] = %d sumn[%d] = %d\n", rbase + pos, maxn[rbase + pos], rbase + pos, sumn[rbase + pos]);
	for (i = PARENT(rbase + pos); PARENT(i) != i; i = PARENT(i)) {
		maxn[i] = MAX(maxn[LEFT(i)], maxn[RIGHT(i)]);
		if (maxn[LEFT(i)] == maxn[RIGHT(i)]) {
			sumn[i] = (sumn[LEFT(i)] + sumn[RIGHT(i)]) % MODULO;
		} else if (maxn[LEFT(i)] > maxn[RIGHT(i)]) {
			sumn[i] = sumn[LEFT(i)];
		} else {
			sumn[i] = sumn[RIGHT(i)];
		}
		VDEBUG("maxn[%d] = %d sumn[%d] = %d\n", i, maxn[i], i, sumn[i]);
	}
}

void range_set_cd(int pos, int c, int d) {
	int i;
	maxc[rbase + pos] = c;
	mind[rbase + pos] = d;
	for (i = PARENT(rbase + pos); PARENT(i) != i; i = PARENT(i)) {
		maxc[i] = MAX(maxc[LEFT(i)], maxc[RIGHT(i)]);
		mind[i] = MIN(mind[LEFT(i)], mind[RIGHT(i)]);
	}
}

void _range_get_n(int a, int b, int i, int ra, int rb, int end, int *n, int *sn) {
	if (a > b) {
		*n = 0;
		*sn = 0;
		return;
	}
	VDEBUG("[%d %d] (end=%d): maxn=%d sumn=%d maxc=%d mind=%d\n", ra, rb, end, maxn[i], sumn[i], maxc[i], mind[i]);
	
	if (a > rb || b < ra) {
		*n = 0;
		*sn = 0;
		VDEBUG("-> EMPTY\n");
		return;
	}
	if (ra >= a && rb <= b) {
		/* For all x in [ra, rb], [x, end] has to fit in [c, d]. */
		if (end - rb + 1 >= maxc[i] && end - ra + 1 <= mind[i]) {
			*n = maxn[i];
			*sn = sumn[i];
			VDEBUG("-> COVERED\n");
			return;
		} else if (ra == rb) {
			/* One element range that doesn't fit. */
			*n = 0;
			*sn = 0;
			VDEBUG("-> EMPTY\n");
			return;
		}
	}
	int ln = 0, lsn = 0, rn = 0, rsn = 0;
	/* always go right. */
	_range_get_n(a, b, RIGHT(i), (ra + rb)/2 + 1, rb, end, &rn, &rsn);
	
	/* maxc and mind from right influences left. */
	int rc = maxc[RIGHT(i)], rd = mind[RIGHT(i)];
	b = MIN(b, end - rc + 1);
	a = MAX(a, end - rd + 1);
	_range_get_n(a, b, LEFT(i), ra, (ra + rb)/2, end, &ln, &lsn);
	
	if (ln == rn) {
		*n = ln;
		*sn = (lsn + rsn) % MODULO;
	} else if (ln > rn) {
		*n = ln;
		*sn = lsn;
	} else {
		*n = rn;
		*sn = rsn;
	}
}

int range_get_n(int a, int b, int *n, int *sn) {
	_range_get_n(a, b, 0, 0, rbase, b, n, sn);
}

int main() {
	int i;
	int n, sn;
	
	scanf("%d", &N);
	for(i = 0; i < N; i++)
		scanf("%d %d", &c[i], &d[i]);
	init_rangetree();
	range_set_n(0, 0, 1); /* only point with n = 0 and sn > 0 */
	for (i = 0; i < N; i++) {
		DEBUG("c[%d]=%d d[%d]=%d\n", i, c[i], i, d[i]);
		range_set_cd(i, c[i], d[i]);
		range_get_n(0, i, &n, &sn);
		range_set_n(i + 1, (n > 0 /* odd chance that sn % MODULO == 0 */ || sn > 0) ? n + 1 : 0, sn);
		DEBUG("n,sn[%d] = %d, %d\n", i + 1, maxn[rbase + i + 1], sumn[rbase + i + 1]);
	}
	n = maxn[rbase + N];
	sn = sumn[rbase + N];
	if (sn == 0) {
		printf("NIE\n");
	} else {
		printf("%d %d\n", n, sn);
	}
	return 0;
}