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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

int n;
VI c,d;

void readData(){
    scanf("%d",&n);
    c.resize(n); d.resize(n);
    REP(i,n) scanf("%d%d",&(c[i]),&(d[i]));
}


class IntervalTree{
public:
    VI T;
    int M;

    IntervalTree(int n){
        int pow = 32-__builtin_clz(n);
        M = 1 << pow; // 0 is empty
        T.resize(2*M,0);
    }

    IntervalTree(VI &v,bool neg=false) {
        int n = SIZE(v);
        int pow = 32-__builtin_clz(n);
        M = 1 << pow; // 0 is empty
        T.resize(2*M,0);
        if (!neg) 
            REP(i,n) T[M+i] = v[i];
        else
            REP(i,n) T[M+i] = -v[i];
        for(int i=M-1; i >= 1; i--)
            T[i] = max(T[2*i],T[2*i+1]);
    } 

    void clear(){ T = VI(2*M,0);}

    void maxUpdate(int ind, int val){
        int v = M+ind;
        T[v] = val;
        while(v != 1){
            v /= 2;
            T[v] = max(T[2 * v],T[2 * v + 1]);
        }
    }

    void minUpdate(int ind, int val){
        maxUpdate(ind,-val);
    }

    int maxQuery(int ind1, int ind2){
        int v1 = M + ind1, v2 = M + ind2;
        int res = (v1 == v2) ? T[v1] : max(T[v1],T[v2]);
        while (v1 / 2 != v2 / 2) {
            if (v1 % 2 == 0) res = max(res,T[v1 + 1]);
            if (v2 % 2 == 1) res = max(res,T[v2 - 1]);
            v1 /= 2; v2 /= 2;
        }
        return res;
    }

    int minQuery(int ind1, int ind2){
        return -maxQuery(ind1,ind2);
    }
};

#define MOD 1000000007
#define ADD(a,b) (((a) + (b)) % MOD)

II solve(){
    IntervalTree minTree(d,true);
    IntervalTree maxTree(c);
    VII dp(n+1,MP(0,1));
    REP(i,n){
        int best = 0, cnt = 0;
        int len = c[i];
        int start = i-(len-1);
        int ok = false;
        int l, u;
        while (start >= 0){
            l = maxTree.maxQuery(start,i);
            u = minTree.minQuery(start,i);
            if (l <= len && len <= u){
                ok = true;
                start = i-(l-1);
                break;
            } else if (l > len){
                len = l;
                start = i-(len-1);
            } else 
                break;
        }
        if (!ok){ dp[i+1] = MP(0,0); continue;}

        int pen = 0;
        while(1){
            if (len >= l){
                if (dp[start].X > best){
                    best = dp[start].X;
                    cnt = dp[start].Y;
                    pen = 0;
                } else if (dp[start].X == best){
                    cnt = ADD(cnt,dp[start].Y);
                    pen = 0;
                } else if (++pen == 50)
                    break;
            }
            start--; len++;
            if (start < 0) break;
            l = max(l,c[start]);
            u = min(u,d[start]);
            if (len > u)
                break;
        } 

        dp[i+1] = MP(best+1,cnt);
    }
    return dp[n];
}

II solve_brute(){
    VII dp(n+1,MP(0,1)); // groups/ways
    REP(i,n){
        int best = 0, cnt = 0;
        FOR(g,c[i],d[i]+1){
            int start = i-(g-1);
            if (start < 0)
                break;
            bool ok = true;
            REP(j,g) if( c[i-j] > g || d[i-j] < g){ ok = false; break;}
            if (!ok) continue;
            if (dp[start].X > best){
                best = dp[start].X;
                cnt = dp[start].Y;
            } else if (dp[start].X == best)
                cnt = ADD(cnt,dp[start].Y);
        } 
        dp[i+1] = MP(best+1,cnt);
    }
    return dp[n];
}

int main(int argc, char *argv[]){
    readData();
    II res = (n <= 50000)?solve_brute():solve();
    if (res.Y == 0)
        printf("NIE\n");
    else
        printf("%d %d\n",res.X,res.Y);
    return 0;
}