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
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <deque>

#define REP(i, n) for (int i = 0; i < (n); ++i) 
#define FOR(i, a, b) for (int i = (a); i < (b); ++i) 

using namespace std;

const int MAX_N = 1000100;
const long long MOD = 1000000007;
int n;
int dmax[MAX_N], dmin[MAX_N];
long long d[MAX_N];
int num_teams[MAX_N];
int stop[MAX_N];

int main() {
    scanf("%d", &n);
    REP(i, n) scanf("%d %d", &dmin[i], &dmax[i]);
    REP(i, n) d[i] = 0;
    REP(i, n) num_teams[i] = -1;
    REP(i, n) stop[i] = 0;

    d[MAX_N-1] = 1;
    num_teams[MAX_N-1] = 0;

    // (kiedy_wypada, pozycja)
    set<pair<int, int> > maxy;

    REP(end, n) {
        // SLOW SLOW SLOW
        int need = 0;
        int need_max = MAX_N;
        int limit = 0;
        for (int i = end; i >= limit; i--) {
            need = max(need, dmin[i]);
            need_max = min(need_max, dmax[i]);

            if (need_max < end - i + 1) break;

            if (need <= end - i + 1) {
                int idx = (i == 0 ? (MAX_N-1) : (i-1));
                if (num_teams[idx] == -1) {
                    // pass
                } else if (num_teams[idx] + 1 > num_teams[end]) {
                    num_teams[end] = num_teams[idx] + 1;
                    d[end] = d[idx];
                    limit = stop[num_teams[end]-1];
                } else if (num_teams[idx] + 1 == num_teams[end]) {
                    d[end] += d[idx];
                }
            }
        }
        d[end] %= MOD;
        stop[num_teams[end]] = min(stop[num_teams[end]], end);
    }

    if (num_teams[n-1] == -1) {
        printf("NIE\n");
    } else {
        printf("%d %lld\n", num_teams[n-1], d[n-1] % MOD);
    }

    return 0;
}