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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <ctime>
#include <cstring>
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define FOREACH(i,t) FORE(i,t)
#define REMIN(x,y) (x)=min((x),(y))
#define REMAX(x,y) (x)=max((x),(y))
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define IN(x,y) ((y).find((x))!=(y).end())
#define un(v) v.erase(unique(ALL(v)),v.end())
#define LOLDBG
#ifdef LOLDBG
#define DBG(vari) cerr<<#vari<<" = "<<vari<<endl;
#define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl;
#else
#define DBG(vari)
#define DBG2(v1,v2)
#endif
#define CZ(x) scanf("%d",&(x));
#define CZ2(x,y) scanf("%d%d",&(x),&(y));
#define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z));
#define ALL(x) (x).begin(),(x).end()
#define tests int dsdsf;cin>>dsdsf;while(dsdsf--)
#define testss int dsdsf;CZ(dsdsf);while(dsdsf--)
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; }
#define INF (1<<20)
#define MOD 1000000007
int n;
int c[1<<20],d[1<<20];


int ile[1<<20];
int dp[1<<20];

int pre[1<<20];
int nex[1<<20];
bool act[1<<20];


namespace drzewo
{
    int M;
    int d[1<<21];
    void init(int wielkosc)
    {
        M=1;
        while(wielkosc+3>=M) M*=2;
        FOR(i,2*M)d[i]=-INF;
    }
    void ustaw(int pos,int val)
    {
        d[pos+M]=val;
        int g=(pos+M)/2;
        while(g)
		{
			d[g]=max(d[g*2],d[g*2+1]);
			g/=2;
		}
    }
    int wez(){return d[1];}
}

vi ev[1<<20];
char g[1<<20];
int ways[1<<20];
int sumw[1<<20];
int sta;


void zmiana(int pos,char jak)
{
	if (pos<sta-1) return;
	if (jak=='X')
	{
		if (g[pos]!='X' && dp[pos]>=0)
		{
			ile[dp[pos]]+=1;
            sumw[dp[pos]]+=ways[pos];
            if (sumw[dp[pos]]>=MOD) sumw[dp[pos]]-=MOD;
            if (ile[dp[pos]]==1) drzewo::ustaw(dp[pos],dp[pos]);
		}
		g[pos]='X';
	}
	else
	{
		if (g[pos]=='X'&& dp[pos]>=0)
		{
			ile[dp[pos]]-=1;
			sumw[dp[pos]]-=ways[pos];
            if (sumw[dp[pos]]<0) sumw[dp[pos]]+=MOD;
            if (ile[dp[pos]]==0) drzewo::ustaw(dp[pos],-INF);
		}
		g[pos]='-';
	}

}






pii sol4()
{
	drzewo::init(n);
	int najw=0;

	deque<int> Q;
    dp[0]=0;
    FOR(i,n+2) ile[i]=0;
    FOR(i,n+2) sumw[i]=0;
    vi p;
    p.pb(0);

	FOR(j,n+1) g[j]=' ';
	g[n+1]=0;
	pre[0]=0;
	nex[0]=1;
	REP(i,1,n){pre[i]=i-1;nex[i]=i+1;}
	REP(i,1,n) ev[i].clear();
	vi imp;
	sta=1;
	ways[0]=1;
	int cnt=0;
	REP(i,1,n)
    {
        //if (i%1000==0) {DBG(i);DBG(imp.sz);DBG(cnt);}
    	g[i-1]='-';
    	dp[i]=-INF;
        act[i]=1;
        while(1)
        {
            int x=pre[i];
            if (!x) break;
            if (c[x]<=c[i])
			{
				int y=pre[x];
                REPD(k,i-c[x]-1,y)
                {
                    if (k<=i-c[i]-1) break;
                    //DBG(g[k]);
                    //g[k]='-';
                    cnt++;
                    zmiana(k,'-');
                }
				act[x]=0;
                nex[y]=i;
                pre[i]=y;
			}
			else break;
        }
        {
        	int y=pre[i];
        	int nr=max(i,y+c[i]);
        	if (nr<=n) ev[nr].pb(i);
        }
        FOR(j,ev[i].sz) imp.pb(ev[i][j]);
        vi nimp;
        FOR(j,imp.sz)
        {
            int x=imp[j];
            if (act[x])
			{
				int y=pre[x];
				if (i-c[x]>=y)
				{
					//DBG(g[i-c[x]]);
					//g[i-c[x]]='X';
					zmiana(i-c[x],'X');
					nimp.pb(x);
				}
				if (i-x==c[x])
				{
					act[x]=0;
					int z=nex[x];
					nex[y]=z;
					pre[z]=y;
				}

			}
        }

		imp=nimp;
		int oldsta=sta;
        while(Q.sz && d[Q.back()]>=d[i]) Q.pop_back();
        Q.push_back(i);
        while(1)
		{
            if (Q.front()<sta) Q.pop_front();
            if (i-sta+1>d[Q.front()])
			{
				if (g[sta-1]=='X') zmiana(sta-1,'-');
				sta++;
			}
            else break;
		}
		int mx=drzewo::wez();
		//REPD(j,najw,0) {cnt++;if (ile[j]) {mx=j;break;}}
		dp[i]=mx+1;
		if (dp[i]>0)
		{
			ways[i]=sumw[mx];
		}
		REMAX(najw,dp[i]);
    }

    if (dp[n]>0) return mp(dp[n],ways[n]);
    return mp(0,0);
}

int main()
{
	CZ(n);
	FOR(i,n)CZ2(c[i+1],d[i+1]);
	pii wyn=sol4();
	if (wyn.fi>0) printf("%d %d",wyn.fi,wyn.se);
	else puts("NIE");
	//DBG(brut());
	return 0;
}