#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include using namespace std; #include using namespace atcoder; #define REP(i,n) for(int i = 0; i < (int)n; i++) #define RREP(i,n) for(int i = (int)n-1; i >= 0; i--) #define LREP(i,n) for(LL i = 0; i < (LL)n; i++) #define Vi vector #define Vl vector #define P pair #define LP pair #define T3 tuple #define T4 tuple #define INF 1000000007 #define SIZE 1000100 #define MOD 1000000007 typedef long long LL; LL N; vector A; bool done[5000][5000]; LL memo[5000][5000]; LL dfs(LL i, LL j) { if (done[i][j]) return memo[i][j]; LL res = 0; if (i == N) { res = 1; for (LL k = j; k < N; k++) { res = (res * (N - k)) % MOD; } } else { if (A[i].second == 0) { res += ((A[i].first - j) * dfs(i + 1, j + 1)) % MOD; } else { res += dfs(i + 1, j) % MOD; res -= ((A[i].first - j) * dfs(i + 1, j + 1)) % MOD; } } res %= MOD; done[i][j] = true; return memo[i][j] = res; } int main() { cin >> N; REP(i, N) { LL t, x; cin >> t >> x; if (t == 1) x--; A.push_back(LP(x, t)); } sort(A.begin(), A.end()); cout << (MOD + dfs(0, 0)) % MOD << endl; }