P = 10**9 + 7 n = int(input()) rs: list[tuple[int, int]] = [] for _ in range(n): t, x = map(int, input().split()) if t == 1: rs.append((x - 1, 1)) else: rs.append((x, 0)) rs.sort() pd = [1] for i, (r, f) in enumerate(rs): print(i, r, f) dp = [0] * (i + 2) for j in range(i + 1): num = r - j if f == 1: dp[j + 1] -= pd[j] * num % P dp[j] += pd[j] else: dp[j + 1] += pd[j] * num % P pd = [e % P for e in dp] ans = 0 fac = 1 for j in reversed(range(n + 1)): ans += pd[j] * fac % P fac = fac * (n - j + 1) % P print(ans % P)