Problem:
Genealogy of Puppets
The
Inventive and Creative Puppets Corporation (ICPC)
sells a variety of educational
puppets
for children, and also for grown-ups who were once children and still remember it. To celebrate
its centennial anniversary, ICPC has decided to offer for sale a collection of many of its most
popular puppets in its history of one hundred years, which will be a collectors' envy for sure.
Each puppet has a ring attached to its head, with which it can be hung below one of two
toes of another puppet. At most one puppet can be hung on one toe. As puppets do not feel
comfortable in handstand positions, loops of puppets should be avoided. A
tree
of puppets can
thus be made by hanging all the puppets to a toe of another puppet, leaving the ring of the
topmost puppet to be hung on the wall.
You are enthusiastic in ICPC puppets since childhood. You like imagining genealogies of the
puppets, assuming that puppets are hung on a parent puppet. You also imagine personalities
of puppets, and decided to obey the following rules on forming the tree:
each puppet has its own constraints on the number of children, that are its minimum and maximum, and
if a puppet has any children, at least one of them should have release date later than the parent.
Note that, if a puppet has two children, one of them may have its release date earlier than the
parent.
You want to write a program to calculate how many
different
trees can be made by the collection,
satisfying the rules. Two trees are considered different if any of the puppets are hung on different
parents, or hung on different toes of the same parent.
Input
The input consists of a single test case of the following format.
$n$
$x_1$ $y_1$
$\vdots$
$x_n$ $y_n$
The first line contains an integer $n$ ($2 \leq n \leq 300$), representing the number of puppets. The
$i$-th line of the following n lines describes the personality of a puppet. Lines are in the order
of the release dates of the puppets, from older to newer. Two integers in the line, $x_i$ and $y_i$,
are the minimum number and the maximum number of children of the puppet, respectively.
$0 \leq x_i \leq y_i \leq 2$ holds.
Output
Output a single integer in a line which is the number of the different trees satisfying the rules
modulo $10^9 + 7$.
Sample Input 1
3
0 1
1 2
0 2
Sample Output 1
6
Sample Input 2
2
0 2
1 2
Sample Output 2
0
For Sample Input 1, there are 6 possible trees satisfying the rules shown in Figure G.1.
For Sample Input 2, no trees can satisfy the rules.
Figure G.1. Trees satisfying the rules for Sample Input 1. The numbers on the puppets
        represent the release order.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=305,INF=15<<26;

//modint+畳み込み+逆元テーブル

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

#include <algorithm>
#include <array>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder



#include <utility>

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;
    
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
    
    unsigned int umod() const { return _m; }
    
    unsigned int mul(unsigned int a, unsigned int b) const {
        
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
        (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    for (long long a : {2, 7, 61}) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};
    
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;
    
    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b
        
        
        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
                           std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;
    
public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }
    
    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }
    
    unsigned int val() const { return _v; }
    
    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }
    
    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    
    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }
    
    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }
    
private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;
    
public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }
    
    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }
    
    unsigned int val() const { return _v; }
    
    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }
    
    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    
    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }
    
    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }
    
private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <type_traits>
#include <vector>

namespace atcoder {

namespace internal {

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);
    
    static bool first = true;
    static mint sum_e[30];  // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
    if (first) {
        first = false;
        mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for (int i = cnt2; i >= 2; i--) {
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for (int i = 0; i < cnt2 - 2; i++) {
            sum_e[i] = es[i] * now;
            now *= ies[i];
        }
    }
    for (int ph = 1; ph <= h; ph++) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint now = 1;
        for (int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for (int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p] * now;
                a[i + offset] = l + r;
                a[i + offset + p] = l - r;
            }
            now *= sum_e[bsf(~(unsigned int)(s))];
        }
    }
}

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);
    
    static bool first = true;
    static mint sum_ie[30];  // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
    if (first) {
        first = false;
        mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for (int i = cnt2; i >= 2; i--) {
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for (int i = 0; i < cnt2 - 2; i++) {
            sum_ie[i] = ies[i] * now;
            now *= es[i];
        }
    }
    
    for (int ph = h; ph >= 1; ph--) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint inow = 1;
        for (int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for (int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p];
                a[i + offset] = l + r;
                a[i + offset + p] =
                (unsigned long long)(mint::mod() + l.val() - r.val()) *
                inow.val();
            }
            inow *= sum_ie[bsf(~(unsigned int)(s))];
        }
    }
}

}  // namespace internal

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};
    if (std::min(n, m) <= 60) {
        if (n < m) {
            std::swap(n, m);
            std::swap(a, b);
        }
        std::vector<mint> ans(n + m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i + j] += a[i] * b[j];
            }
        }
        return ans;
    }
    int z = 1 << internal::ceil_pow2(n + m - 1);
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for (int i = 0; i < z; i++) {
        a[i] *= b[i];
    }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    mint iz = mint(z).inv();
    for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

template <unsigned int mod = 998244353,
class T,
std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};
    
    using mint = static_modint<mod>;
    std::vector<mint> a2(n), b2(m);
    for (int i = 0; i < n; i++) {
        a2[i] = mint(a[i]);
    }
    for (int i = 0; i < m; i++) {
        b2[i] = mint(b[i]);
    }
    auto c2 = convolution(move(a2), move(b2));
    std::vector<T> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        c[i] = c2[i].val();
    }
    return c;
}

std::vector<long long> convolution_ll(const std::vector<long long>& a,
                                      const std::vector<long long>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};
    
    static constexpr unsigned long long MOD1 = 754974721;  // 2^24
    static constexpr unsigned long long MOD2 = 167772161;  // 2^25
    static constexpr unsigned long long MOD3 = 469762049;  // 2^26
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
    
    static constexpr unsigned long long i1 =
    internal::inv_gcd(MOD2 * MOD3, MOD1).second;
    static constexpr unsigned long long i2 =
    internal::inv_gcd(MOD1 * MOD3, MOD2).second;
    static constexpr unsigned long long i3 =
    internal::inv_gcd(MOD1 * MOD2, MOD3).second;
    
    auto c1 = convolution<MOD1>(a, b);
    auto c2 = convolution<MOD2>(a, b);
    auto c3 = convolution<MOD3>(a, b);
    
    std::vector<long long> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        unsigned long long x = 0;
        x += (c1[i] * i1) % MOD1 * M2M3;
        x += (c2[i] * i2) % MOD2 * M1M3;
        x += (c3[i] * i3) % MOD3 * M1M2;
        long long diff =
        c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
        if (diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {
            0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }
    
    return c;
}

}  // namespace atcoder

using mint=atcoder::modint1000000007;

vector<mint> inv,fac,finv;

void make(){
    
    inv.resize(MAX);
    fac.resize(MAX);
    finv.resize(MAX);
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    
    for(int i=2;i<MAX;i++){
        inv[i]=-inv[mod%i]*(mod/i);
        fac[i]=fac[i-1]*i;
        finv[i]=finv[i-1]*inv[i];
    }
}

mint comb(ll a,ll b){
    if(a<b) return 0;
    return fac[a]*finv[b]*finv[a-b];
}

mint perm(ll a,ll b){
    if(a<b) return 0;
    return fac[a]*finv[a-b];
}

mint dp[MAX][MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    make();
    
    int N;cin>>N;
    vii A(N);
    for(int i=0;i<N;i++){
        cin>>A[i].fi>>A[i].se;
    }
    reverse(all(A));
    dp[0][0][0]=1;
    for(int i=0;i<N;i++){
        auto [l,r]=A[i];
        vi IN(3);
        for(int j=l;j<=r;j++) IN[j]=1;
        for(int a=0;a<=i;a++){
            for(int b=0;b<=i;b++){
                if(dp[i][a][b]==0) continue;
                //cout<<i<<" "<<a<<" "<<b<<" "<<dp[i][a][b].val()<<endl;
                
                if(IN[2]&&a>=2){
                    dp[i+1][a+1-2][b]+=dp[i][a][b]*perm(a,2);
                }
                if(IN[2]&&a>=1){
                    dp[i+1][a+1-1][b+1]+=dp[i][a][b]*perm(a,1)*2;
                }
                
                if(IN[1]&&a>=1){
                    dp[i+1][a+1-1][b]+=dp[i][a][b]*perm(a,1)*2;
                }
                
                if(IN[0]){
                    dp[i+1][a+1][b]+=dp[i][a][b];
                }
                
                if(b){
                    if(IN[2]&&a-1>=2){
                        dp[i+1][a-2][b-1]+=dp[i][a][b]*perm(a-1,2)*b;
                    }
                    if(IN[2]&&a-1>=1){
                        dp[i+1][a-1][b-1+1]+=dp[i][a][b]*perm(a-1,1)*2*b;
                    }
                    
                    if(IN[1]&&a-1>=1){
                        dp[i+1][a-1][b-1]+=dp[i][a][b]*perm(a-1,1)*2*b;
                    }
                    
                    if(IN[0]){
                        dp[i+1][a][b-1]+=dp[i][a][b]*b;
                    }
                }
            }
        }
    }
    
    cout<<dp[N][1][0].val()<<endl;
}
accuracy: 1
time_ms: 170
memory_kb: 114292
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void add(ll &a, ll b) {
    (a += b) %= MOD7;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector dp(n + 1, vector(n + 1, 0LL));
    dp[0][0] = 1;
    rep(_, n) {
        int le, ri;
        cin >> le >> ri;
        auto is_valid = [&](int x) -> bool {
            return le <= x and x <= ri;
        };
        vector nx(n + 1, vector(n + 1, 0LL));
        rep(j, n + 1) {
            rep(k, n + 1) {
                if(is_valid(0)) {
                    if(j) add(nx[j - 1][k], dp[j][k] * j);
                    if(k != n) add(nx[j][k + 1], dp[j][k]);
                }
                if(is_valid(1)) {
                    if(j != n and k != n) add(nx[j + 1][k + 1], dp[j][k] * 2);
                    add(nx[j][k], dp[j][k] * j * 2);
                }
                if(is_valid(2)) {
                    if(j != n) add(nx[j + 1][k], dp[j][k] * j);
                    if(j <= n - 2 and k < n) add(nx[j + 2][k + 1], dp[j][k]);
                    if(j < n) add(nx[j + 1][k], dp[j][k] * 2 * k);
                    if(k > 0) add(nx[j][k - 1], dp[j][k] * 2 * j * (k - 1));
                }
            }
        }
        swap(nx, dp);
    }
    cout << dp[0][1] << endl;
    return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 4668
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define foa(s, v)    for(auto& s : v)
#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n)    rng(i, 0, n)
#define sz(x)        int(x.size())
#define all(x)       begin(x), end(x)

int n;
constexpr ll mod = 1000000007;
void solve() {
	vector<pair<int, int>> lr(n);
	rep(i, n) cin >> lr[i].first >> lr[i].second;
	vector dp(n, vector(n + 1, vector(n + 1, 0ll)));
	{
		auto [l, r] = lr.back();
		if(l == 0) {
			dp[n - 1][1][0] = 1;
		}
	}
	for(int i = n - 2; i >= 0; --i) {
		auto [l, r] = lr[i];
		rep(j, n - i) {
			rep(k, n + 1) {
				auto now = dp[i + 1][j][k];
				if(now == 0) continue;
				// cout << i << " " << j << " " << k << " " << now << endl;
				rep(t, 3) {
					if(l <= t and t <= r) {
						if(t == 0) {
							//ある頂点の子にする場合
							if(k) {
								dp[i][j][k - 1] += now * k;
								dp[i][j][k - 1] %= mod;
							}
							//しない場合
							dp[i][j + 1][k] += now;
							dp[i][j + 1][k] %= mod;
						} else if(t == 1) {
							//ある頂点の子にする場合
							if(j >= 2 and k) {
								dp[i][j - 1][k - 1] += now * k * (j - 1) * 2;
								dp[i][j - 1][k - 1] %= mod;
							}
							//しない場合
							dp[i][j][k] += now * j * 2;
							dp[i][j][k] %= mod;
						} else {
							//ある頂点の子にする場合
							//二つ付ける
							if(j >= 3 and k) {
								dp[i][j - 2][k - 1] += now * k * (j - 1) * (j - 2);
								dp[i][j - 2][k - 1] %= mod;
							}
							//一つ付ける
							if(j >= 2) {
								dp[i][j - 1][k] += now * k * (j - 1) * 2;
								dp[i][j - 1][k] %= mod;
							}
							//しない場合
							//二つ付ける
							if(j >= 2) {
								dp[i][j - 1][k] += now * j * (j - 1);
								dp[i][j - 1][k] %= mod;
							}
							//一つ付ける
							if(j) {
								dp[i][j][k + 1] += now * j * 2;
								dp[i][j][k + 1] %= mod;
							}
						}
					}
				}
			}
		}
	}
	auto ans = dp[0][1][0];
	ans %= mod;
	if(ans < 0) ans += mod;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	while(cin >> n) solve();
}
accuracy: 1
time_ms: 130
memory_kb: 219008
final_rank: 13

code_snippet: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
#include <functional>
#include<unordered_map>
#include <random>
#include<bitset>
#include<cassert>
using namespace std;  
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define arr(x) (x).rbegin(),(x).rend()
typedef long long ll;
typedef unsigned long long ull;
const ll inf=1e18;  
using graph = vector<vector<int> > ;
using vi=vector<int>;
using P= pair<ll,ll>;  
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vll=vector<ll>; 
using vvll=vector<vll>;
using vvvll=vector<vvll>;
using vp=vector<P>;
using vvp=vector<vp>;
using vd=vector<double>;
using vvd =vector<vd>;

const int mod=1e9+7;


vll anss; 
ll dp[305][305];
ll old[305][305];

void op(ll &a,ll b){
    a+=b;
    a%=mod;
}
void solve(int test){
    int n; cin >> n;
    vi l(n),r(n);rep(i,n)cin >> l[i] >> r[i];
    auto isin=[&](int v,int i){
        return l[i]<=v && v<=r[i];
    };
    dp[0][0]=1;
    rep(i,n){
        rep(co,n+1)rep(deg,n+1){
            old[co][deg]=dp[co][deg];
            dp[co][deg]=0;
        }
        rep(co,n+1)rep(deg,n+1){
            ll now=old[co][deg];
            if(now==0)continue;
            {
               if(isin(0,i) && deg<=n)op(dp[co+1][deg],now);
               if(isin(1,i) && deg+1<=n)op(dp[co+1][deg+1],now*2);
               if(isin(2,i)){
                    if(deg+2<=n)op(dp[co+1][deg+2],now);
                    if(deg+1<=n)op(dp[co][deg+1],now*co*2);
               }
            }
            {
               if(isin(0,i) && deg-1>=0)op(dp[co][deg-1],now*deg);
               if(isin(1,i) && deg-1+1>=0)op(dp[co][deg-1+1],now*deg*2);
               if(isin(2,i)){
                    if(deg-1+2<=n)op(dp[co][deg-1+2],now*deg);
                    if(deg-1+1<=n && co>=2)op(dp[co-1][deg-1+1],now*deg*(co-1)*2);
               }
            }
        }
    }
    cout << dp[1][0] << endl;
}
//g++ cf.cpp -std=c++17 -I .  
int main(){cin.tie(0);ios::sync_with_stdio(false);
    int t=1;//cin >>t;
    rep(test,t)solve(test);
    for(auto ans:anss){
        cout<< ans << endl;
    }
}
accuracy: 1
time_ms: 120
memory_kb: 4920
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

template <unsigned mod = 1000000007> struct fp {
    unsigned v;
    static constexpr int get_mod() {
        return mod;
    }
    constexpr unsigned inv() const {
        assert(v != 0);
        int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;
        while (y > 0) {
            t = x / y;
            x -= t * y, p -= t * q;
            tmp = x, x = y, y = tmp;
            tmp = p, p = q, q = tmp;
        }
        if (p < 0)
            p += mod;
        return p;
    }
    constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    fp operator-() const {
        return fp() - *this;
    }
    fp pow(ull t) {
        fp res = 1, b = *this;
        while (t) {
            if (t & 1)
                res *= b;
            b *= b;
            t >>= 1;
        }
        return res;
    }
    fp &operator+=(const fp &x) {
        if ((v += x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator-=(const fp &x) {
        if ((v += mod - x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator*=(const fp &x) {
        v = ull(v) * x.v % mod;
        return *this;
    }
    fp &operator/=(const fp &x) {
        v = ull(v) * x.inv() % mod;
        return *this;
    }
    fp operator+(const fp &x) const {
        return fp(*this) += x;
    }
    fp operator-(const fp &x) const {
        return fp(*this) -= x;
    }
    fp operator*(const fp &x) const {
        return fp(*this) *= x;
    }
    fp operator/(const fp &x) const {
        return fp(*this) /= x;
    }
    bool operator==(const fp &x) const {
        return v == x.v;
    }
    bool operator!=(const fp &x) const {
        return v != x.v;
    }
    friend istream &operator>>(istream &is, fp &x) {
        return is >> x.v;
    }
    friend ostream &operator<<(ostream &os, const fp &x) {
        return os << x.v;
    }
};

template <unsigned mod> void rd(fp<mod> &x) {
    fastio::rd(x.v);
}
template <unsigned mod> void wt(fp<mod> x) {
    fastio::wt(x.v);
}

template <typename T> T Inv(ll n) {
    static const int md = T::get_mod();
    static vector<T> buf({0, 1});
    assert(n > 0);
    n %= md;
    while (SZ(buf) <= n) {
        int k = SZ(buf), q = (md + k - 1) / k;
        buf.push_back(buf[k * q - md] * q);
    }
    return buf[n];
}

template <typename T> T Fact(ll n, bool inv = 0) {
    static const int md = T::get_mod();
    static vector<T> buf({1, 1}), ibuf({1, 1});
    assert(n >= 0 and n < md);
    while (SZ(buf) <= n) {
        buf.push_back(buf.back() * SZ(buf));
        ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r, inv);
}

/**
 * @brief Modint
 */

using Fp = fp<1000000007>;

int main() {
    int N;
    cin >> N;
    vector<int> X(N), Y(N);
    rep(i,0,N) cin >> X[i] >> Y[i];
    reverse(ALL(X)), reverse(ALL(Y));
    static Fp DP[300][610][310] = {};
    if (X[0] <= 0 && 0 <= Y[0]) DP[0][0][1] = 1;
    rep(i,0,N-1) {
        rep(j,0,610) {
            rep(k,0,310) {
                if (DP[i][j][k] == 0) continue;
                if (X[i+1] <= 0 && 0 <= Y[i+1]) {
                    if (j >= 1) {
                        DP[i+1][j-1][k] += DP[i][j][k] * j;
                    }
                    DP[i+1][j][k+1] += DP[i][j][k];
                }
                if (X[i+1] <= 1 && 1 <= Y[i+1]) {
                    if (j >= 1 && k >= 2) {
                        DP[i+1][j-1][k-1] += DP[i][j][k] * j * (k - 1) * 2;
                    }
                    DP[i+1][j][k] += DP[i][j][k] * k * 2;
                }
                if (X[i+1] <= 2 && 2 <= Y[i+1]) {
                    if (j >= 1 && k >= 3) {
                        DP[i+1][j-1][k-2] += DP[i][j][k] * j * (k - 1) * (k - 2);
                    }
                    if (k >= 2) {
                        DP[i+1][j][k-1] += DP[i][j][k] * j * (k - 1) * 2;
                    }
                    DP[i+1][j+1][k] += DP[i][j][k] * k * 2;
                    if (k >= 2) {
                        DP[i+1][j][k-1] += DP[i][j][k] * k * (k - 1);
                    }
                }
            }
        }
    }
    cout << DP[N-1][0][1] << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 222916
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Modint {
    constexpr static ll MOD = 1000000007;
    ll val;

    Modint(ll val_ = 0) {
        val = val_;
        val %= MOD;
        if(val < 0) val += MOD;
    }
    Modint& operator+=(const Modint& other) {
        val += other.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    Modint operator+(const Modint& other) const { return Modint(*this) += other; }
    Modint& operator-=(const Modint& other) {
        val -= other.val;
        if(val < 0) val += MOD;
        return *this;
    }
    Modint operator-(const Modint& other) const { return Modint(*this) -= other; }
    Modint& operator*=(const Modint& other) {
        val *= other.val;
        val %= MOD;
        return *this;
    }
    Modint operator*(const Modint& other) const { return Modint(*this) *= other; }
};

int main() {
    int n; cin >> n;
    vector<int> x(n), y(n);
    for(int i = 0; i < n; i++) cin >> x[i] >> y[i];
    vector<vector<Modint>> dp(n+1, vector<Modint>(n*2+1));
    dp[0][0] = 1;
    for(int ii = n-1; ii >= 0; ii--) {
        int xx = x[ii], yy = y[ii];
        auto validlegs = [&](int a) { return xx <= a && a <= yy; };
        vector<vector<Modint>> ndp(n+1, vector<Modint>(n*2+1));
        for(int i = 0; i <= n; i++) {
            for(int j = 0; j <= n*2; j++) {
                if(dp[i][j].val == 0) continue;
                if(validlegs(0)) {
                    if(i+1 <= n) ndp[i+1][j] += dp[i][j];
                    if(j-1 >= 0) ndp[i][j-1] += dp[i][j]*j;
                }
                if(validlegs(1)) {
                    ndp[i][j] += dp[i][j]*2*i;
                    if(i-1 >= 0 && j-1 >= 0) ndp[i-1][j-1] += dp[i][j]*2*i*j - dp[i][j]*2*j;
                }
                if(validlegs(2)) {
                    if(i-1 >= 0) ndp[i-1][j] += dp[i][j]*i*(i-1);
                    if(i-2 >= 0 && j-1 >= 0) ndp[i-2][j-1] += dp[i][j]*i*(i-1)*j - dp[i][j]*j*(i-1)*2;
                    if(j+1 <= n*2) ndp[i][j+1] += dp[i][j]*2*i;
                    if(i-1 >= 0) ndp[i-1][j] += dp[i][j]*2*i*j - dp[i][j]*2*j;
                }
            }
        }
        dp.swap(ndp);
    }
    cout << dp[1][0].val << '\n';
}
accuracy: 1
time_ms: 200
memory_kb: 6008
final_rank: 6

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

const ll mod = 1'000'000'007;

int main() {
    int n;
    std::cin >> n;
    std::vector dp(n+1, std::vector<ll>(n+1, 0));
    dp[0][0] = 1;
    rep(i,0,n) {
        int x,y;
        std::cin >> x >> y;
        // x = 0, y = 2;
        std::vector nxt(n+1, std::vector<ll>(n+1, 0));
        rep(j,0,n+1) rep(k,0,n+1) {
            // すでにでてきたものの子にする
            {
                if(x == 0 && j - 1 >= 0) {
                    nxt[j-1][k] += dp[j][k] * j % mod;
                    nxt[j-1][k] %= mod;
                }
                if(x <= 1 && 1 <= y) {
                    nxt[j][k] += dp[j][k] * j * 2 % mod;
                    nxt[j][k] %= mod;
                }
                if(x <= 2 && 2 <= y && k - 1 >= 0) {
                    nxt[j][k-1] += (dp[j][k] * j % mod) * (k-1) * 2 % mod;
                    nxt[j][k-1] %= mod;
                }
                if(x <= 2 && 2 <= y && j < n) {
                    nxt[j + 1][k] += dp[j][k] * j % mod;
                    nxt[j + 1][k] %= mod;
                }
            }
            // 後に出てくるものを親にする
            {
                if(x == 0 && k < n) {
                    nxt[j][k + 1] += dp[j][k] % mod;
                    nxt[j][k + 1] %= mod;
                }
                if(x <= 1 && 1 <= y && j < n && k < n) {
                    nxt[j + 1][k + 1] += dp[j][k] * 2 % mod;
                    nxt[j + 1][k + 1] %= mod;
                }
                if(x <= 2 && 2 <= y && j < n) {
                    nxt[j + 1][k] += dp[j][k] * k * 2 % mod;
                    nxt[j + 1][k] %= mod;
                }
                if(x <= 2 && 2 <= y && j + 2 <= n && k < n) {
                    nxt[j + 2][k + 1] += dp[j][k] % mod;
                    nxt[j + 2][k + 1] %= mod;
                }
            }
        }
        std::swap(dp, nxt);
    }
    std::cout << dp[0][1] % mod << '\n';
}
accuracy: 1
time_ms: 500
memory_kb: 4532
final_rank: 10

code_snippet: #include <vector>
#include <iostream>
using namespace std;

int main() {
	// step #1. input
	int N;
	cin >> N;
	vector<int> L(N), R(N);
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> R[i];
	}
	
	// step #2. dynamic programming
	const int MOD = 1000000007;
	vector<vector<long long> > dp(N + 1, vector<long long>(N + 1));
	dp[0][0] = 1;
	for (int i = N - 1; i >= 0; i--) {
		vector<vector<long long> > ndp(N + 1, vector<long long>(N + 1));
		for (int j = 0; j <= N; j++) {
			for (int k = 0; k <= N; k++) {
				if (L[i] <= 0 && 0 <= R[i]) {
					ndp[j][k] += (j != 0 ? dp[j - 1][k] : 0);
					ndp[j][k] += (k != N ? dp[j][k + 1] * (k + 1) : 0);
				}
				if (L[i] <= 1 && 1 <= R[i]) {
					ndp[j][k] += dp[j][k] * j * 2;
					ndp[j][k] += (j != N && k != N ? dp[j + 1][k + 1] * j * (k + 1) * 2 : 0);
				}
				if (L[i] <= 2 && 2 <= R[i]) {
					ndp[j][k] += (j != N ? dp[j + 1][k] * (j + 1) * j : 0);
					ndp[j][k] += (j <= N - 2 && k != N ? dp[j + 2][k + 1] * (j + 1) * j * (k + 1) : 0);
					ndp[j][k] += (k != 0 ? dp[j][k - 1] * j * 2 : 0);
					ndp[j][k] += (j != N ? dp[j + 1][k] * j * k * 2 : 0);
				}
			}
		}
		for (int j = 0; j <= N; j++) {
			for (int k = 0; k <= N; k++) {
				dp[j][k] = ndp[j][k] % MOD;
			}
		}
	}
	
	// step #3. output
	cout << dp[1][0] << endl;
	
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 4616
final_rank: 5

code_snippet: #include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

#define MOD 1000000007

int main() {
	int n = ri();
	std::vector<std::pair<int, int> > a(n);
	for (int i = n; i--; ) a[i].first = ri(), a[i].second = ri();
	
	
	std::vector<std::vector<int> > dp = { { 1 } };
	for (int i = 0; i < n; i++) {
		int low = a[i].first;
		int high = a[i].second;
		std::vector<std::vector<int> > next(i + 2, std::vector<int>(2 * i + 3));
		for (int j = 0; j <= i; j++) for (int k = 0; k <= 2 * i; k++) if (dp[j][k]) {
			auto nCr = [] (int i, int j) {
				if (j == 0) return 1;
				if (j == 1) return i;
				if (j == 2) return i * (i - 1) / 2;
				assert(0);
			};
			auto go = [&] (int nj, int nk, int coef) {
				next[nj][nk] = (next[nj][nk] + (int64_t) dp[j][k] * coef) % MOD;
			};
			// attach
			if (k) {
				for (int l = 0; (l <= high || l == 0) && l < j; l++) {
					for (int m = std::max(0, low - l); l + m >= low && l + m <= high; m++) {
						if (!l && m) break;
						go(j - l, k - 1 + m, k * nCr(j - 1, l) * (l + m ? 2 : 1));
					}
				}
			}
			// no attach
			for (int l = 0; (l <= high || l == 0) && l <= j; l++) {
				for (int m = std::max(0, low - l); l + m <= high; m++) {
					if (!l && m) break;
					go(j + 1 - l, k + m, nCr(j, l) * (l + m ? 2 : 1));
				}
			}
		}
		/*
		std::cerr << i << " : " << std::endl;
		for (int i = 0; i < (int) next.size(); i++) for (int j = 0; j < (int) next[i].size() ;j++) if (next[i][j]) std::cerr << "  " << i << " " << j << " : " << next[i][j] << std::endl;
		*/
		std::swap(dp, next);
		
	}
	
	printf("%d\n", dp[1][0]);
	
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 4980
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)

#define all(c) std::begin(c), std::end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)

template <class T, class ...Args> void debug_impl(string s, T&& f, Args &&...args) {
    cerr << '(' << s << "): (" << forward<T>(f);
    ((cerr << ", " << forward<Args>(args)), ..., (cerr << ')' << '\n'));
}
#else
#define debug(...) void(0)
#endif

template <typename T>
bool chmax(T& a, const T& b) { return b > a ? (a = b, true) : false; }
template <typename T>
bool chmin(T& a, const T& b) { return b < a ? (a = b, true) : false; }

template <typename T>
istream& operator>>(istream& in, vector<T> &v) {
    for (auto &e : v) in >> e;
    return in;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T> &v) {
    int n = v.size();
    rep(i, n) {
        out << v[i];
        if (i + 1 != n) out << ' ';
    }
    return out;
}

template <typename ...Args> void read(Args& ...args) {
    (cin >> ... >> args);
}
template <typename Head, typename ...Args> void print(Head &&h, Args&& ...tail) {
    cout << h, ((cout << ' ' << forward<Args>(tail)), ..., (cout << '\n'));
}

template<class T> using vc = vector<T>;
using ll = long long;

const ll MOD = 1e9 + 7;

// ll fact[330];

int main() {
    // fact[0] = 1;
    // for(int j = 1; j <= 310; ++ j) {
    //     fact[j] = fact[j - 1];
    //     (fact[j] *= j) %= MOD;
    // }
    int n; read(n);
    vc<int> x(n), y(n);
    rep(i, n) {
        read(x[i], y[i]);
    }
    reverse(all(x)); reverse(all(y));
    vector pd(n + 5, vc<ll>(n + 5, 0));
    // 子供の枠として埋めなければならない個数としてやると良さそう！
    if(x[0] != 0) {
        print(0);
        return 0;
    }
    pd[1][0] = 1;
    for(int i = 1; i < n; ++ i) {
        vector dp(n + 5, vc<ll>(n + 5, 0));
        for(int j = 0; j <= n; ++ j) {
            for(int k = 0; k <= n; ++ k) {
                // debug(i, j, k);
                // 大としてとるものの個数で場合わけ
                // 孤立点としてとる
                // 子供はとれない　// 子供は0個
                if(x[i] == 0 and j - 1 >= 0) {
                    (dp[j][k] += pd[j - 1][k]) %= MOD;
                    // debug(i, j - 1, k - y[i], pd[j - 1][k]);
                }
                // 子供として2つとる　大，大
                if(y[i] == 2) {
                    (dp[j][k] += pd[j + 1][k] * (j + 1) % MOD * j % MOD) %= MOD;
                    // debug(i, j + 1, k, pd[j + 1][k]);
                }
                // 子供として1つとる 
                // 大
                // 大，小？
                if(y[i] >= 1) {
                    // (x[i], y[i])
                    // (1, 1), (1, 2), (2, 2)
                    if(x[i] != 2) (dp[j][k] += pd[j][k] * j * 2 % MOD) %= MOD; //大のみ
                    if(k - 1 >= 0 and y[i] != 1) (dp[j][k] += pd[j][k - 1] * j * 2 % MOD) %= MOD; // 大，小
                    // debug(i, j, k - y[i] + 1, pd[j][k - y[i] + 1]);
                }
                // // 子供の枠に埋める
                // // 子供はとれない
                // if(x[i] == 0 and k + 1 >= 0) {
                //     (dp[j][k] += pd[j][k + 1] * (k + 1) % MOD) %= MOD;
                //     debug(i, j, k + 1, pd[j][k + 1]);
                // }
                // 親をとる
                {
                    // 連結成分は -0
                    // 子供は0個
                    if(x[i] == 0 and j - 1 >= 0) {
                        (dp[j][k] += pd[j][k + 1] * (k + 1)) %= MOD;
                    }
                    // 連結成分は -2
                    // 子供として2つとる　大，大
                    if(y[i] == 2) {
                        (dp[j][k] += pd[j + 2][k + 1] * (j + 1) % MOD * j % MOD * (k + 1) % MOD) %= MOD;
                    }
                    // 連結成分は-1
                    // 子供として1つとる 
                    // 大
                    // 大，小？
                    if(y[i] >= 1) {
                        // (x[i], y[i])
                        // (1, 1), (1, 2), (2, 2)
                        if(x[i] != 2) (dp[j][k] += pd[j + 1][k + 1] * j * 2 % MOD * (k + 1) % MOD) %= MOD; //大のみ
                        if(k - 1 >= 0 and y[i] != 1) (dp[j][k] += pd[j + 1][k] * j * 2 % MOD * k % MOD) %= MOD; // 大，小
                        // debug(i, j, k - y[i] + 1, pd[j][k - y[i] + 1]);
                    }
                }
                if(dp[j][k] != 0) debug(i + 1, j, k, dp[j][k]);
            }
            
        }
        dp.swap(pd);
    }
    ll ans = pd[1][0];
    // ll ans = 0;
    // for(int j = 0; j <= n; ++ j) {
        // for(int k = 0; k <= 2 * n; ++ k) {
        //     if(dp[n][1][k] == 0) continue;
        //     debug(k, dp[n][1][k]);
        //     (ans += dp[n][1][k]) %= MOD;
        // }
    // }
    // NG
    // for(int j = 1; j <= n; ++ j) {
    //     debug(j, dp[n][j][j - 1]);
    //     ans += dp[n][j][j - 1] * fact[j];
    // }
    print(ans);
}
accuracy: 1
time_ms: 670
memory_kb: 4696
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

struct Union_Find_Tree {
    vector<int> data;
    const int n;
    int cnt;

    Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}

    int root(int x) {
        if (data[x] < 0) return x;
        return data[x] = root(data[x]);
    }

    int operator[](int i) { return root(i); }

    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        cnt--;
        return true;
    }

    int size(int x) { return -data[root(x)]; }

    int count() { return cnt; };

    bool same(int x, int y) { return root(x) == root(y); }

    void clear() {
        cnt = n;
        fill(begin(data), end(data), -1);
    }
};

mint ans;

void dfs(int N, const vector<int> &a, const vector<int> &b, int t, vector<int> p) {
    if (t == N) {
        vector<vector<int>> ids(N);
        Union_Find_Tree uf(N);
        int K = N;
        rep(i, N) {
            if (p[i] != -1) ids[p[i]].eb(i), K--, uf.unite(p[i], i);
        }
        if (uf.count() != 1) return;
        if (K != 1) return;
        mint tmp = 1;
        rep(i, N) {
            if (empty(ids[i])) continue;
            if (sz(ids[i]) < a[i] || sz(ids[i]) > b[i]) return;
            if (*max_element(all(ids[i])) <= i) return;
            tmp *= 2;
        }
        ans += tmp;
        // print(p);
        // cout << tmp << '\n';
        return;
    }

    rep2(i, -1, N - 1) {
        if (i == t) continue;
        p[t] = i;
        dfs(N, a, b, t + 1, p);
    }
}

mint solve(int N, vector<int> a, vector<int> b) {
    ans = 0;
    vector<int> p(N, -1);
    dfs(N, a, b, 0, p);
    return ans;
}

int main() {
    int N;
    cin >> N;

    vector<int> a(N), b(N);
    rep(i, N) cin >> a[i] >> b[i];

    vector<vector<mint>> dp(N + 1, vector<mint>(N + 1, 0)), ndp(N + 1, vector<mint>(N + 1, 0));
    dp[0][0] = 1;

    rep3(i, N - 1, 0) {
        rep(j, N + 1) rep(k, N + 1) ndp[j][k] = 0;
        rep(j, N + 1) {
            rep(k, N + 1) {
                if (dp[j][k] == 0) continue;
                rep(l, 3) {
                    if (l < a[i] || b[i] < l) continue;
                    if (l == 0) {
                        ndp[j + 1][k] += dp[j][k];
                        if (k > 0) ndp[j][k - 1] += dp[j][k] * k;
                    } else if (l == 1) {
                        if (j == 0) continue;
                        ndp[j][k] += dp[j][k] * j * 2;
                        if (j > 0 && k > 0) ndp[j - 1][k - 1] += dp[j][k] * mint(j - 1) * mint(k) * 2;
                    } else {
                        if (j == 0) continue;
                        if (j > 1) ndp[j - 1][k] += dp[j][k] * mint(j) * mint(j - 1);
                        if (j > 1 && k > 0) ndp[j - 2][k - 1] += dp[j][k] * mint(j - 1) * mint(j - 2) * mint(k);
                        ndp[j][k + 1] += dp[j][k] * j * 2;
                        if (j > 0 && k > 0) ndp[j - 1][k] += dp[j][k] * mint(j - 1) * mint(k) * 2;
                    }
                }
            }
        }
        // cout << i + 1 << '\n';
        // rep(i, N + 1) print(ndp[i]);
        swap(dp, ndp);
    }

    cout << dp[1][0] << '\n';
    // cout << solve(N, a, b) << '\n';
}
accuracy: 1
time_ms: 180
memory_kb: 3840
final_rank: 4

code_snippet: #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(...) emplace_back(__VA_ARGS__)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define REP(i, l, r) for (int i = l; i < (r); i++)
#define REP2(i, l, r) for (int i = (int)r - 1; i >= (l); i--)
#define siz(x) (ll) x.size()
template <class T>
using rque = priority_queue<T, vector<T>, greater<T>>;

template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
bool chmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return 1;
    }
    return 0;
}

template <class T>
void print(vector<T> a) {
    if (a.empty())
        cout << '\n';
    else {
        for (int i = 0; i < a.size(); i++)
            cout << a[i] << (i + 1 == a.size() ? '\n' : ' ');
    }
}

// __int128_t gcd(__int128_t a, __int128_t b) {
//     if (a == 0)
//         return b;
//     if (b == 0)
//         return a;
//     __int128_t cnt = a % b;
//     while (cnt != 0) {
//         a = b;
//         b = cnt;
//         cnt = a % b;
//     }
//     return b;
// }

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

struct UnionFind {
    vector<int> data;
    int num;

    UnionFind(int sz) {
        data.assign(sz, -1);
        num = sz;
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

    int size(int k) {
        return (-data[find(k)]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int operator[](int k) {
        return find(k);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {
    }

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {
    }

    static int get_mod() {
        return mod;
    }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod)
            x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() {
        return *this += Mod_Int(1);
    }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() {
        return *this -= Mod_Int(1);
    }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const {
        return Mod_Int(-x);
    }

    Mod_Int operator+(const Mod_Int &p) const {
        return Mod_Int(*this) += p;
    }

    Mod_Int operator-(const Mod_Int &p) const {
        return Mod_Int(*this) -= p;
    }

    Mod_Int operator*(const Mod_Int &p) const {
        return Mod_Int(*this) *= p;
    }

    Mod_Int operator/(const Mod_Int &p) const {
        return Mod_Int(*this) /= p;
    }

    bool operator==(const Mod_Int &p) const {
        return x == p.x;
    }

    bool operator!=(const Mod_Int &p) const {
        return x != p.x;
    }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1)
                ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow2(ll x, ll n, ll mod) {
    ll ans = 1;
    x %= mod;
    while (n != 0) {
        if (n & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    ans %= mod;
    return ans;
}

ll modinv2(ll a, ll mod) {
    ll b = mod, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= mod;
    if (u < 0)
        u += mod;
    return u;
}

ll devide_int(ll a, ll b) {
    return (a >= 0 ? a / b : (a - b + 1) / b);
}

constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
// constexpr int mod = 31607;
using mint = Mod_Int<mod>;

mint mpow(mint x, ll n) {
    bool rev = n < 0;
    n = abs(n);
    mint ans = 1;
    while (n != 0) {
        if (n & 1)
            ans *= x;
        x *= x;
        n = n >> 1;
    }
    return (rev ? ans.inverse() : ans);
}

// ----- library -------
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;
    vector<int> x(n), y(n);
    rep(i, n) cin >> x[i] >> y[i];
    vector<vector<mint>> dp(n + 1, vector<mint>(n + 1, 0)), ndp(n + 1, vector<mint>(n + 1, 0));
    REP(k, x[0], y[0] + 1) dp[k][1] = (k == 1 ? 2 : 1);
    int sy = y[0];
    REP(t, 1, n) {
        rep(i, n + 1) rep(j, n + 1) ndp[i][j] = 0;
        REP(k, x[t], y[t] + 1) rep(f, 2) rep(g, 2) {
            int di = 1 - f - k + g, dj = g - f;
            int il = max(0, di), ir = min(min(n, sy), n + di) + 1, jl = max(0, dj), jr = min(min(n, t), n + dj) + 1;
            REP(i, il, ir) REP(j, jl, jr) {
                if (k != 2 && g)
                continue;
                int ni = i - 1 + f + k - g, nj = j + f - g;
                mint val = dp[i][j];
                if (!f)
                    val *= i;
                if (g || k == 1)
                    val += val;
                if (g)
                    val *= j - 1 + f;
                ndp[i - 1 + f + k - g][j + f - g] += val;
            }
        }
        swap(dp, ndp);
        sy += y[t];
    }
    cout << dp[0][1] << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 3876
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define inf 1000000007
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)n-1; i >= 0; --i)
#define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define SZ(c) (int)(c).size()
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
    os << "{";
    for (int i = 0; i<(int)vec.size(); i++) {
        os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// pair出力
template<typename T, typename U>
ostream& operator << (ostream& os, pair<T, U> pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// map出力
template<typename T, typename U>
ostream& operator << (ostream& os, map<T, U>& map_var) {
    os << "{";
    for(auto itr = map_var.begin(); itr != map_var.end(); itr++){
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if(itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set 出力
template<typename T>
ostream& operator << (ostream& os, set<T>& set_var) {
    os << "{";
    for(auto itr = set_var.begin(); itr != set_var.end(); itr++){
        os << (*itr);
        ++itr;
        if(itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// tuple 出力
template<int N,class Tuple>
void out(ostream &os,const Tuple &t){}
template<int N,class Tuple,class H,class ...Ts>
void out(ostream &os,const Tuple &t){
    if(N)os<<", ";
    os<<get<N>(t);
    out<N+1,Tuple,Ts...>(os,t);
}
template<class ...Ts>
ostream& operator<<(ostream &os, const tuple<Ts...> &t){
    os<<"(";
    out<0,tuple<Ts...>,Ts...>(os,t);
    os<<")";
    return os;
}
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define dbg(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dbg(...) (void(0))
#define dump(x) (void(0))
#endif
template<typename A, typename T>
std::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    array = val;
}
template<typename A, typename T>
std::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    for (auto& a : array) {
        fill(a, val);
    }
}
template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
vector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;}
template <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;}
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
ll allbit(ll n) { return (1LL << n) - 1; }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }


template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> void zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
}
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

template <unsigned int mod>
class ModInt {
private:
    unsigned int v;
    static unsigned int norm(const unsigned int& x){ return x < mod ? x : x - mod; }
    static ModInt make(const unsigned int& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static unsigned int inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    unsigned int operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        unsigned int v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

#define MOD 1000000007
using mod = ModInt<MOD>;

#define MAX_N 200000
mod inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i=2;i<MAX_N;i++){
        inv[i] = MOD - inv[MOD % i] * (MOD / i);
        fac[i] = fac[i-1] * i;
        finv[i] = finv[i-1] * inv[i];
    }
}

mod comb(int a, int b)
{
    if(a<b) return 0;
    return fac[a] * finv[b] * finv[a-b];
}

mod dp[311][311][311];

int main(){
    INT(n);
    vector<int> a(n),b(n);
    rep(i,n)cin >> a[i] >> b[i];
    dp[0][0][0] = 1;
    rep(i,n){
        rep(j,n+1){
            rep(k,n+1){
                if(dp[i][j][k]!=0){
                    dbg(i,j,k,dp[i][j][k]);
                }
                for(int c = a[i];c<=b[i];c++){
                    if(j!=0){
                        // 頭　のみ
                        int dd = 1;
                        if(c==1)dd = 2;
                        dp[i+1][j+c-1][k] += dp[i][j][k] * j * dd;
                    }
                    if(c==2){
                        // 頭　片足
                        if(j!=0&&k!=0)dp[i+1][j+c-2][k-1] += dp[i][j][k] * j * (k-1) * 2;
                    }
                    {
                        // 何もつなげない
                        int dd = 1;
                        if(c==1)dd = 2;
                        dp[i+1][j+c][k+1] += dp[i][j][k] * dd;
                    }
                    if(c==2){
                        // 片足
                        if(k!=0){
                            dp[i+1][j+c-1][k] += dp[i][j][k] * k * 2;
                        }
                    }
                }
            }
        }
    }
    cout << dp[n][0][1] << endl;
    return 0;
}
accuracy: 1
time_ms: 540
memory_kb: 123276
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define inf 1000000007
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)n-1; i >= 0; --i)
#define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define SZ(c) (int)(c).size()
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
    os << "{";
    for (int i = 0; i<(int)vec.size(); i++) {
        os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// pair出力
template<typename T, typename U>
ostream& operator << (ostream& os, pair<T, U> pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// map出力
template<typename T, typename U>
ostream& operator << (ostream& os, map<T, U>& map_var) {
    os << "{";
    for(auto itr = map_var.begin(); itr != map_var.end(); itr++){
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if(itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set 出力
template<typename T>
ostream& operator << (ostream& os, set<T>& set_var) {
    os << "{";
    for(auto itr = set_var.begin(); itr != set_var.end(); itr++){
        os << (*itr);
        ++itr;
        if(itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// tuple 出力
template<int N,class Tuple>
void out(ostream &os,const Tuple &t){}
template<int N,class Tuple,class H,class ...Ts>
void out(ostream &os,const Tuple &t){
    if(N)os<<", ";
    os<<get<N>(t);
    out<N+1,Tuple,Ts...>(os,t);
}
template<class ...Ts>
ostream& operator<<(ostream &os, const tuple<Ts...> &t){
    os<<"(";
    out<0,tuple<Ts...>,Ts...>(os,t);
    os<<")";
    return os;
}
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define dbg(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dbg(...) (void(0))
#define dump(x) (void(0))
#endif
template<typename A, typename T>
std::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    array = val;
}
template<typename A, typename T>
std::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val)
{
    for (auto& a : array) {
        fill(a, val);
    }
}
template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
vector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;}
template <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;}
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
ll allbit(ll n) { return (1LL << n) - 1; }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }


template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> void zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
}
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

template <unsigned int mod>
class ModInt {
private:
    unsigned int v;
    static unsigned int norm(const unsigned int& x){ return x < mod ? x : x - mod; }
    static ModInt make(const unsigned int& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static unsigned int inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    unsigned int operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        unsigned int v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

#define MOD 1000000007
using mod = ModInt<MOD>;

#define MAX_N 200000
mod inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i=2;i<MAX_N;i++){
        inv[i] = MOD - inv[MOD % i] * (MOD / i);
        fac[i] = fac[i-1] * i;
        finv[i] = finv[i-1] * inv[i];
    }
}

mod comb(int a, int b)
{
    if(a<b) return 0;
    return fac[a] * finv[b] * finv[a-b];
}

mod dp[311][311][311];

int main(){
    INT(n);
    vector<int> a(n),b(n);
    rep(i,n)cin >> a[i] >> b[i];
    dp[0][0][0] = 1;
    rep(i,n){
        rep(j,n+1){
            rep(k,n+1){
                if(dp[i][j][k]!=0){
                    dbg(i,j,k,dp[i][j][k]);
                }
                for(int c = a[i];c<=b[i];c++){
                    // かける
                    if(j!=0){
                        int dd = 1;
                        if(c==1)dd = 2;
                        dp[i+1][j+c-1][k] += dp[i][j][k] * j * dd;
                    }
                    if(c==2){
                        if(k!=0)dp[i+1][j+c-2][k-1] += dp[i][j][k] * j * k * 2;
                    }
                    // かけない
                    {
                        int dd = 1;
                        if(c==1)dd = 2;
                        dp[i+1][j+c][k+1] += dp[i][j][k] * dd;
                    }
                    if(c==2){
                        dp[i+1][j+c-1][k] += dp[i][j][k] * k * 2;
                    }
                }
            }
        }
    }
    cout << dp[n][0][1] << endl;
    return 0;
}
accuracy: 0.1
time_ms: 20
memory_kb: 123200
final_rank: 17

code_snippet: #ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
//#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
	print(p.a,2);
	print(p.b,suc);
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
	rep(i,v.size())
		print(v[i]+off,i==int(v.size())-1?suc:2);
}

template<class T,size_t N>
void print(const array<T,N>&v,int suc=1){
	rep(i,N)
		print(v[i],i==int(N)-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

vvc<int> readGraph(int n,int m){
	vvc<int> g(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		//sc.read(a,b);
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	return g;
}

vvc<int> readTree(int n){
	return readGraph(n,n-1);
}

//mint107 は verify してねえ
//#define DYNAMIC_MOD

struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
	mod=m;
	im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
	ull z=ull(a)*b;
	uint x=((unsigned __int128)z*im)>>64;
	uint v=uint(z)-x*mod;
	return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
	static constexpr uint const &mod=ref.mod;
	static modular root(){return modular(ref.root);}
	uint v;
	//modular(initializer_list<uint>ls):v(*ls.bg){}
	modular(ll vv=0){s(vv%mod+mod);}
	modular& s(uint vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	modular operator-()const{return modular()-*this;}
	modular& operator+=(const modular&rhs){return s(v+rhs.v);}
	modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
	modular&operator*=(const modular&rhs){
		#ifndef DYNAMIC_MOD
		v=ull(v)*rhs.v%mod;
		#else
		v=ref.product(v,rhs.v);
		#endif
		return *this;
	}
	modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
	modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
	modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
	modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
	modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
	modular pow(ll n)const{
		if(n<0)return inv().pow(-n);
		modular res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modular inv()const{return pow(mod-2);}
	/*modular inv()const{
		int x,y;
		int g=extgcd<ll>(v,mod,x,y);
		assert(g==1);
		if(x<0)x+=mod;
		return modular(x);
	}*/
	friend modular operator+(ll x,const modular&y){
		return modular(x)+y;
	}
	friend modular operator-(ll x,const modular&y){
		return modular(x)-y;
	}
	friend modular operator*(ll x,const modular&y){
		return modular(x)*y;
	}
	friend modular operator/(ll x,const modular&y){
		return modular(x)/y;
	}
	friend ostream& operator<<(ostream&os,const modular&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,modular&m){
		ll x;is>>x;
		m=modular(x);
		return is;
	}
	bool operator<(const modular&r)const{return v<r.v;}
	bool operator==(const modular&r)const{return v==r.v;}
	bool operator!=(const modular&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};

#ifndef DYNAMIC_MOD
//extern constexpr modinfo base{998244353,3};
extern constexpr modinfo base{1000000007,0};
//modinfo base{1,0};
#ifdef USE_GOOD_MOD
static_assert(base.mod==998244353);
#endif
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;

#ifdef LOCAL
const int vmax=1010;
#else
const int vmax=(1<<21)+10;
#endif
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
	fact[0]=1;
	rng(i,1,vmax){
		fact[i]=fact[i-1]*i;
	}
	finv[vmax-1]=fact[vmax-1].inv();
	for(int i=vmax-2;i>=0;i--){
		finv[i]=finv[i+1]*(i+1);
	}
	for(int i=vmax-1;i>=1;i--){
		invs[i]=finv[i]*fact[i-1];
	}
}
mint choose(int n,int k){
	return fact[n]*finv[n-k]*finv[k];
}
mint binom(int a,int b){
	return fact[a+b]*finv[a]*finv[b];
}
mint catalan(int n){
	return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}

/*
const int vmax=110;
mint binbuf[vmax][vmax];
mint choose(int n,int k){
	return binbuf[n-k][k];
}
mint binom(int a,int b){
	return binbuf[a][b];
}
void initfact(){
	binbuf[0][0]=1;
	rep(i,vmax)rep(j,vmax){
		if(i)binbuf[i][j]+=binbuf[i-1][j];
		if(j)binbuf[i][j]+=binbuf[i][j-1];
	}
}
*/

mint p2[vmax];
void initp2(){
	p2[0]=1;
	rep(i,vmax-1)p2[i+1]=p2[i]*2;
}

const int nmax=310;
mint dp[2][nmax][nmax];

void slv(){
	int n;cin>>n;
	vi lw(n),up(n);
	rep(i,n)cin>>lw[i]>>up[i];
	int cur=0;
	int h=0,w=0;
	dp[cur][0][0]=1;
	per(vert,n){
		int nx=cur^1;
		int nh=h,nw=w;
		if(lw[vert]==0)chmax(nh,h+1);
		if(up[vert]==2)chmax(nw,w+1);
		
		rep(top,h+1)rep(ch,w+1){
			if(inc(lw[vert],0,up[vert])){
				dp[nx][top+1][ch]+=dp[cur][top][ch];
				if(ch)dp[nx][top][ch-1]+=dp[cur][top][ch]*ch;
			}
			if(inc(lw[vert],1,up[vert])){
				dp[nx][top][ch]+=dp[cur][top][ch]*top*2;
				if(top&&ch)dp[nx][top-1][ch-1]+=dp[cur][top][ch]*ch*(top-1)*2;
			}
			if(inc(lw[vert],2,up[vert])){
				if(top)dp[nx][top-1][ch]+=dp[cur][top][ch]*top*(top-1);
				dp[nx][top][ch+1]+=dp[cur][top][ch]*top*2;
				if(top>=2&&ch)dp[nx][top-2][ch-1]+=dp[cur][top][ch]*ch*(top-1)*(top-2);
				if(top)dp[nx][top-1][ch]+=dp[cur][top][ch]*ch*(top-1)*2;
			}
		}
		
		rep(i,h+1)rep(j,w+1)dp[cur][i][j]=0;
		cur=nx;
		h=nh;
		w=nw;
	}
	print(dp[cur][1][0]);
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	//int t;cin>>t;rep(_,t)
	slv();
}
accuracy: 1
time_ms: 170
memory_kb: 36988
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> bool chmax(T& x, U y){
	if(x<y){ x=y; return true; }
	return false;
}
template<class T,class U> bool chmin(T& x, U y){
	if(y<x){ x=y; return true; }
	return false;
}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class T>
V<T> Vec(size_t a) {
    return V<T>(a);
}
template<class T, class... Ts>
auto Vec(size_t a, Ts... ts) {
  return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));
}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<class D> D divFloor(D a, D b){
	return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
template<class D> D divCeil(D a, D b) {
	return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}
template<unsigned int mod_>
struct ModInt{
	using uint = unsigned int;
	using ll = long long;
	using ull = unsigned long long;

	constexpr static uint mod = mod_;

	uint v;
	ModInt():v(0){}
	ModInt(ll _v):v(normS(_v%mod+mod)){}
	explicit operator bool() const {return v!=0;}
	static uint normS(const uint &x){return (x<mod)?x:x-mod;}		// [0 , 2*mod-1] -> [0 , mod-1]
	static ModInt make(const uint &x){ModInt m; m.v=x; return m;}
	ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}
	ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}
	ModInt operator-() const { return make(normS(mod-v)); }
	ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}
	ModInt operator/(const ModInt& b) const { return *this*b.inv();}
	ModInt& operator+=(const ModInt& b){ return *this=*this+b;}
	ModInt& operator-=(const ModInt& b){ return *this=*this-b;}
	ModInt& operator*=(const ModInt& b){ return *this=*this*b;}
	ModInt& operator/=(const ModInt& b){ return *this=*this/b;}
	ModInt& operator++(int){ return *this=*this+1;}
	ModInt& operator--(int){ return *this=*this-1;}
	template<class T> friend ModInt operator+(T a, const ModInt& b){ return (ModInt(a) += b);}
	template<class T> friend ModInt operator-(T a, const ModInt& b){ return (ModInt(a) -= b);}
	template<class T> friend ModInt operator*(T a, const ModInt& b){ return (ModInt(a) *= b);}
	template<class T> friend ModInt operator/(T a, const ModInt& b){ return (ModInt(a) /= b);}
	ModInt pow(ll p) const {
		if(p<0) return inv().pow(-p);
		ModInt a = 1;
		ModInt x = *this;
		while(p){
			if(p&1) a *= x;
			x *= x;
			p >>= 1;
		}
		return a;
	}
	ModInt inv() const {		// should be prime
		return pow(mod-2);
	}
	// ll extgcd(ll a,ll b,ll &x,ll &y) const{
	// 	ll p[]={a,1,0},q[]={b,0,1};
	// 	while(*q){
	// 		ll t=*p/ *q;
	// 		rep(i,3) swap(p[i]-=t*q[i],q[i]);
	// 	}
	// 	if(p[0]<0) rep(i,3) p[i]=-p[i];
	// 	x=p[1],y=p[2];
	// 	return p[0];
	// }
	// ModInt inv() const {
	// 	ll x,y;
	// 	extgcd(v,mod,x,y);
	// 	return make(normS(x+mod));
	// }

	bool operator==(const ModInt& b) const { return v==b.v;}
	bool operator!=(const ModInt& b) const { return v!=b.v;}
	bool operator<(const ModInt& b) const { return v<b.v;}
	friend istream& operator>>(istream &o,ModInt& x){
		ll tmp;
		o>>tmp;
		x=ModInt(tmp);
		return o;
	}
	friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;}
};
using mint = ModInt<1000000007>;

mint dp[310][310][310];	//n,m,l

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N; cin >> N;
	V<int> X(N),Y(N); rep(i,N) cin >> X[i] >> Y[i];
	dp[0][0][0] = 1;
	per(i,N){
		int n = N-1-i;
		// n -> n+1, add i
		rep(m,N) rep(l,m+1) if(dp[n][m][l]){
			int r = n-m+l;
			show("---------");shows(n,m,l,r); show(dp[n][m][l]);
			assert(0<=r && r<=n);
			rep(use_l,2){
				if(l-use_l < 0) continue;
				int can_r = r - use_l;
				if(X[i] <= 0 and 0 <= Y[i]){
					//00
					dp[n+1][m][l-use_l] += dp[n][m][l] * (use_l ? l : 1);
				}
				if(X[i] <= 1 and 1 <= Y[i]){
					//01
					if(can_r > 0){
						dp[n+1][m+1][l-use_l] += dp[n][m][l] * (use_l ? l : 1) * can_r * 2;
					}
				}
				if(X[i] <= 2 and 2 <= Y[i]){
					if(can_r > 0){
						//02
						dp[n+1][m+2][l-use_l] += dp[n][m][l] * (use_l ? l : 1) * can_r * (can_r-1);
						//11
						dp[n+1][m+2][l-use_l+1] += dp[n][m][l] * (use_l ? l : 1) * can_r * 2;
					}
				}
			}
		}
	}
	cout << dp[N][N-1][0] << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 119836
final_rank: 9

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <bitset>
#include <fstream>
#include <chrono>

constexpr int MOD = 1'000'000'007;

using Puppets = std::vector<std::pair<int, int>>;
long long int solve(const Puppets& puppets) {
	const int n = puppets.size(); 
	std::vector<std::vector<std::vector<int>>> memo(n, std::vector<std::vector<int>>(n + 1, std::vector<int>(n + 1, 0))), version(n, std::vector<std::vector<int>>(n + 1, std::vector<int>(n + 1, -1)));
	memo[0][0][0] = 1;
	std::vector<std::tuple<int, int, int>> prev, next; prev.emplace_back(0, 0, 0);
	int p = 0;
	auto update = [n, &p, &memo, &next, &version](const int edge_count, const int head, const int toe, long long int count) mutable {
		if (edge_count < 0 || n <= edge_count) return;
		if (head <= 0 || toe < 0 || n <= toe) return;
		count %= MOD;
		if (count == 0LL) return;
		memo[edge_count][head][toe] = (memo[edge_count][head][toe] + count) % MOD;
		if (version[edge_count][head][toe] != p) {
			next.emplace_back(edge_count, head, toe);
			version[edge_count][head][toe] = p;
		}
	};
	for (; p < n; ++p) {
		const auto [min, max] = puppets[p];
		next.clear();
		std::sort(prev.rbegin(), prev.rend(), [](const auto a, const auto b) {
			if (std::get<0>(a) != std::get<0>(b)) return std::get<0>(a) < std::get<0>(b);
			if (std::get<1>(a) != std::get<1>(b)) return std::get<1>(a) < std::get<1>(b);
			return std::get<2>(a) < std::get<2>(b);
			});
		prev.erase(std::unique(prev.begin(), prev.end()), prev.end());
		for (const auto [edge_count, head, toe] : prev) {
			const long long int count = memo[edge_count][head][toe];
			if (min <= 0 && 0 <= max) {
				update(edge_count, head + 1, toe, count);
				update(edge_count + 1, head, toe - 1, count * toe);
			}
			if (min <= 1 && 1 <= max) {
				update(edge_count, head + 1, toe + 1, count * 2);
				update(edge_count + 1, head, toe, count * 2 * toe);
			}
			if (min <= 2 && 2 <= max) {
				update(edge_count, head + 1, toe + 2, count);
				update(edge_count + 1, head, toe + 1, count * 2 * head);
				update(edge_count + 1, head, toe + 1, count * toe);
				update(edge_count + 2, head - 1, toe, count * 2 * (head - 1) * toe);
			}
			memo[edge_count][head][toe] = 0;
		}
		std::swap(next, prev);
	}
	const auto result = memo[n - 1][1][0];
	return result;
}

int main() {
	int n; std::cin >> n;
	Puppets puppets(n);
	for (auto& [min, max] : puppets) {
		std::cin >> min >> max;
	}
	const auto result = solve(puppets);
	std::cout << result << '\n';
}
accuracy: 1
time_ms: 690
memory_kb: 224400
final_rank: 16