import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): md = 10 ** 9 + 7 n = II() xt = [] one = 0 for _ in range(n): t, x = MI() if t: x -= 1 one += 1 xt.append((x, t)) tt = [] xx = [] for x, t in sorted(xt): tt.append(t) xx.append(x) #print(tt) #print(xx) # dp[i][j]...i個まで見たとき、t=1の処理をj個飛ばしたときの場合の数 dp = [[0] * (one + 1) for _ in range(n + 1)] dp[0][0] = 1 for i in range(n): for j in range(min(i, one) + 1): pre = dp[i][j] if pre == 0: continue # 処理する dp[i + 1][j] = (dp[i + 1][j]+(xx[i] - i + j) * pre)%md # 処理しない if tt[i]: dp[i + 1][j + 1] = (pre+dp[i + 1][j + 1])%md #p2D(dp) fac=[1] for i in range(1,one+1):fac.append(fac[-1]*i%md) ans=0 for j in range(one+1): if (one-j)&1:ans-=dp[n][j]*fac[j] else:ans+=dp[n][j]*fac[j] ans%=md print(ans) main()