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

#define MODULO 1000000007LL

struct Option {
	long long int optionsCount;
	int teams;
	int curLen;
	int canAdd;
	int mustAdd;
};

void addFirst(queue<Option>& options, int c, int d) {
	Option newOne;
	newOne.teams = 0;
	newOne.optionsCount = 1;
	newOne.curLen = 1;
	newOne.canAdd = d-1;
	newOne.mustAdd = c-1;

	options.push(newOne);
}

void updateOptions(queue<Option>& options, int c, int d) {
	Option nowClosed;
	nowClosed.teams = -1;

	queue<Option> newQueue;

	Option current;
	while (!options.empty()) {
		current = options.front();
		options.pop();

		if (current.mustAdd == 0) {
			current.teams++;
			if (nowClosed.teams < current.teams) {
				nowClosed = current;
			} else if (nowClosed.teams == current.teams) {
				nowClosed.optionsCount = (nowClosed.optionsCount + current.optionsCount) % MODULO;
			}
			current.teams--;
		}

		if (current.canAdd > 0) {
			current.canAdd--;
			if (current.mustAdd > 0) {
				current.mustAdd--;
			}
			current.curLen++;
			if (current.curLen <= d) {
				current.canAdd = min(current.canAdd, d - current.curLen);
				current.mustAdd = max(current.mustAdd, c - current.curLen);
				newQueue.push(current);
			}
		}
	}

	if (nowClosed.teams > 0) {
		nowClosed.canAdd = d-1;
		nowClosed.curLen = 1;
		nowClosed.mustAdd = c-1;
		options.push(nowClosed);
	}

	while (!newQueue.empty()) {
		options.push(newQueue.front());
		newQueue.pop();
	}
}

void closeOptions(queue<Option>& options) {
	Option final;
	final.teams = -1;

	Option current;
	while (!options.empty()) {
		current = options.front();
		options.pop();

		if (current.mustAdd == 0) {
			current.canAdd = 0;
			current.teams++;
			if (final.teams < current.teams) {
				final = current;
			} else if (final.teams == current.teams) {
				final.optionsCount = (final.optionsCount + current.optionsCount) % MODULO;
			}
		}
	}

	if (final.teams > 0) {
		options.push(final);
	}
}

int main() {
	int n;
	int c, d;

	vector<pair<int, int> > input;
	queue<Option> options;
	scanf("%d", &n);

	int maxC = 1;

	for (int i = 0; i < n; ++i) {
		scanf("%d %d", &c, &d);
		maxC = max(maxC, c);
		input.push_back(make_pair(c, d));
	}

	addFirst(options, input[0].first, min(input[0].second, 2*maxC));

	for (int i = 1; i < n; ++i) {
		updateOptions(options, input[i].first, min(input[i].second, 2*maxC));
		if (options.empty()) {
			break;
		}
	}

	if (!options.empty()) {
		closeOptions(options);
	}

	if (options.empty()) {
		printf("NIE\n");
	} else {
		Option o = options.front();
		printf("%d %lld", o.teams, o.optionsCount);
	}

	return 0;
}