#include using namespace std; typedef long long ll; #define pb push_back #define fi first #define se second typedef pair P; using VP = vector

; using VVP = vector; using VI = vector; using VVI = vector; using VVVI = vector; const int inf=1e9+7; const ll INF=1LL<<61; const ll mod=1e9+7; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } vector inv,fact,invfact; void Mod_build(int n=101010){ fact.resize(n+1); inv.resize(n+1); invfact.resize(n+1); fact[0]=inv[0]=invfact[0]=1; inv[1]=1; for(ll i=0;i0)inv[i+1]=mod-inv[mod%(i+1)]*(mod/(i+1))%mod; invfact[i+1]=invfact[i]*inv[i+1]%mod; } } ll perm(int n,int k){ if(n<0||k<0||k>n)return 0; return fact[n]*invfact[n-k]%mod; } ll comb(int n,int k){ if(n<0||k<0||k>n)return 0; return (fact[n]*invfact[n-k]%mod)*invfact[k]%mod; } ll powmod(ll n,ll k){ k%=mod-1; if(k<0)k+=mod-1; ll ret=1; while(k){ if(k&1)ret=ret*n%mod; n=n*n%mod; k>>=1; } return ret; } int main(){ int i,j; int n; cin>>n; VI v(n+1,0),w(n+1,0); for(i=0;i>t>>x; if(t) v[x]++; else w[x]++; } Mod_build(); ll ok=0; ll fin=0; VVI dp(n+1,VI(n+1,0)); dp[0][0]=1; for(i=0;i=0&&i-j+fin>=0&&ok-i+j+fin>=0) { dp[i+1][j-w[i+1]] += (fact[j]*invfact[j-w[i+1]]%mod)*((ok-i+j+fin)*dp[i][j]%mod)%mod; //i+1 in v dp[i+1][j-w[i+1]]%mod; } if(j+1-w[i+1]>=0) { dp[i+1][j+1-w[i+1]] += (fact[j+1]*invfact[j+1-w[i+1]]%mod)*(dp[i][j]%mod)%mod; //i+1 in w dp[i+1][j+1-w[i+1]]%mod; } } fin += w[i+1]; } cout<