import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10 ** 9 + 7 N = int(readline()) TX = np.array(read().split(), np.int32) T = TX[::2] X = TX[1::2] X[T == 1] -= 1 ind = X.argsort() X = X[ind].tolist() T = T[ind].tolist() dp = np.zeros(1, np.int64) dp[0] = 1 for t, x in zip(T, X): newdp = np.zeros(len(dp) + 1, np.int64) newdp[1:] = dp * np.arange(x, x - len(dp), -1) newdp[x + 1:] = 0 if t == 1: newdp[:-1] += dp newdp %= MOD dp = newdp fact = 1 for i in range(1, N + 1): fact *= i fact %= MOD dp[N - i] *= fact n = (N - sum(T)) & 1 dp[n ^ 1 ::2] *= - 1 dp %= MOD answer = dp.sum() % MOD print(answer)