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
//Krzysztof Pieprzak
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <map>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) typeof(n)v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);

const int    INF = 1000000001;
const double EPS = 10e-9;

const int MAXN = 1000005;
const int MOD = 1000000007;

pii t[MAXN];
pii res[MAXN];

pii sufMin[MAXN];
pii sufMax[MAXN];

void rob()
{
    int n;
    scanf("%d", &n);

    FOR (i, 0, n) scanf("%d %d", &t[i].F, &t[i].S);

    res[0] = t[0].F == 1 ? MP(1,1) : MP(0,1);
    sufMin[0] = MP(t[0].F,0);
    sufMax[0] = MP(t[0].S,0);
    int sufsMin = 0;
    int sufsMax = 0;
    int curSufMin = 0;
    int curSufMax = 0;
    FOR (i, 1, n)
    {
        //printf("res: ");FOR (j, 0, n) printf("(%d,%d) ", res[j].F, res[j].S);printf("\n");
        //printf("suf: ");FOR (j, 0, n) printf("(%d,%d) ", suf[j].F, suf[j].S);printf("\n\n");

        if (t[i].F == 1 && res[i-1].F) res[i] = MP(res[i-1].F+1,res[i-1].S);
        else if (t[i].F <= i+1 && t[i].S >= i+1 && sufMin[0].F <= i+1 && sufMax[0].F >= i+1) res[i] = MP(1,1);

        curSufMin = sufsMin;
        curSufMax = sufsMax;
        for (int j = min(i-t[i].F+1,i-1); j >= max(i-t[i].S+1,1); --j)
        {
            //printf("j=%d\n", j);
            if (curSufMax && sufMax[curSufMax-1].S == j) --curSufMax;
            if (curSufMin && sufMin[curSufMin-1].S == j) --curSufMin;

            if (sufMax[curSufMax].F < i-j+1) break;
            if (sufMin[curSufMin].F <= i-j+1)
            {
                if (res[j-1].F)
                {
                    if (res[j-1].F+1 == res[i].F) res[i].S = (res[i].S+res[j-1].S)%MOD;
                    else if (res[j-1].F+1 > res[i].F) res[i] = MP(res[j-1].F+1,res[j-1].S);
                }
            }
        }

        for (; sufsMin >= 0 && sufMin[sufsMin].F <= t[i].F; --sufsMin);
        sufMin[++sufsMin] = MP(t[i].F,i);

        for (; sufsMax >= 0 && sufMax[sufsMax].S >= t[i].S; --sufsMax);
        sufMax[++sufsMax] = MP(t[i].S,i);
    }

    if (res[n-1].F) printf("%d %d\n", res[n-1].F, res[n-1].S);
    else printf("NIE\n");
}

int main()
{
    int test = 1;
    //scanf("%d", &test);

    while (test--) rob();

    return 0;
}