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
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;
const int P = 1000000007;

struct Problem {
	int n;
	VI c,d;
	VPII r;
	Problem(int n) : n(n), c(n), d(n), r(n+1, PII(-1,0)) { }
	PII Go();
};

PII Problem::Go()
{
	r[n] = PII(0, 1);
	FORD(i,n-1,0) {
		int cc = 0, dd = INF;
		FOR(j,i,n-1) {
			cc = max(cc, c[j]);
			dd = min(dd, d[j]);
			if (cc > dd) break;
			if (j-i+1 >= cc) {
				if (j-i+1 <= dd) {
					if (r[j+1].first != -1) {
						if (r[i].first == -1 || r[j+1].first + 1 > r[i].first) {
							r[i] = r[j+1];
							r[i].first++;
						}
						else if (r[j+1].first + 1 == r[i].first) r[i].second = (r[i].second + r[j+1].second) % P;
					}
				}
				else break;
			}
		}
	}
	return r[0];
}

int main(int argc, char** argv)
{
	int n;
	scanf("%d", &n);
	Problem p(n);
	REP(i,n) scanf("%d%d", &p.c[i], &p.d[i]);
	PII res = p.Go();
	if (res.first == -1) printf("NIE\n");
	else printf("%d %d\n", res.first, res.second);

#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}