#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b>n; vector> v(n); ll cnt=0; rep(i,n){ cin>>v[i].second>>v[i].first; if(v[i].second==1) v[i].first--,cnt++; } sort(ALL(v)); dp[0][0]=1; rep(i,n){ for(int j=0;j<=i;j++){ dp[i+1][j]+=dp[i][j]*max(v[i].first-(i-j),0ll); dp[i+1][j]%=mod; if(v[i].second){ dp[i+1][j+1]+=dp[i][j]; dp[i+1][j+1]%=mod; } } } ll ans=0; rep(i,cnt+1){ if(i%2==0) ans+=dp[n][cnt-i]*fact[cnt-i]%mod; else ans+=mod-dp[n][cnt-i]*fact[cnt-i]%mod; ans%=mod; } // rep(i,n+1){ // rep(j,n+1){ // cout<