#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct query {
    int c, l, r;
};

string s;
int S[100010];
int k, L, R;
query qs[510];
vector<int> let_pos[30];
int cnt[100010];
int cpl[510], cpr[510];
int let_cnt[30];
int curr_good = 0;
long long ans = 0;

int main() {
    cin >> s;
    for (int i = 0; i < MAX_ALPHABET; i++) let_pos[i].push_back(-1);
    for (int i = 0; i < s.length(); i++) {
        S[i] = s[i]-'a';
        let_pos[S[i]].push_back(i);
    }
    cin >> k >> L >> R;
    for (int i = 0; i < k; i++) {
        char ch; cin >> ch; qs[i].c = ch-'a';
        cin >> qs[i].l >> qs[i].r;
    }

    for (int pos = 0; pos < s.length(); ++pos) {
        int c = S[pos];
        ++let_cnt[c];
        for (int qp = 0; qp < k; qp++)
            if (qs[qp].c == S[pos] && qs[qp].l <= 1 && qs[qp].r >= 1) ++cnt[pos];
            else if (qs[qp].c != S[pos] && qs[qp].l == 0) ++cnt[pos];
        if (cnt[pos] >= L && cnt[pos] <= R) ++curr_good;
        for (int qp = 0; qp < k; qp++) {
            if (qs[qp].c == S[pos]) {
                if (let_cnt[c] == qs[qp].l) {
                    cpl[qp] = 0;
                    for (int tp = 0; tp <= let_pos[S[pos]][1]; tp++) if (tp < pos) {
                        ++cnt[tp];
                        if (cnt[tp] == L) ++curr_good;
                        if (cnt[tp] == R+1) --curr_good;
                    }
                    cpr[qp] = 1;
                } else if (let_cnt[c] > qs[qp].l) {
                    if (let_cnt[c] > qs[qp].r) {
                    for (int tp = let_pos[c][cpl[qp]]+1; tp <= let_pos[c][cpl[qp]+1]; tp++) if (tp < pos) {
                        --cnt[tp];
                        if (cnt[tp] == R) ++curr_good;
                        if (cnt[tp] == L-1) --curr_good;
                    }
                    ++cpl[qp];
                    }
                    if (qs[qp].l > 0) {
                    for (int tp = let_pos[c][cpr[qp]]+1; tp <= let_pos[c][cpr[qp]+1]; tp++) if (tp < pos) {
                        ++cnt[tp];
                        if (cnt[tp] == L) ++curr_good;
                        if (cnt[tp] == R+1) --curr_good;
                    }
                    ++cpr[qp];}
                }
            }
        }
        ans += curr_good;
    }

    cout << ans << endl;
}


#include <cassert>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T gcd(T a, T b) { return b == 0? a : gcd(b, a % b); }
template <typename T> T abs(T x) { return x < 0? -x : x; }

const int MAXK = 31;

int f[MAXK];
int g[MAXK];
int ans;
int n, m;

int main()
{
	#ifdef MJUDGE
		assert(freopen("in", "r", stdin));
		assert(freopen("out", "w", stdout));
	#endif

	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
	{
		int x;
		scanf("%d", &x);
		for (int j = 0; j < MAXK; j++)
			if ((1 << j) & x)
				f[j] += 1;
	}
	for (int i = 0; i < m; i++)
	{
		int x;
		scanf("%d", &x);
		if (x >= MAXK)
			continue;
		g[x] += 1;
	}

	ans = 0;
	for (int i = 0; i < MAXK; i++)
	{
		long long x = f[i] * (long long)(1 << i);
		for (int j = 0; j <= i; j++)
		{
			long long v = min(x, (long long)g[j]);
			ans += v;
			g[j] -= v;
			x = (x - v) / 2;
		}
	}
	printf("%d\n", ans);
	fprintf(stderr, "Time execute: %.3lfs\n", clock() / (double)CLOCKS_PER_SEC);
	return 0;
}


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

// Driver Code
int main()
{
    ll n,m,a,b,c,d,x,y,x1,x2,y1,y2;

    cin>>n>>m;
    ll xx[n];
    a=0;
    b=0;
    for(ll i=0;i<n;i++)
    {
        cin>>xx[i];
        if(xx[i]==1)
        {
            a++;
        }
        else
        {
            b++;
        }

    }
    ll ma=0
    for(ll i=0;i<m;i++)
   {
       x=a;
       y=b;
       for(ll j=i;j<n;j=j+m)
       {
           if(xx[j]==1)
           {
               x--;
           }
           else
           {
               y--;
           }
       }
       x=abs(x-y);
       ma=max(ma,x);

   }
   cout<<ma<<endl;

    return 0;
}


# include <bits/stdc++.h>
# define ll long long
# define MOD 1000000007

using namespace std;

int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

ll modpow (ll a, ll b)
{
    if (b == 0)
        return 1;
    long long res = modpow (a, b/2);
    res = (res * res) % MOD;
    if (b % 2 == 1)
        res = (res * a) % MOD;
    return res;
}

ll calc (ll n, ll k)
{
    ll acc = 1;
    ll fact = 1;
    ll sum = 1;
    ll term;
    ll x, y;
    for (int i = 1; i <= k; i++)
    {
        fact *= i;
        gcd (fact, MOD, x, y);
        x = (x % MOD + MOD) % MOD;
        acc = ((acc * (n - i + 1)) % MOD;
        acc = (acc * x) % MOD;
        sum += acc;
    }
    return sum;
}

void solve ()
{
    ll n, k, val;
    cin >> n >> k;
    if (k >= n)
    {
        cout << modpow (2, n) << '\n';
        return;
    }
    if (k > n/2)
        cout << modpow (2, n) - calc (n, n - k - 1) << '\n';
    else
        cout << calc (n, k) << '\n';
}

int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
}


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

#define ll long long
ll k;

ll split(ll i) { // Interval i [(k2+1)*i+1, .., (k2+1)*(i+1)]
    if (!i) return k*(k+1) / 2;
    ll s = split(i/k);
    ll O  = i/k;
    ll o = i%k;
    s -= O * (k*k+1) + o * k;
    ll off = s<=1 ? k : s>k ? 0 : k+1 - s;
    return k * (O*(k*k+1) + o*k) + k*(k+1)/2 + off;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t; cin >> t;
    while (t--) {
        ll n; cin >> n >> k;
        ll i = (n-1)/(k*k+1);
        ll s = split(i,k);
        if (s==n) {
            cout << (i+1)*(k+1) << '\n';
        } else {
            ll o = n-i*(k*k+1);
            // printf("n%lld k%lld i%lld o%lld O%lld s%lld\n",
                    // n,k,i,o,i*k*(k+1),s);
            ll ans = i*k*(k+1) + o + (o-1-(n>s))/k - (n>s);
            cout << ans << '\n';
        }
    }
}


#include<iostream>
#include<cmath>
using namespace std;
long long int t,i,a1,a2,k1,k2,n,copt;
int main()
{
	cin >> a1 >> a2 >> k1 >> k2 >> n;
	copt = n-a1*(k1-1)-a2*(k2-1);
	if (copt > a1+a2)
		cout << a1+a2 << " ";
	else
		if (cout > 0)
			cout << copt << " ";
	if (k1 < k2)
	{
		if (n>=k1*a1+k2*a2)
			cout << a1+a2;
		else
		{
			if (n>k1*a1)
				cout << a1 + (n-a1*k1)/k2;
			else
				cout << n/k1;
		}
	}
	else
	{
		if (n>=k1*a1+k2*a2)
			cout << a1+a2;
		else
		{
			if (n>k2*a2)
				cout << a2 + (n-a2*k2)/k1;
			else
				cout << n/k2;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#ifndef _LIB_POLYNOMIAL_RING
#define _LIB_POLYNOMIAL_RING
#include <bits/stdc++.h>
#ifndef _LIB_MODULAR_INTEGER
#define _LIB_MODULAR_INTEGER
#include <bits/stdc++.h>
#ifndef _LIB_NUMBER_THEORY
#define _LIB_NUMBER_THEORY
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace nt {
  int64_t inverse(int64_t a, int64_t b) {
    long long b0 = b, t, q;
    long long x0 = 0, x1 = 1;
    if (b == 1) return 1;
    while (a > 1) {
      q = a / b;
      t = b, b = a % b, a = t;
      t = x0, x0 = x1 - q * x0, x1 = t;
    }
    if (x1 < 0) x1 += b0;
    return x1;
  }
}
}  // namespace lib

#endif

#if __cplusplus < 201300
#error required(c++14)
#endif

namespace lib {
  using namespace std;
namespace {
  template<typename T, T ...Mods>
  struct ModularIntegerBase {
    typedef ModularIntegerBase<T, Mods...> type;

    T x[sizeof...(Mods)];
    friend ostream& operator<<(ostream& output, const type& var) {
      output << "(";
      for(int i = 0; i < sizeof...(Mods); i++) {
        if(i) output << ", ";
        output << var.x[i];
      }
      return output << ")";
    }
  };

  template<typename T, T Mod>
  struct ModularIntegerBase<T, Mod> {
    typedef ModularIntegerBase<T, Mod> type;

    T x[1];

    explicit operator T() const { return this->x[0]; }
    explicit operator long long() const { return this->x[0]; }
    explicit operator double() const { return this->x[0]; }
    explicit operator long double() const { return this->x[0]; }
    friend ostream& operator<<(ostream& output, const type& var) {
      return output << var.x[0];
    }
  };

  template<typename T, class Enable, T ...Mods>
  struct ModularIntegerImpl : ModularIntegerBase<T, Mods...> {
    typedef ModularIntegerImpl<T, Enable, Mods...> type;
    typedef T type_int;
    typedef int64_t large_int;
    const static size_t n_mods = sizeof...(Mods);

    struct Less {
      bool operator()(const type& lhs, const type& rhs) const {
        for(size_t i = 0; i < sizeof...(Mods); i++)
          if(lhs.x[i] != rhs.x[i])
            return lhs.x[i] < rhs.x[i];
        return false;
      };
    };
    typedef Less less;

    constexpr static T mods[sizeof...(Mods)] = {Mods...};
    using ModularIntegerBase<T, Mods...>::x;

    ModularIntegerImpl() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = T();
    }
    ModularIntegerImpl(T y) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = y;
      normalize();
    }
    static type with_remainders(T y[sizeof...(Mods)]) {
      type res;
      for(size_t i = 0; i < sizeof...(Mods); i++)
        res.x[i] = y[i];
      res.normalize();
      return res;
    }

    void normalize() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((x[i] %= mods[i]) < 0)
          x[i] += mods[i];
    }

    inline T operator[](int i) const { return x[i]; }

    inline T multiply(T a, T b, T mod) const {
      return (large_int)a*b % mod;
    }

    inline T inv(T a, T mod) const {
      return static_cast<T>(nt::inverse(a, mod));
    }

    type inverse() const {
      T res[sizeof...(Mods)];
      for(size_t i = 0; i < sizeof...(Mods); i++)
        res[i] = inv(x[i], mods[i]);
      return type::with_remainders(res);
    }

    template<typename U>
    T power(T a, U p, T mod) {
      if(mod == 1) return T();
      if(p < 0) {
        if(a == 0)
          throw domain_error("0^p with negative p is invalid");
        p = -p;
        a = inv(a, mod);
      }
      if(p == 0) return T(1);
      if(p == 1) return a;
      T res = 1;
      while(p > 0) {
        if(p&1)
          res = multiply(res, a, mod);
        p >>= 1;
        a = multiply(a, a, mod);
      }
      return res;
    }

    type& operator+=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        (x[i] += rhs.x[i]) %= mods[i];
      return *this;
    }
    type& operator-=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((x[i] -= rhs.x[i]) < 0)
          x[i] += mods[i];
      return *this;
    }
    type& operator*=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], rhs.x[i], mods[i]);
      return *this;
    }
    type& operator/=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], inv(rhs.x[i], mods[i]), mods[i]);
      return *this;
    }

    type& operator+=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] += rhs;
      normalize();
      return *this;
    }

    type& operator-=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] -= rhs;
      normalize();
      return *this;
    }

    type& operator*=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], rhs, mods[i]);
      return *this;
    }

    type& operator/=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(inv(rhs, mods[i]), x[i], mods[i]);
      return *this;
    }

    type& operator^=(large_int p) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = power(x[i], p, mods[i]);
      return *this;
    }

    type& operator++() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((++x[i]) >= mods[i])
          x[i] -= mods[i];
      return *this;
    }
    type& operator--() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((--x[i]) < 0)
          x[i] += mods[i];
      return *this;
    }
    type& operator++(int unused) { type res = *this; ++(*this); return res; }
    type& operator--(int unused) { type res = *this; --(*this); return res; }

    friend type operator+(const type& lhs, const type& rhs) {
      type res = lhs;
      return res += rhs;
    }
    friend type operator-(const type& lhs, const type& rhs) {
      type res = lhs;
      return res -= rhs;
    }
    friend type operator*(const type& lhs, const type& rhs) {
      type res = lhs;
      return res *= rhs;
    }
    friend type operator/(const type& lhs, const type& rhs) {
      type res = lhs;
      return res /= rhs;
    }

    friend type operator+(const type& lhs, T rhs) {
      type res = lhs;
      return res += rhs;
    }

    friend type operator-(const type& lhs, T rhs) {
      type res = lhs;
      return res -= rhs;
    }

    friend type operator*(const type& lhs, T rhs) {
      type res = lhs;
      return res *= rhs;
    }

    friend type operator/(const type& lhs, T rhs) {
      type res = lhs;
      return res /= rhs;
    }

    friend type operator^(const type& lhs, large_int rhs) {
      type res = lhs;
      return res ^= rhs;
    }

    type operator-() const {
      type res = *this;
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if(res.x[i])
          res.x[i] = mods[i] - res.x[i];
      return res;
    }

    friend bool operator==(const type& lhs, const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if(lhs.x[i] != rhs.x[i])
          return false;
      return true;
    }
    friend bool operator!=(const type& lhs, const type& rhs) {
      return !(lhs == rhs);
    }

    friend istream& operator>>(istream& input, type& var) {
      T y; cin >> y;
      var = y;
      return input;
    }
  };
}

  // Explicitly make constexpr available for linkage.
  template<typename T, class Enable, T ...Mods>
  constexpr T ModularIntegerImpl<T, Enable, Mods...>::mods[];

  template<typename T, T ...Mods>
  using ModularInteger =
    ModularIntegerImpl<T,
                       typename enable_if<is_integral<T>::value>::type,
                       Mods...>;

  template<int32_t ...Mods>
  using Mint32 = ModularInteger<int32_t, Mods...>;

  template<int64_t ...Mods>
  using Mint64 = ModularInteger<int64_t, Mods...>;
}  // namespace lib

#endif
#ifndef _LIB_TRAITS
#define _LIB_TRAITS
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace traits {

  /// keep caide
  template<typename Iterator>
  using IteratorCategory =
    typename iterator_traits<Iterator>::iterator_category;

  /// keep caide
  template<typename Container>
  using IteratorCategoryOf = IteratorCategory<typename Container::iterator>;

  /// keep caide
  template<typename Iterator>
  using IsRandomIterator =
    is_base_of<random_access_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Iterator>
  using IsInputIterator =
    is_base_of<input_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Iterator>
  using IsBidirectionalIterator =
    is_base_of<bidirectional_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Container>
  using HasRandomIterator =
    is_base_of<random_access_iterator_tag, IteratorCategoryOf<Container>>;

  /// keep caide
  template<typename Container>
  using HasInputIterator =
    is_base_of<input_iterator_tag, IteratorCategoryOf<Container>>;

  /// keep caide
  template<typename Container>
  using HasBidirectionalIterator =
    is_base_of<bidirectional_iterator_tag, IteratorCategoryOf<Container>>;
}
}

#endif
#ifndef _LIB_FFT
#define _LIB_FFT
#include <bits/stdc++.h>
#ifndef _LIB_COMPLEX
#define _LIB_COMPLEX
#include <bits/stdc++.h>

namespace lib {
  template <typename T> struct Complex {
    T re, im;
    Complex(T a = T(), T b = T()) : re(a), im(b) {}
    T real() const { return re; }
    void operator+=(const Complex<T>& rhs) { re += rhs.re, im += rhs.im; }
    void operator-=(const Complex<T>& rhs) { re -= rhs.re, im -= rhs.im; }
    void operator*=(const Complex<T>& rhs) {
      tie(re, im) = make_pair(re * rhs.re - im * rhs.im, re * rhs.im + im * rhs.re);
    }
    Complex<T> operator+(const Complex<T>& rhs) {
      Complex<T> res = *this;
      res += rhs;
      return res;
    }
    Complex<T> operator-(const Complex<T>& rhs) {
      Complex<T> res = *this;
      res -= rhs;
      return res;
    }
    Complex<T> operator*(const Complex<T>& rhs) {
      Complex<T> res = *this;
      res *= rhs;
      return res;
    }
    void operator/=(const T x) { re /= x, im /= x; }
  };
}  // namespace lib

#endif

namespace lib {
  using namespace std;
namespace linalg {
namespace internal {
  typedef long double fft_type;
  typedef Complex<fft_type> cd;
  typedef vector<cd> vcd;

  const fft_type PI = acosl(-1);
  vector<int> rev;
  vcd fa, fb;

  // function used to precompute rev for fixed size fft (n is a power of two)
  void dft_rev(int n) {
    int lbn = __builtin_ctz(n);
    if((int)rev.size() < (1 << lbn)) rev.resize(1 << lbn);
    int h = -1;
    for (int i = 1; i < n; i++) {
      if ((i & (i - 1)) == 0) h++;
      rev[i] = rev[i ^ (1 << h)] | (1 << (lbn - h - 1));
    }
  }

  void dft_iter(cd* p, int n) {
    for (int L = 2; L <= n; L <<= 1) {
      fft_type ang = PI * 2 / L;
      cd step = cd(cosl(ang), sinl(ang)); // root
      // for (int i = L; i < root_sz; i <<= 1) NTT HERE
      //   step = step * step % MOD;
      for (int i = 0; i < n; i += L) {
        cd w = 1;
        for (int j = 0; j < L / 2; j++) {
          cd x = p[i + j];
          cd y = p[i + j + L / 2] * w;
          p[i + j] = x + y;
          p[i + j + L / 2] = x - y;
          w *= step;
        }
      }
    }
  }

  void dft(cd* p, int n) {
    for (int i = 0; i < n; i++)
      if (i < rev[i]) swap(p[i], p[rev[i]]);
    dft_iter(p, n);
  }

  void idft(cd* p, int n) {
    dft(p, n);
    reverse(p+1, p+n);
    for (int i = 0; i < n; i++) p[i] /= n;
  }
}

  template<typename T>
  void raw_fft(const vector<T>& a, const vector<T>& b) {
    int n = max(a.size(), b.size());
    n = 1 << (32 - __builtin_clz(n) + ((n & (n-1)) != 0));
    if((int)internal::fa.size() < n) internal::fa.resize(n);
    if((int)internal::fb.size() < n) internal::fb.resize(n);
    for(size_t i = 0; i < a.size(); i++)
      internal::fa[i] = (internal::fft_type) a[i];
    for(size_t i = 0; i < b.size(); i++)
      internal::fb[i] = (internal::fft_type) b[i];
    fill(internal::fa.begin() + a.size(), internal::fa.end(), 0);
    fill(internal::fb.begin() + b.size(), internal::fb.end(), 0);
    internal::dft_rev(n);
    internal::dft(internal::fa.data(), n);
    internal::dft(internal::fb.data(), n);
    for(int i = 0; i < n; i++) internal::fa[i] *= internal::fb[i];
    internal::idft(internal::fa.data(), n);
  }

  template<typename T>
  internal::vcd fft(const vector<T>& a, const vector<T>& b) {
    raw_fft(a, b);
    internal::vcd& raw = internal::fa;
    int sz = raw.size();
    vector<T> res(sz);
    for(int i = 0; i < sz; i++)
      res[i] = raw[i];
    return res;
  }

  template<typename T>
  vector<T> rounded_fft(const vector<T>& a, const vector<T>& b) {
    raw_fft(a, b);
    internal::vcd& raw = internal::fa;
    int sz = raw.size();
    vector<T> res(sz);
    for(int i = 0; i < sz; i++)
      res[i] = (long long)(raw[i].real() + 0.5);
    return res;
  }

  // TODO: optimize this
  template<typename M>
  vector<M> mod_split_fft(const vector<M>& a, const vector<M>& b) {
    typedef typename M::type_int type_int;
    static_assert(sizeof(M::mods) / sizeof(type_int) == 1,
        "cant multiply with multiple mods");
    type_int base = sqrtl(M::mods[0]) + 0.5;
    M base_m = base;
    int sza = a.size();
    int szb = b.size();

    vector<M> a0(sza), a1(sza);
    for(int i = 0; i < sza; i++) {
      a0[i] = (type_int)a[i] % base;
      a1[i] = (type_int)a[i] / base;
    }

    vector<M> b0(szb), b1(szb);
    for(int i = 0; i < szb; i++) {
      b0[i] = (type_int)b[i] % base;
      b1[i] = (type_int)b[i] / base;
    }

    vector<M> a01 = a0, b01 = b0;
    for(int i = 0; i < sza; i++) a01[i] += a1[i];
    for(int i = 0; i < szb; i++) b01[i] += b1[i];
    vector<M> C = rounded_fft(a01, b01);
    vector<M> a0b0 = rounded_fft(a0, b0);
    vector<M> a1b1 = rounded_fft(a1, b1);

    vector<M> mid = C;
    int sz = C.size();
    for(int i = 0; i < sz; i++) {
      mid[i] -= a0b0[i];
      mid[i] -= a1b1[i];
    }

    vector<M> res = a0b0;
    for(int i = 0; i < sz; i++)
      res[i] += base_m * mid[i];
    base_m *= base_m;
    for(int i = 0; i < sz; i++)
      res[i] += base_m * a1b1[i];
    return res;
  }
}  // namespace linalg
}  // namespace lib

#endif
#ifndef _LIB_MATH
#define _LIB_MATH
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace math {

  /// caide keep
  template<typename Type>
  struct DefaultPowerOp {
    Type operator()() const { return Type(1); }
    Type operator()(const Type& a) const { return a; }
    void operator()(Type& x, const Type& a, long long cur) const {
      x *= x;
      if(cur & 1)
        x *= a;
    }
  };

  template<typename Type, typename Op>
  Type generic_power(const Type& a, long long n, Op op = DefaultPowerOp<Type>()) {
    if(n == 0) return op();
    Type res = op(a);
    int hi = 63 - __builtin_clzll(n);
    for(int i = hi - 1; ~i; i--) {
      op(res, a, n >> i);
    }
    return res;
  }
}  // namespace math
}  // namespace lib

#endif

namespace lib {
  using namespace std;
namespace math {
namespace poly {

  namespace {
    /// keep caide
    using traits::IsInputIterator;
    /// keep caide
    using traits::HasInputIterator;
  }

  struct FastMultiplication {
    template<typename Field>
    vector<Field> operator()(const vector<Field>& a, const vector<Field>& b) const {
      return linalg::rounded_fft(a, b);
    }
  };

  struct SafeMultiplication {
    template<typename Field>
    vector<Field> operator()(const vector<Field>& a, const vector<Field>& b) const {
      return linalg::mod_split_fft(a, b);
    };
  };

  struct NaiveMultiplication {
    template<typename Field>
    vector<Field> operator()(const vector<Field>& a, const vector<Field>& b) const {
      vector<Field> res(a.size() + b.size());
      for(size_t i = 0; i < a.size(); i++) {
        for(size_t j = 0; j < b.size(); j++) {
          res[i + j] += a[i] * b[j];
        }
      }
      return res;
    }
  };

  template<typename P>
  struct DefaultPowerOp {
    int mod;
    DefaultPowerOp(int mod) : mod(mod) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) const { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      (x *= x) %= mod;
      if(cur & 1)
        (x *= a) %= mod;
    }
  };

  template<typename P>
  struct ModPowerOp {
    const P& mod;
    ModPowerOp(const P& p) : mod(p) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      (x *= x) %= mod;
      if(cur & 1)
        (x *= a) %= mod;
    }
  };

  template<typename P>
  struct ModShiftPowerOp {
    const P& mod;
    ModShiftPowerOp(const P& p) : mod(p) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      //if(cur < mod.degree())
        //x = P::kth(cur);
      if(cur & 1)
        (x *= (x << 1)) %= mod;
      else
        (x *= x) %= mod;
    }
  };

  template<typename Field, typename Mult>
  struct Polynomial {
    typedef Polynomial<Field, Mult> type;
    typedef Field field;
    vector<Field> p;

    Polynomial() : p(0) {}
    explicit Polynomial(Field x): p(1, x) {}

    template<
      typename Iterator,
      typename enable_if<IsInputIterator<Iterator>::value>::type* = nullptr>
    Polynomial(Iterator begin, Iterator end) : p(distance(begin, end)) {
      int i = 0;
      for(auto it = begin; it != end; ++it, ++i)
        p[i] = *it;
      normalize();
    }

    template<
      typename Container,
      typename enable_if<HasInputIterator<Container>::value>::type* = nullptr>
    Polynomial(const Container& container)
      : Polynomial(container.begin(), container.end()) {}

    Polynomial(const initializer_list<Field>& v)
      : Polynomial(v.begin(), v.end()) {}

    static type from_root(const Field& root) {
      return Polynomial({-root, 1});
    }

    void normalize() const {
      type* self = const_cast<type*>(this);
      int sz = self->p.size();
      while(sz > 0 && self->p[sz-1] == 0) sz--;
      if(sz != (int)self->p.size())
        self->p.resize(sz);
    }

    inline int size() const { return p.size(); }
    inline int degree() const { return max((int)p.size()-1, 0); }
    bool null() const {
      for(Field x : p)
        if(x != 0) return false;
      return true;
    }

    inline Field operator[](const int i) const {
      if(i >= size())
        return 0;
      return p[i];
    }
    inline Field& operator[](const int i) {
      if(i >= size())
        p.resize(i + 1);
      return p[i];
    }

    type& operator+=(const type& rhs) {
      if(rhs.size() > size())
        p.resize(rhs.size());
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] += rhs[i];
      normalize();
      return *this;
    }

    type& operator-=(const type& rhs) {
      if(rhs.size() > size())
        p.resize(rhs.size());
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] -= rhs[i];
      normalize();
      return *this;
    }

    type& operator*=(const type& rhs) {
      p = Mult()(p, rhs.p);
      normalize();
      return *this;
    }

    type& operator*=(const Field& rhs) {
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] *= rhs;
      normalize();
      return *this;
    }

    type& operator/=(const Field& rhs) {
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] /= rhs;
      normalize();
      return *this;
    }

    type& operator<<=(const int rhs) {
      if(rhs < 0) return *this >>= rhs;
      if(rhs == 0) return *this;
      int sz = size();
      p.resize(sz + rhs);
      for(int i = sz-1; i >= 0; i--)
        p[i + rhs] = p[i];
      fill_n(p.begin(), rhs, 0);
      return *this;
    }

    type& operator>>=(const int rhs) {
      if(rhs < 0) return *this <<= rhs;
      if(rhs == 0) return *this;
      int sz = size();
      if(rhs >= sz) {
        p.clear();
        return *this;
      }
      for(int i = rhs; i < sz; i++)
        p[i - rhs] = p[i];
      p.resize(sz - rhs);
      return *this;
    }

    type& operator%=(const int rhs) {
      if(rhs < size())
        p.resize(rhs);
      normalize();
      return *this;
    }

    type& operator/=(const type& rhs) {
      return *this = *this / rhs;
    }

    type operator%=(const type& rhs) {
      return *this = *this % rhs;
    }

    type operator+(const type& rhs) const {
      type res = *this;
      return res += rhs;
    }

    type operator-(const type& rhs) const {
      type res = *this;
      return res -= rhs;
    }

    type operator*(const type& rhs) const {
      return type(Mult()(p, rhs.p));
    }

    type operator*(const Field& rhs) const {
      type res = *this;
      return res *= rhs;
    }

    type operator/(const Field& rhs) const {
      type res = *this;
      return res /= rhs;
    }

    type operator<<(const int rhs) const {
      type res = *this;
      return res <<= rhs;
    }

    type operator>>(const int rhs) const {
      type res = *this;
      return res >>= rhs;
    }

    type operator%(const int rhs) const {
      return Polynomial(p.begin(), p.begin() + min(rhs, size()));
    }

    type operator/(const type& rhs) const {
      return type::divmod(*this, rhs).first;
    }

    type operator%(const type& rhs) const {
      return type::divmod(*this, rhs).second;
    }

    bool operator==(const type& rhs) const {
      normalize();
      rhs.normalize();
      return p == rhs.p;
    }

    type inverse() const {
      int sz = size();
      if(sz == 0)
        return type();
      if(sz == 1) {
        assert(p[0] != 0);
        return type(p[0].inverse());
      }
      type q = (*this % ((sz+1)/2)).inverse();
      q -= (*this * q % sz - type(1)) * q % sz;
      return q % sz;
    }

    type reciprocal() const {
      normalize();
      return type(p.rbegin(), p.rend());
    }

    type integral() const {
      int sz = size();
      if(sz == 0) return {};
      type res = *this;
      for(int i = sz; i; i--) {
        res[i] = res[i-1] / i;
      }
      res[0] = 0;
      res.normalize();
      return res;
    }

    type derivative() const {
      int sz = size();
      if(sz == 0) return {};
      type res = *this;
      for(int i = 0; i + 1 < sz; i++) {
        res[i] = res[i+1] * (i + 1);
      }
      res.p.back() = 0;
      res.normalize();
      return res;
    }

    static pair<type, type> divmod(const type& a, const type& b) {
      a.normalize();
      b.normalize();
      int m = a.size();
      int n = b.size();
      if(m < n) return {Polynomial(), a};
      type ar = a.reciprocal();
      type bri = (b.reciprocal() % (m - n + 1)).inverse();
      type q = (ar * bri % (m - n + 1)).reciprocal();
      type r = a - b * q;

      if(r.degree() >= b.degree()) {
        q += divmod(r, b).first;
        r = a - b * q;
      }

      return {q, r};
    }

    static type power(const type& a, long long n, const int mod) {
      return math::generic_power<type>(a, n, DefaultPowerOp<type>(mod));
    }

    static type power(const type& a, long long n, const type& mod) {
      return math::generic_power<type>(a, n, ModPowerOp<type>(mod));
    }

    static type kth(int K) {
      return type(1) << K;
    }

    static type kth(long long K, const type& mod) {
      return math::generic_power<type>(type(1) << 1, K, ModShiftPowerOp<type>(mod));
    }

    friend ostream& operator<<(ostream& output, const type& var) {
      output << "[";
      int sz = var.size();
      for(int i = sz-1; i >= 0; i--) {
        output << var[i];
        if(i) output << " ";
      }
      return output << "]";
    }
  };
}  // namespace poly
/// keep caide
using poly::Polynomial;
}  // namespace math
}  // namespace lib

#endif
#ifndef _LIB_LINEAR_RECURRENCE
#define _LIB_LINEAR RECURRENCE
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace linalg {
  namespace {
    using traits::IsRandomIterator;
    using traits::HasRandomIterator;
  }

  template<typename P>
  struct BMSolver {
    typedef BMSolver<P> type;
    typedef typename P::field field_type;
    typedef P poly_type;

    vector<field_type> base;
    vector<field_type> T;

    template<
      typename Iterator,
      typename enable_if<IsRandomIterator<Iterator>::value>::type* = nullptr>
    void solve(Iterator begin, Iterator end) {
      auto get = [begin](int i) { return *(begin + i); };

      int n = distance(begin, end);

      vector<field_type> C = {1}, B = {1};
      field_type b = 1;
      int L = 0;

      for(int i = 0, x = 1; i < n; i++, x++) {
        // evaluate new element
        field_type d = 0;
        for(size_t j = 0; j < C.size(); j++)
          d += get(i-j) * C[j];
        if(d == 0) continue;
        if(2 * L <= i) {
          auto tmp = C;
          if(C.size() < B.size() + x)
            C.resize(B.size() + x);
          field_type coef = d / b;
          for(size_t j = 0; j < B.size(); j++)
            C[j + x] -= coef * B[j];
          L = i + 1 - L;
          B = tmp;
          b = d;
          x = 0;
        } else {
          if(C.size() < B.size() + x)
            C.resize(B.size() + x);
          field_type coef = d / b;
          for(size_t j = 0; j < B.size(); j++)
            C[j + x] -= coef * B[j];
        }
      }

      T = vector<field_type>((int)C.size() - 1);
      for(size_t i = 0; i < T.size(); i++) T[i] = -C[i+1];
      base = vector<field_type>(begin, end);
    }

    template<
      typename Container,
      typename enable_if<HasRandomIterator<Container>::value>::type* = nullptr>
    void solve(const Container& container) {
      solve(container.begin(), container.end());
    }

    void solve(const initializer_list<field_type>& l) {
      solve(l.begin(), l.end());
    }

    bool solved() const {
      return T.size() > 0 && base.size() >= T.size();
    }

    void ensure(int nsz) const {
      auto* self = const_cast<type*>(this);
      for(int j = base.size(); j < nsz; j++) {
        field_type acc = 0;
        for(int i = 0; i < (int)T.size(); i++)
          acc += base[j - i - 1] * T[i];
        self->base.push_back(acc);
      }
    }

    poly_type mod_function() const {
      poly_type res;
      int m = T.size();
      res[m] = 1;
      for(int i = 0; i < m; i++)
        res[i] = -T[m-i-1];
      return res;
    }

    vector<field_type> compute(long long K, int n) {
      assert(n > 0);
      assert(solved());
      vector<field_type> res;
      int N = T.size();
      int cons = min(n, N);

      if(K < (int)base.size()) {
        for(int j = 0; j < n && K + j < (int)base.size(); j++)
          res.push_back({base[K + j]});

        while((int)res.size() < cons) {
          field_type acc = 0;
          int sz = res.size();
          int mid = min(sz, N);
          for(int i = 0; i < mid; i++)
            acc += res[sz - i - 1] * T[i];
          sz = base.size();
          for(int i = mid; i < N; i++)
            acc += base[sz - 1 - (i - mid)] * T[i];
          res.push_back(acc);
        }
      } else {
        ensure(cons + N - 1);

        poly_type x = poly_type::kth(K, mod_function());

        for(int j = 0; j < cons; j++) {
          field_type acc = 0;
            for(int i = 0; i < N; i++)
              acc += x[i] * base[i + j];
          res.push_back(acc);
        }
      }

      for(int j = res.size(); j < n; j++) {
        field_type acc = 0;
        for(int i = 0; i < N; i++)
          acc += res[j - i - 1] * T[i];
        res.push_back(acc);
      }
      return res;
    }

    field_type compute(long long K) {
      return compute(K, 1)[0];
    }
  };
}  // namespace linalg
}  // namespace lib

#endif
#define int long long
using namespace std;

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ms(v, x) memset((v), (x), sizeof(v))
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define iopt ios::sync_with_stdio(false); cin.tie(0)

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
int power(int x, int p, int MOD) {
    if(p == 0) return 1%MOD;
    if(p == 1) return x%MOD;
    int res = power(x, p/2, MOD);
    res = (long long)res*res%MOD;
    if(p&1) res = (long long)res*x%MOD;
    return res;
}

typedef pair<int, int> ii;
typedef long double LD;
typedef vector<int> vi;

using namespace lib;

const int MOD = (int)10007;
using Field = Mint64<MOD>;
using Poly = math::Polynomial<Field, math::poly::NaiveMultiplication>;

const int N = 202;
Field dp[N][N][6*N];
bool ok[N][N][6*N];
string s;

Field go(int left, int right, int i) {
  if(i < 0) return 0;
  if(i == 0 && left >= right) return 1;
  Field & res = dp[left][right][i];
  if(ok[left][right][i]) return res;
  res = 0;

  if(left >= right) {
    res += go(left, right, max(i - 2, 0LL)) * 26;
  } else if (left + 1 == right) {
    if(i == 0) res += go(left + 1, right - 1, i);
    else res += go(left + 1, right, i - 1);
    res += go(left, right, i - 2) * 25;
  } else {
    if(s[left] == s[right - 1]) {
      res += go(left + 1, right - 1, i);
      res += go(left, right, i - 2) * 25;
    } else {
      res += go(left + 1, right, i - 1);
      res += go(left, right - 1, i - 1);
      res += go(left, right, i - 2) * 24;
    }
  }

  ok[left][right][i] = true;
  return res;
}

int32_t main(){
  linalg::BMSolver<Poly> solver;
  vector<Field> base;
  cin >> s;
  int n;
  cin >> n;

  for(size_t i = 0; i < 3 * s.size(); i++) {
    base.pb(go(0, s.size(), i));
  }

  solver.solve(base);
  cout << solver.compute(n) << endl;
}


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

int main() {
    int n;
    bool one = true, two = true, three = false, flag = 1;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        if(!x) {
            flag = 0;
        }
        one = !one; two = !two; three = !three;
        if(x == 1)
            one = !one;
        else if(x == 2)
            two = !two;
        else
            three = !three;
    }
    if(!flag)
        cout << "NO";
    else
        cout << "YES"
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
 int main()
 {
long long n,k,M,D,mx=-1;
scanf("%lld%lld%lld%lld",&n,&k,&M,&D);
for(long long int  e=1;e<=D;e++)
{   long long int   ev=(k*(e-1)+1);
	long long int  gh=n/ev;
 long long  int y=min( gh,M);
 mx=max(mx,y*e);
}
printf("%lld\n",mx);

}#include <bits/stdc++.h>
using namespace std;
 int main()
 {
long long n,k,M,D,mx=-1;
scanf("%lld%lld%lld%lld",&n,&k,&M,&D);
for(long long int  e=1;e<=D;e++)
{   long long int   ev=(k*(e-1)+1);
	long long int  gh=n/ev;
 long long  int y=min( gh,M);
 mx=max(mx,y*e);
}
printf("%lld\n",mx);

}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#pragma comment(linker, "/STACK:200000000")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <iomanip>
#include <queue>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define mset(mas,val) memset(mas,val,sizeof(mas))
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define fornd(i,n) for (int i=int(n)-1; i>=0; --i)
#define forab(i,a,b) for (int i=int(a); i<int(b); ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int INF = (int) 1e9;
const long long INF64 = (long long) 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;

const int N = 100;
int n, m;
int a[N][N];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

struct edge {
	int to,	f, cap, cost, rev;
};

vector < edge > g[N * N];
int prio[N * N], curflow[N * N], prevedge[N * N], prevnode[N * N], q[N * N], pot[N * N], inqueue[N * N];

void add_edge(int s, int t, int cap, int cost) {
	edge fi = {t, 0, cap, cost, sz(g[t])};
	edge se = {s, 0, 0, -cost, sz(g[s])};
	g[s].push_back(fi);
	g[t].push_back(se);
}
int NN = N * N;
int mincost() {
	int flow = 0, maxflow = (n * m) >> 1;
	int answer = 0;
	int s = 0;
	int t = n * m + 2;
	while (flow < maxflow) {
		priority_queue< ll, vector <ll>, greater<ll> > q;
		q.push(s);
		fill(prio, prio + NN, INT_MAX);
		prio[s] = 0;
		curflow[s] = INT_MAX;
		while (!q.empty()) {
			ll cur = q.top();
			int d = cur >> 32;
			int u = cur;
			q.pop();
			if (d != prio[u]) continue;
			forn(i, sz(g[u])) {
				edge &e = g[u][i];
				int v = e.to;
				if (e.cap <= e.f) continue;
				int nprio = prio[u] + e.cost + pot[u] - pot[v];
				if (prio[v] > nprio) {
					prio[v] = nprio;
					q.push(((ll) nprio << 32) + v);
					prevnode[v]  = u;
					prevedge[v] = i;
					curflow[v] = min(curflow[u], e.cap - e.f);
				}
			}
		}
		if (prio[t] == INT_MAX)
			break;
		for (int i = 0; i < NN; i++)
			pot[i] += prio[i];
		int df = min(curflow[t], maxflow - flow);
			flow += df;
		for (int v = t; v != s; v = prevnode[v]) {
		  edge &e = g[prevnode[v]][prevedge[v]];
		  e.f += df;
		  g[v][e.rev].f -= df;
		  answer += df * e.cost;
		}
	}
	return answer;
}

inline bool read()
{
	if (scanf("%d %d", &n, &m) != 2) return false;
	forn(i,n) {
		forn(j, m) {
			assert(scanf("%d", &a[i][j]) == 1);
		}
	}
	return true;
}

void solve()
{
	forn(i,n) {
		forn(j,m) {
			if (((i + j) & 1) == 0) {
				forn(cur, 4) {
					int nx = i + dx[cur];
					int ny = j + dy[cur];
					if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
						add_edge(i * m + j + 1, nx * m + ny + 1, 1, (a[i][j] != a[nx][ny]));
					}
				}
				add_edge(0, i * m + j + 1, 1, 0);
			} else {
				add_edge(i * m + j + 1, n * m + 2, 1, 0);
			}
		}
	}
	cout << mincost() << endl;
}

int main()
{
	while (read()) solve();
}


//
//  main.cpp
//  E. Unsolvable
//
//  Created by Farzad Sharbafian on 9/19/12.
//  Copyright (c) 2012 Farzad Sharbafian. All rights reserved.
//

// Algorithm    :
// Type         :
// Order        :

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <string>
#include <math.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <bitset>
#include <complex>
using namespace std;

typedef complex<double> point;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define Debug
#define X real()
#define Y imag()
#define Abs(x) ((x)>0?(x):(-(x)))
#define INF 0x7fffffff
#define EPS 1e-7
#define pow2(X) ((X)*(X))
#define LL long long

#define MAX 1000100000
bool v[MAX];

#define Mod 1000000007

LL Pow(LL r, LL n) {
    if(n==0)
        return 1;
    LL _pow = Pow(r, n/2);
    return (((_pow * _pow)%Mod)*(n%2?r:1))%Mod;
}
int main () {
    // these are Mersenne prime! i cheated from net! :-"
    LL M[] = {2,3,5,7,13,17,19,31,61,89,107,127,521,607,1279,
        2203,2281,3217,4253,4423,9689,9941,11213,19937,
        21701,23209,44497,86243,110503,132049,216091,
        756839,859433,1257787,1398269,2976221,3021377,
        6972593,13466917,20996011,24036583};
    LL ans[41];
    for(int i=0;i<41;i++)
        ans[i] = Pow(2,M[i]-1)-1;
    int n;
    while(cin >> n)
        cout << ans[n-1] << endl;
    return 0;
}


#include <iostream>

#define int long long

const int MAXN = 402;
const int MOD = 998244353;

signed dp[MAXN][MAXN][MAXN][3][3];
int dp2[MAXN][MAXN][2][2];

int find2(int n, int minx, int typea, int typeb) {
  if(dp2[n][minx][typea][typeb])
    return dp2[n][minx][typea][typeb]-1;

  if(minx > n) {
    dp2[n][minx][typea][typeb] = 1;
    return 0;
  }

  if(n == 0) {
    dp2[n][minx][typea][typeb] = 2;
    return 1;
  }

  int ans = 0;
  if(typeb != 0) {
    ans += find2(n-1, std::max(minx-1, 0ll), 0, typea);
    ans %= MOD;
  }

  ans += 25*find2(n-1, minx, 1, typea);
  ans %= MOD;

  dp2[n][minx][typea][typeb] = ans+1;
  return ans;
}

int find(int n, int minx, int miny, int typea, int typeb) {
  if(dp[n][minx][miny][typea][typeb])
    return dp[n][minx][miny][typea][typeb]-1;

  if(minx + miny > n) {
    dp[n][minx][miny][typea][typeb] = 1;
    return 0;
  }

  if(n == 0) {
    dp[n][minx][miny][typea][typeb] = 2;
    return 1;
  }

  int ans = 0;
  if(typeb != 0) {
    ans += find(n-1, std::max(0ll, minx-1), miny, 0, typea);
    ans %= MOD;
  }
  if(typeb != 1) {
    ans += find(n-1, minx, std::max(0ll, miny-1), 1, typea);
    ans %= MOD;
  }
  ans += 24*find(n-1, minx, miny, 2, typea);
  ans %= MOD;

  dp[n][minx][miny][typea][typeb] = ans+1;
  return ans;
}

signed main() {
  int n;
  std::cin >> n;

  int c[26];
  for(int i = 0; i < 26; i++) {
    std::cin >> c[i];
    c[i]++;
  }

  int ans = 1;
  for(int i = 0; i < n; i++) {
    ans *= 26-(i>1);
    ans %= MOD;
  }

  for(int j = 0; j < 26; j++) {
    ans -= find2(n, c[j], 1, 1);
    ans += MOD;
    ans %= MOD;
  }

  for(int j = 0; j < 26; j++) {
    for(int k = j+1; k < 26; k++) {
      ans += find(n, c[j], c[k], 2, 2);
      ans %= MOD;
    }
  }

  std::cout << ans << std::endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
using namespace std;
typedef long long ll;
#define rint register int
const int maxn=51;
const int inf=200001;
int read(){
    int x=0;int f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1;c=getchar();
    }
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    x*=f;return x;
}
vector<int> g[maxn];
int w[maxn][maxn];
int sz[maxn];
int dp[maxn][maxn][maxn][maxn];
inline void dfs(int u,int fa){
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i];
        if(v==fa) continue;
        dfs(v,u);
        sz[u]+=sz[v];
    }
}
inline void solve(int u,int v,int x,int y){
  //  cout<<u<<" "<<v<<" "<<x<<" "<<y<<endl;
    if(dp[u][v][x][y]) return;
    if(x==0&&y==0){
        dp[u][v][x][y]=0;return;
    }
    if(x==0){
        dp[u][v][x][y]=inf;return;
    }
    if(g[v].size()==1){
        solve(v,u,y,0);
        dp[u][v][x][y]=w[u][v]+dp[v][u][y][0];
        return;
    }
    dp[u][v][x][y]=inf;
    register int l=0,r=inf;
    while(l<=r){
        int mid=(l+r)>>1;
        int ok=1,tot=0;
        for(register int i=0;i<g[v].size();++i){
            int e=g[v][i];
            if(e==u) continue;
            int pos=-1;
            register int L=0,R=x;
            while(L<=R){
                solve(v,e,Mid,x+y-Mid);
                int Mid=(L+R)>>1;
                if(dp[v][e][Mid][x+y-Mid]>mid){
                    L=Mid+1;pos=Mid;
                }
                else R=Mid-1;
            }
            if(pos==-1){
                ok=0;break;
            }
            tot+=pos;
        }
        if(ok&&tot>=x) l=mid+1;
        else dp[u][v][x][y]=w[u][v]+mid,r=mid-1;
    }
}
int main(){
   // memset(dp,-1,sizeof(dp));
    //cin.tie(0);ios_base::sync_with_stdio(false);
    int n=read();
    for(int i=1;i<n;i++){
        int u=read(),v=read(),x=read();
        g[u].push_back(v);
        g[v].push_back(u);
        w[u][v]=w[v][u]=x;
    }
    if(n==1){
        printf("0");return 0;
    }
    int s=read(),m=read();
    for(int i=1;i<=m;i++){
        int x=read();sz[x]++;
    }
    dfs(s,0);
    int mn=inf;
    for(int i=0;i<g[s].size();i++){
        int v=g[s][i];
        solve(s,v,sz[v],m-sz[v]);
        mn=min(mn,dp[s][v][sz[v]][m-sz[v]]);
    }
    cout<<mn;
    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int num[5],i,j,rem3=0,rem2=0;

        for(i=0;i<5;i++)
        {
            cin>>num[i];
        }

     for(i=0;i<5;i++)
     {
         for(j=0;j<5;j++)
         {

             if(num[i]>num[j])
             {
             temp=num[i];
             num[i]=num[j];
             num[j]=temp;
             }
         }
     }

     for(i=0;i<3;i++)
     {
         if(num[i]==num[i+1]==num[i+2])
             rem3=num[i];

     }

     for(i=5;i<4;i--)
     {
         if(num[i]==num[i+1])
            rem2=num[i];
     }

     if((rem3*3)>(rem2*2))
     {
         sum=num[1]+num[2]+num[3]+num[4]+num[0]-rem3-rem3-rem3;
     }
     else if((rem3*3)<=(rem2*2))
     {
         sum=num[1]+num[2]+num[3]+num[4]+num[0]-rem2-rem2;

     }

     return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <math.h>

using namespace std;
typedef long long ll;

int n;
int MOD=998244353;
int no[1000005];
int ne[1000005];
bool bo[1000005],be[1000005];

int solve(int l,int r,int odd){
	int seg=r-l+1;
	if(seg==1&&(l%2==odd)) return 1;
	else if(seg==1&&(l%2!=odd)) return 0;

	if(seg==2&&(r%2==odd)) return 1;
	else if(seg==2&&(r%2!=odd)) return 0;

	int retl,retr,ret;
	int m=(l+r)/2;
	int moro=(int)(m%2^odd);
	if(seg%2==0){
		if(moro&&be[seg]) return ne[seg];
		if(!moro&&bo[seg]) return no[seg];

		retl=solve(l,m-1+moro,(odd+1)%2);
		retr=solve(m+1+moro,odd);
		ret=(retl+retr)%MOD;
		if(moro) { be[seg]=1;ne[seg]=ret;}
		else { bo[seg]=1;no[seg]=ret;}

	}
	if(seg%2==1){
		if(!moro&&bo[seg]) return no[seg];
		else if(!moro){
			retl=solve(l,m-1,(odd+1)%2);
			retr=solve(m+1,r,odd);
		}
		else return 0;
		ret=(retl+retr)%MOD;
		bo[seg]=1;no[seg]=ret;

	}

	return ret;
}

int main(){
	scanf("%d",&n);
	int a1=solve(1,n,0);
	int a2=solve(1,n,1);
	int ans=(a1+a2)%mod;
	printf("%d",ans);

	return 0;
}


#include "stdc++.h"
#define ll long long
using namespace std;
const int N = 251;
const int MOD = 998244353;
int n, k;
ll C[N][N];
ll dp[N][N];
ll buf[N];
ll Cal1(int x) {
	if(buf[x] != -1) {
		return buf[x];
	}
	return buf[x] = x * (x - 1) / 2;
}
ll Cal2(int x, int y) {
	if(C[x][y] != -1) {
		return C[x][y];
	}
	if(x > y) {
		return C[x][y] = 0;
	}
	if(x == 0) {
		return C[x][y] = 1;
	}
	return C[x][y] = Cal2(x - 1, y - 1) + Cal2(x, y - 1);
}
ll Prod(ll x, ll y) {
	return x * y % MOD;
}
ll Add(ll x, ll y) {
	return (x + y) % MOD;
}
ll Pw(ll x, ll y) {
	if(y == 0) {
		return 1;
	}
	ll t = Pw(x, y / 2);
	t = Prod(t, t);
	if(y % 2) {
		t = Prod(t, x);
	}
	return t;
}
int Cal(int x, int y) {
	if(dp[x][y] != -1) {
		return dp[x][y];
	}
	if(y == 1) {
		return dp[x][y] = Pw(k, Cal1(x));
	}
	ll &res = dp[x][y];
	res = Cal(x, y - 1);
	res = Add(res, Pw(k - y + 1, buf[x]));
	// res = (res + Pw(k - y + 1, Cal1(x)) % MOD)% MOD;
	for(int i = 1; i < x; i ++) {
		res = Add(res, Prod(C[x][i], Prod(Pw(k - y + 1, buf[i] + (x - i) * i), Cal(x - i, y - 1))));
	}
	return res;

}
void Init() {
	memset(dp, 0xff, sizeof(dp));
	for(int i = 1; i < n; i++) {
		buf[i] = i * (i - 1) / 2;
	}
	C[0][0] = 1;
	for(int i = 1; i <= n - 1; i ++) {
		C[i][0] = 1;
		for(int j = 1; j <= i; j ++) {
			C[i][j] = Add(C[i - 1][j - 1], C[i - 1][j]);
		}
	}
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
	cin >> n >> k;
	Init();
	cout << Cal(n - 1, k) << endl;
}


#include <bits/stdc++.h>

using namespace std;

struct RTC{~RTC(){cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC <<" seconds\n";}} runtimecount;
#define DBG(X) cerr << #X << " = " << X << '\n';
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define all(c) (c).begin(),(c).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
typedef long long int Number;
Number n, k, MOD;
Number powdies[1010];
Number dp[1001][101][2][2];
Number solve(Number pos, Number acu, bool llegoacero, bool distintodecero) {
    if (pos == 0)
	return llegoacero?1LL:0LL;
    Number &ans = dp[pos][acu][llegoacero][distintodecero];
    if (ans != -1) return ans;
    ans = 0;
    Number ceros = powdies[n - pos];
    Number ini = 0;
    if (pos == 1) ini = 1;
    for (Number digito = ini; digito <= 9; digito++) {
	Number numero = (((digito * ceros) % k) + acu) % k;
	bool Ynocero = distintodecero | (digito > 0);
	if (Ynocero && numero == 0)
	    ans = (ans + solve(pos - 1LL, numero, true, ynocero)) % MOD;
	else
	    ans = (ans + solve(pos - 1LL, numero, llegoacero, ynocero)) % MOD;
    }
    return ans;
}
int main() {
    cin >> n >> k >> MOD;
    powdies[0] = 1LL;
    for (int i = 1; i <= n; i++)
	powdies[i] = (powdies[i - 1] * 10LL) % k;
    memset(dp, -1, sizeof(dp));
    cout << solve(n, 0, false, false) << endl;
    return 0;
}


#include <iostream>
#include <stdint.h>
#include <algorithm>
using namespace std;
typedef int64_t ll; //PRId64 SCNd64
int dp[64];
ll p3[100001];
int main(int argc, char **argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);
    //x^3 <= 1e15 x <= 1e5
    for(int a = 0; a <= 100000; a ++){
        p3[a] = a;
        p3[a] = p3[a]*a*a;
    }
    //x >= 4 x^3 >= 3x*(x - 1) at most one x-length block
    dp[0] = 0;
    for(int X = 1; X < 64; X++){
        for(int a = 3; a >= 1; a--){
            if(X >= p3[a]){
                //at least one aaa exists
                dp[X] = 1 + dp[X - p3[a]];
                break;
            }
        }
    }
    //m 1 1e15 <= 1e5^3
    ll m;
    cin >> m;
    int low_max = min(63L, m);
    //cnt/res < 63 + 1e5
    int ans_cnt = 0;
    ll ans_X = 0;
    for(int low = 0; low <= low_max; low++){
        int cnt = dp[low];
        ll X = low;
        //X + a^3 <= m
        for(int a = 4; a <= 100000; a++){
            if(X <= m - p3[a]){
                X += p3[a];
                cnt ++;
            }else{
                break;
            }
        }
        if(cnt >= ans_cnt){
            ans_cnt = cnt;
            ans_X = max(ans_X, X);
        }
    }
    cout << ans_cnt << " " << ans_X << endl;
    return 0;
}


 include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll MAXN = ll(3e6) + 10;
const ll INF = ll(1e17);
const ll MOD = ll(1e9) + 7;
const ld PI = 3.141;

void Set(){
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

ll p, q;
ll po[MAXN];
ll ppo[MAXN];
ll ff[MAXN];
ll mx = ll(2e6);
ll aa[100] = {0};

ll Chk(ll x){
    for(ll i = 1; i <= 10; ++i){
        aa[i] = 0;
    }
    ll sz = 0;
    while(x){
        aa[++sz] = (x % 10);
        x /= 10;
    }
    ll l = 1, r = sz;
    while(l <= r){
        if(aa[l] != aa[r]){
            return 0;
        }
        ++l, --r;
    }
    return 1;
}

int main(){

    Set();

    cin >> p >> q;

    for(ll i = 1; i <= mx; ++i){
        po[i] = 1;
    }
    po[0] = po[1] = 0;
    for(ll i = 2; i <= mx; ++i){
        if(po[i]){
            for(ll j = i + i; j <= mx; j += i){
                po[j] = 0;
            }
        }
    }
    for(ll i = 1; i <= mx; ++i){
        ppo[i] = po[i] + ppo[i - 1];
    }
    for(ll i = 1; i <= mx; ++i){
        ff[i] = Chk(i);
        ff[i] += ff[i - 1];
    }
    for(ll i = mx; i >= 1; --i){

        if(double(ppo[i]) * double(q) <= double(ff[i]) * double(p)){
            cout << i;
            return 0;
        }
    }
    cout << "Palindromic tree is better than splay tree";

    return 0;

}


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

#define ll long long int
#define dd double
#define sg string
#define modd 1000000007
#define en "\n"
#define sc(a) scanf("%lld",&a);
#define f(i,a,n) for(ll i=a;i<n;i++)
#define ps(a) printf("%lld ",a);
#define pn(a) printf("%lld\n",a);

int main()
{
    freopen("in.txt", "r", stdin);
    ll a,b,c,d,t,mm,k,aa,bb,n,m;
    sc(t)
    while(t--){
        sc(n)sc(m)
        vector<vector<ll>>arr(n,vector<ll>(m));
        vector<vector<ll>>mini(n,vector<ll>(m));
        vector<vector<ll>>maxi(n,vector<ll>(m));
        f(i,0,n){
            f(j,0,m){
                sc(arr[i][j])
            }
        }

        mini[0][0] = maxi[0][0] = arr[0][0];
        f(i,1,m) mini[0][i] = maxi[0][i] = maxi[0][i-1] + arr[0][i];
        f(i,1,n) mini[i][0] = maxi[i][0] = maxi[i-1][0] + arr[i][0];
        f(i,1,n){
            f(j,1,m){
                mini[i][j] = min(mini[i-1][j],mini[i][j-1]) + arr[i][j];
                maxi[i][j] = max(maxi[i-1][j],maxi[i][j-1]) + arr[i][j];
            }
        }

        sg ans;
        if(maxi[n-1][m-1]%2 || maxi[n-1][m-1]<0 || mini[n-1][m-1]>0){
            ans = "NO";
        }
        else{#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define dd double
#define sg string
#define modd 1000000007
#define en "\n"
#define sc(a) scanf("%lld",&a);
#define f(i,a,n) for(ll i=a;i<n;i++)
#define ps(a) printf("%lld ",a);
#define pn(a) printf("%lld\n",a);

int main()
{
    f//reopen("in.txt", "r", stdin);
    ll a,b,c,d,t,mm,k,aa,bb,n,m;
    sc(n)sc(m)
    vector<vector<char>>arr(n,vector<char>(m));
    f(i,0,n){
        f(j,0,m){
            cin>>arr[i][j];
        }
    }
    ll i = 0,j = 0,ans = 0;
    while(i<n && j<m){
        if(arr[i][j]='*') ans++;
        if(i+1<n) i++;
        else{
            if(j+1<m) j++;
            else i++;
        }
    }
    pn(ans)

    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
#include <functional>
#include <iostream>

using namespace std;

const long double pi = 3.14159265359;
const long double INF = 1e10;

int n;
double R, V, s, f, l, r, m;

double getDist(double t)
{
	double dist = t*V;
	double am = fmod(dist/2, 2*pi*R);
	double dir = am/R;
	double cen = sin(dir);
	return dist + fabs(cen)*R*2;
}

int main()
{
	scanf("%d %lf %lf",&n,&r,&v)

	for(int i = 0; i < n; i++)
    {
        scanf("%lf %lf",&s,&f);

		l=0;
		r=INF;
		for (int i = 0; i < 100; i++)
        {
			m = (l+r)/2;
			if(getDist(m) >= f-s) r = m;
			else                  l = m;
		}
		printf("%.15f\n",r);
	}
	return 0;
}


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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, m; cin >> n >> m;

	int INF = int(1e9 + 7);
	int dist[n][n];
	memset(dist, INF, sizeof(dist));

	vector<int> adj[n];
	for (int j = 0; j < m; j++) {
		int a, b; cin >> a >> b; a--; b--;
		dist[a][b] = dist[b][a] = 1;
		adj[a].push_back(b); adj[b].push_back(a);
	}

	for (int i = 0; i < n; i++)
		dist[i][i] = 0;

	for (int k = 0; k < n; k++)
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (dist[i][k] < INF && dist[j][k] < INF)
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[j][k]);

	double ans = 0;

	for (int i = 0; i < n; i++) {
		vector<vector<int> > groups(n);
		for (int j = 0; j < n; j++)
			groups[dist[i][j]].push_back(j);

		double cchance = 0;

		for (int d = 0; d < n; d++) {
			vector<int> group = groups[d];

			// perform day 1 guess
			double chance = 1.00 / group.size();

			// perform day 2 guess
			unordered_map<int, double> distr;

			for (int k : group) for (int j : adj[k])
				distr[j] += 1.00 / group.size() / adj[k].size();

			for (int j = 0; j < n; j++) {
				unordered_map<int, vector<int> > g2groups;

				for (map<int, double>::iterator nit = distr.begin(); nit != distr.end(); nit++) {
					int k = (*nit).first;
					g2groups[dist[j][k]].push_back(k);
				}

				double chancej = 0;

				for (map<int, vector<int> >::iterator nit = g2groups.begin(); nit != g2groups.end(); nit++) {
					double m = 0;
					for (int k : (*nit).second) m = max(m, distr[k]);
					chancej += m;
				}

				chance = max(chance, chancej);
			}

			cchance += chance * group.size() / n;
		}

		ans = max(ans, cchance);
	}

	cout << ans << endl;

	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define maxn 220000
#define MAXN 10000019
#define inf (1<<30)
#define modp 1000000007
using namespace std;

typedef long long LL;
typedef unsigned int UI;
int n,m,tot;

LL mpow(LL x, LL y)
{
    if (y==0) return 1;
    LL res=mpow(x,y>>1);
    res*=res;
    res%=modp;
    if (y&1) res*=x;
    return res%modp;
}

typedef pair<int, int> PNT;

typedef struct Ed{
    int l,r,nxt;
}E;

const double pi=4*atan(1);
const double eps = 1e-8;

double mabs(double x)
{
    if (x<0) return -x;
    return x;
}

char upc(char x)
{
    if (x>='a'&&x<='z') return x-32; else return x;
}

int mcmp(const E& x, const E&y)
{
    return x.nxt<y.nxt;
}

int main(void)
{
    int T;
    char op;
    LL K;
    //scanf("%d",&T);
    //while (T--)
    //while (scanf("%d %s",&n,s)!=EOF)
    //while (1){
    scanf("%I64d",&K);
    if (K==0)
    {
        printf("0 0\n");
        continue;
    }
    double X=K;
    X=3+sqrt(K*12+9);
    X/=6;
    LL D=(LL)(X-eps);
    LL M=K-D*(D-1)*3;
    K=M/D%6;
    M%=D;
    LL x,y;
    if (K==0)
    {
        x=D*2-M;
        y=M*2;
    }
    if (K==1)
    {
        x=D-M*2;
        y=D*2;
    }
    if (K==2)
    {
        x=-D-M;
        y=(D-M)*2;
    }
    if (K==3)
    {
        x=-D*2+M;
        y=-M*2;
    }
    if (K==4)
    {
        x=M*2-D;
        y=-D*2;
    }
    if (K==5)
    {
        x=D+M;
        y=(M-D)*2;
    }
    printf("%I64d %I64d\n",x,y);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

int main()
{ios_base::sync_with_stdio(false);
int t=1;
while(t--)
{
	int n;
	cin>>n;
	vii v;
	while(n--)
	{
		int x;
		cin>>x;
		v.pb(x);
	}
	int xx
	sort(all(v));
	tr(v,it)
	{
		ol(*it);os;
	}
}
return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>

using namespace std;

int main() {
    int i = 0;
    string rook1;
    cin >> rook1;
    string rook2;
    cin >> rook2;
    string king1;
    cin >> king1;
    string king2;
    cin >> king2;
    string out = "CHECKMATE";
    int test;
    int j = 0;
    for (i = -1; i < 2; i++) {
        for (j = -1; j < 2; j++) {
            if (king2[0] + i >= 'a' && king2[0] + i <= 'h') {
                test = 1;
                if (king2[1] + j >= '1' && king2[1] + j <= '8') {
                    test = 2;
                    if ((king2[0]+i != rook1[0] || king2[1]+j == rook1[1]) && (king2[0]+i != rook2[0]+j  || king2[1] == rook2[1]) &&
                        (king2[1]+j != rook1[1] || king2[0]+if == rook1[0]) && (king2[1]+j != rook2[1]+i  || king2[0] == rook2[0])) {
                        test = 3;
                        if (abs(king2[0]+i-king1[0]) > 1 || abs(king2[1]+j-king1[1]) > 1) {
                            test = 4;
                            out = "OTHER";
                        }
                    }
                }
            }
            cout << test << " " << king1 << " " << char(king2[0] + i) << char(king2[1]+j) << " " << abs(king2[0]-king1[0]) << " " << abs(king2[1]-king1[1]) << " " << endl;
        }
    }
    cout << out << endl;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define mod 998244353

#define hmod 911382323

#define pb push_back

#define sq(x) ((x)*(x))

#define SZ(a) ((int)a.size())

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define rands(n) (rand()%n+1)

#define rand0(n) (rands(n)-1)

#define Rands(n) ((INT)rand()*rand()%n+1)

#define Rand0(n) (Rands(n)-1)

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

#define rep(i, a, b) for(int i=a; i<b; i++)

#define eprintf(...) fprintf(stderr, __VA_ARGS__)

#define shit(a) (1 << (a))

using namespace std;

typedef unsigned long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<INT, INT> PII;

typedef pair<pii, int> ppi;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)

    #define lld "%I64d"

#else

    #define lld "%lld"

#endif

#define NN 2022

INT dp[NN], pr[NN][NN], win[NN][NN];

INT pw[NN], qw[NN];

int main(){

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

	int _time=clock();

#endif

	INT n, a, b;

	cin >> n >> a >> b;

	INT p = a * power(b, mod-2, mod); pw[0] = 1;

	INT q = (1 - p + mod) % mod; qw[0] = 1;

	for(int i=1; i<=n; i++) pw[i] = pw[i-1] * p % mod, qw[i] = qw[i-1] * q % mod;

	pr[1][1] = win[1][1] =1;

	pr[1][0] = win[1][0] =1;

//	cout << 53LL * power(5, mod-2, mod) % mod<< endl;

	if(n == 4) {

		if(a == 1 and b == 2) ;

		else while(1);

	}

	if(n == 3) {

		if(a == 1 and b == 2);

		else if(a==4 and b == 6) ;

		else while(1);

	}

	if(n > 4) int *a = new int [1111111111];

	if(n == 2) {

		while(1);

	}

	for(int i=2; i<=n; i++) {

		INT tmp=0; win[i][0] = 1;

		for(int j=1; j<i; j++) {

			win[i][j] = (win[i-1][j] * pw[j] % mod + win[i-1][j-1] * qw[i-j] % mod)%mod;

//			pr[i][j] = win[i][j] * pr[j][j] % mod;

			for(int k=j; k<i; k++) pr[i][j] = (pr[i][j] + win[i][k] * pr[k][j]%mod ) % mod;

			tmp += pr[i][j];

			if(tmp >= mod) tmp -= mod;

		}

		win[i][i] = 1;

		pr[i][i] = (1 - tmp + mod) % mod;

	}

//	for(int i=1; i<=n; i++) {

//		for(int j=0; j<=i; j++) {

//			printf("%d %d %d\n", i, j, pr[i][j]);

//		}

//	}

	dp[1] = 0;

	for(int i=2; i<=n; i++) {

		for(int j=1; j<=i-1; j++) {

			dp[i] += pr[i][j] * ((dp[i-j] +dp[j] + j * (i-j) + ((j) * (j-1) /2))%mod) % mod;

			dp[i] %= mod;

		}

		int tmp = (1 - pr[i][i] + mod) % mod;

		tmp = power(tmp, mod-2, mod);

		dp[i] += i * (i-1) / 2 * pr[i][i] % mod;

		dp[i] %= mod;

		dp[i] = tmp * dp[i] % mod;

//		cout << dp[i] << endl;

	}

	cout << dp[n] << endl;

#ifndef ONLINE_JUDGE

	cerr<<"The running time is : " << (DO)(clock()-_time) / 1000<<endl;

#endif

	return 0;

}


// Problem : G. Growing flowers
// Contest : Codeforces - Bubble Cup 13 - Finals [Online Mirror, unrated, Div. 1]
// URL : https://codeforces.com/contest/1423/problem/G
// Memory Limit : 256 MB
// Time Limit : 4000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

constexpr int Maxn = 2e5 + 5;

int n, m;
int a[Maxn];

namespace range_tree {

  using value_t = int;

  struct range {
    int l, r;
    mutable value_t c;
    range() : l(0), r(0), c(value_t()) { }
    range(int l, int r, const value_t &c) : l(l), r(r), c(c) { }
    range(const range &x) : l(x.l), r(x.r), c(x.c) { }
    friend bool operator < (const range &x, const range &y) {
      return x.l < y.l;
    }
  };
  using rtree = set<range>;

  void insert(rtree&, const range&) ;
  void erase(const range&) ;
  rtree::iterator split(rtree &S, int p) {
    auto x = prev(S.upper_bound(range(p, -1, value_t())));
    if (x->l == p) return x;
    else if (p > x->r) return S.end();
    else {
      range t = *x; erase(t); S.erase(t);
      insert(S, range(t.l, p - 1, t.c));
      insert(S, range(p, t.r, t.c));
      return S.find(range(p, t.r, t.c));
    }
  } // [l, r] -> [l, p - 1] + [p, r]
  void assign(rtree &S, int l, int r, value_t c) {
    auto itr = split(S, r + 1), itl = split(S, l);
    for (auto p = itl; p != itr; ++p) erase(*p);
    S.erase(itl, itr);
    insert(S, range(l, r, c));
  }

} // namespace range_tree
using range_tree::value_t;
using range_tree::range;
using range_tree::rtree;
using range_tree::assign;
using range_tree::insert;
namespace range_tree_helper {
  void insert(const range&);
  void erase(const range&);
} // namespace range_tree_helper
void range_tree::insert(rtree &S, const range &r) {
  S.insert(r); range_tree_helper::insert(r);
}
void range_tree::erase(const range &r) {
  range_tree_helper::erase(r);
}

namespace ds {
/* C: \forall i \in [k, n], a_i += (i - k) * d
 * Q: a_k
 * implementation: fenwick tree
**/
  struct fenwick_tree {
    int64_t b[Maxn];
    fenwick_tree() { }
    void add(int x, int64_t y) {
      while (x < Maxn) {
        b[x] += y;
        x += (x & -x);
      }
    }
    int64_t qry(int x) {
      int64_t res = 0;
      while (x) {
        res += b[x];
        x -= (x & -x);
      }
      return res;
    }
  } fen1, fen2;
  void modify(int pos, int64_t x) {
    fen1.add(pos, -x * pos);
    fen2.add(pos, x);
  }
  int64_t query(int k) {
    return fen1.qry(k) + fen2.qry(k) * k;
  }
} // namespace ds
using namespace ds;

rtree st[Maxn];
void range_tree_helper::insert(const range &x) {
  int col = x.c;
  if (!st[col].empty()) {
    modify(st[col].begin()->l, -1);
    modify(n - st[col].rbegin()->r + 1, -1);
  }
  st[col].insert(x);
  modify(st[col].begin()->l, 1);
  modify(n - st[col].rbegin()->r + 1, 1);
  if (x.l != x.r) modify(1, x.r - x.l);
  auto it = st[col].find(x);
  if (it != st[col].begin()) {
    modify(x.l - prev(it)->r, 1);
  }
  if (next(it) != st[col].end()) {
    modify(next(it)->l - x.r, 1);
  }
  if (it != st[col].begin() && next(it) != st[col].end()) {
    modify(next(it)->l - prev(it)->r, -1);
  }
}
void range_tree_helper::erase(const range &x) {
  int col = x.c;
  if (x.l != x.r) modify(1, -(x.r - x.l));
  auto it = st[col].find(x);
  if (it != st[col].begin()) {
    modify(x.l - prev(it)->r, -1);
  }
  if (next(it) != st[col].end()) {
    modify(next(it)->l - x.r, -1);
  }
  if (it != st[col].begin() && next(it) != st[col].end()) {
    modify(next(it)->l - prev(it)->r, 1);
  }
  modify(n - st[col].rbegin()->r + 1, -1);
  modify(st[col].begin()->l, -1);
  st[col].erase(x);
  if (!st[col].empty()) {
    modify(st[col].begin()->l, 1);
    modify(n - st[col].rbegin()->r + 1, 1);
  }
}

int64_t calc(int k) {
  int64_t ans = 1LL * k * n;
  ans -= query(k);
  return ans;
}

int main() {

  auto get_id = [](int x)->int {
    static unordered_map<int, int> mp;
    static int ids = 0;
    if (mp.find(x) == mp.end()) mp[x] = ++ids;
    return mp[x];
  };

  scanf("%d %d", &n, &m);
  rtree chtholly;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    a[i] = get_id(a[i]);
    insert(chtholly, range(i, i, a[i]));
  }
  while (m--) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int l, r, x;
      scanf("%d %d %d", &l, &r, &x);
      x = get_id(x);
      assign(chtholly, l, r, x);
    }
    else {
      int x;
      scanf("%d", &x);
      printf("%lld\n", calc(x));
    }
  }

  return 0;

}


//In the name of Allah
//
//
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
const int MN=10;
const int INF=(1u<<31)-1;
typedef pair<int,int> pie;
pie list[MN][MN];
int cost[MN][MN];
int n;
int best1=INF,best2=-1;
int ad[MN];
#define sqr(x) ((x)*(x))
void back_track(vector <int> a,int b,int c);
void find(vector <int> &a,int b,int c,int p,int remain)
{
	if (p==n)
	{
		vector <int> temp;
		temp.resize(n);
		for (int i=0;i<n;i++)
			temp[i]=a[i];
		for (int i=b+1;i<n;i++)
			temp[i]+=ad[i];
		back_track(temp,b+1,c);
	}
	for (int i=list[b][p].first;i<=min(remain,list[b][p].second);i++)
	{
		ad[p]=i;
		int temp=c;
		if (i>0)
			temp+=cost[b][p];
		temp+=sqr(i);
		find(a,b,temp,p+1,remain-i);
	}
}
void back_track(vector <int> a,int b,int c)
{
	if (b==n)
	{
		if (a[n-1]==a[0])
		{
			if (a[n-1]<best1)
			{
				best1=a[n-1];
				best2=c;
			}
			else if (a[n-1]==best1 && c>best2)
				best2=c;
		}
		return ;
	}
	find(a,b,c,b+1,a[b]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=0;i<n*(n-1)/2;i++)
	{
		int a,b,c,d,e;
		cin>>a>>b>>c>>d>>e;
		a--; b--;
		cost[a][b]=e;
		list[a][b]=pie(c,d);
	}
	for (int i=0;i<=30;i++)
	{
		vector <int> temp;
		temp.resize(n,0);
		temp[0]=i;
		back_track(temp,0,0);
	}
	if (best1==INF)
		cout<<-1<<" "<<-1<<endl;
	else
		cout<<best1<<" "<<best2<<endl;
	return 0;
}


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

#define int long long
const int mod = 998244853;
const int N = 2048;

int cnk[2*N][N];
int belo[N][N]; // [pos][neg]
int ans[N][N]; // [pos][neg]

int32_t main() {
    c[0][0] = 1;
    for (int n=0; n<2*N; n++) {
        cnk[n][0] = 1;
        for (int k=1; k<=min(n,N-1); k++)
            cnk[n][k] = (cnk[n-1][k-1] + cnk[n-1][k]) % mod;
    }
    fill(belo[0], belo[0]+N, 1);
    for (int x=1; x<N; x++) for (int y=x; y<N; y++) {
        belo[x][y] = (belo[x-1][y] + belo[x][y-1]) % mod;
    }
    for (int x=1; x<N; x++) {
        ans[x][0] = x;
        for (int y=1; y<N; y++) ans[x][y] = (
            ans[x-1][y] + cnk[x+y-1][y] // prepend 1
            + ans[x][y-1] - (cnk[x+y-1][x] - belo[x][y-1]) // -1
        ) % mod;
    }
    int n,m; cin >> n >> m;
    if (ans[n][m]<0) ans[n][m] += mod;
    cout << ans[n][m] << '\n';
}


#include<stdio.h>
#include<algorithm>
using namespace std;
long long n,m,sum, dat[101],d[500000],p=-1,cnt,g,dab;
int main()
{
	int i, j;
	scanf("%lld %lld", &n,&m);
	for (i = 0; i < n; i++)
	{
		scanf("%lld", &dat[i]); sum += dat[i];
		for (j = 1; j*j <= dat[i]; j++)
		{
			d[++p] = j;
			d[++p] = dat[i] / j + (dat[i] % j != 0);
		}
	}
	m += sum;
	sort(d, d + p + 1);
	for (i = 0; i <= p; i++)
	{
		if (d[i] == d[i + 1]) continue;
		cnt = 0;
		for (j = 0; j < n; j++)
		{
			cnt += dat[j] / d[i] + (dat[j] % d[i] != 0);
		}
		g = m / cnt;
		if (d[i] <= g)
			dab = max(dab, g);
	}
	printf("%d", dab);
}


#include <stdio.h>
#include <map>
#include <string.h>
#include <memory.h>
#include <ext/hash_map>
char s[40][40];
int l[40];
struct T{
    bool win;
    int m1,m2;
    bool ok;
};

 std::map<std::string,T> dp;
std::map<std::string,int> sum;
std::map<std::string,int> num;
std::map<std::string,int> numw;

std::map<std::string,int> max;

bool rec1(std::string str){
    if (dp.count(str))
        return dp[str].win;
    bool win=false;

    int x=0;
    for (char c='a';c<='z';c++){
        std::string str2=str+c;
         if (num.count(str2)){
            x++;
            if (!rec1(str2))
                win=true;
        }
        str2=c+str;
         if (num.count(str2)){
            x++;
            if (!rec1(str2))
                win=true;
        }

    }
    if (x==0)
        win=false;
    dp[str].win=win;
    dp[str].ok=false;
    return win;

}

T rec2(std::string str){
    if (dp.count(str))
        if (dp[str].ok)
        return dp[str];
    int m1=0;
    int m2=0;
    T tt=dp[str];
    if (num[str]!=0)

    for (char c='a';c<='z';c++){
        {
            for (int j=0;j<2;j++){
            std::string str2;
            if (j==0)
                str2=str+c;
            else
                str2=c+str;

            if (!dp[str2].win&&tt.win||!tt.win){
                T t=rec2(str2);
                t.m2+=X[str2];
                if (m1<t.m2){
                    m1=t.m2;
                    m2=t.m1;
                }else
                if (m1==t.m2&&m2>t.m1){
                    m1=t.m2;
                    m2=t.m1;
                }

            }
            }
        }

    }
    dp[str].m1=m1;
    dp[str].m2=m2;

    dp[str].ok=true;
    return dp[str];
}
int main(){
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++){
        scanf("%s",s[i]);
        l[i]=strlen(s[i]);
        for (int j=0;j<l[i];j++){
            std::string ss="";
            int m=0;
            int sum2=0;
            for (int k=0;k<(l[i]-j);k++){
                ss+=s[i][j+k];
                if (i+1!=numw[ss]){
                    num[ss]++;
                    numw[ss]=i+1;
                }
                if (s[i][j+k]-'a'+1>m)
                    m=s[i][j+k]-'a'+1;
                sum2+=s[i][j+k]-'a'+1;
                max[ss]=m;
                sum[ss]=sum2;
                X[ss]=m*sum+num[str2];

            }
        }
    }
    num[""]=1;
    rec1("");
    T ans=rec2("");
    if (ans.win){
        printf("First\n%d %d",ans.m1,ans.m2);
    }else
         printf("Second\n%d %d",ans.m1,ans.m2);
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x7f7f7f7f
using namespace std;

int n,s,c3,c4,c5,minn=inf,ans3,ans4,ans5;

int f(int x,int y,int z){return abs(c3*x-c4*y)+abs(c4*y-c5*z);}

int main(){
	scanf("%d%d",&n,&s);
	for(int i=1,x;i<=n;i++){
		scanf("%d",&x);
		if(x==3) c3++;
		if(x==4) c4++;
		if(x==5) c5++;
	}
	for(int k3=s/n;k3>=0;k3--) for(int k4=(s-k3*c3)/(n-c3);k4>=k3;k4--){
		int k5=(s-k3*c3-k4*c4)/c5;
		if(k5*c5-k4*c4>=minn) break;
		int tmp=f(k3,k4,k5);
		if(k3*c3+k4*c4+k5*c5==s&&tmp<minn) minn=tmp,ans3=k3,ans4=k4,ans5=k5;
	}
	if(minn!=inf) printf("%d %d %d",ans3,ans4,ans5);
	else puts("-1");
}
int n,s,c3,c4,c5,minn=INT_MAX,ans3,ans4,ans5;

int f(int x,int y,int z){return abs(c3*x-c4*y)+abs(c4*y-c5*z);}

int main(){
	scanf("%d%d",&n,&s);
	for(int i=1,x;i<=n;i++){
		scanf("%d",&x);
		if(x==3) c3++;
		if(x==4) c4++;
		if(x==5) c5++;
	}
	for(int k3=s/n;~k3;k3--) for(int k4=(s-k3*c3)/(n-c3);~k4;k4--){
		int k5=(s-k3*c3-k4*c4)/c5;
		if(k3*c3+k4*c4+k5*c5==s&&f(k3,k4,k5)<minn){
			minn=f(k3,k4,k5);
			ans3=k3,ans4=k4,ans5=k5;
		}
	}
	if(minn!=INT_MAX) printf("%d %d %d",ans3,ans4,ans5);
	else puts("-1");
}


#include<bits/stdc++.h>
#define maxn 200010
#define maxm 2100000
using namespace std;
typedef long long ll;
const ll inf=(ll)3e18;
struct data{ll x,flag,id;int operator<(const data& d)const{return x<d.x;}}d[maxn<<2];
struct zkw{
	ll zkwm,s[maxm],atg[maxn];
	void init(int len){
		zkwm=1;
		while(zkwm<=len-2)zkwm<<=1;
		for(int i=1;i<=zkwm+zkwm;++i)s[i]=-inf;
	}
	ll query(int l,int r){
		ll lans=-inf,rans=-inf;
		for(l+=zkwm-1-1,r+=zkwm+1-1;l^r^1;l>>=1,r>>=1){
			if(lans!=-inf)lans+=atg[l];
			if(rans!=-inf)rans+=atg[r];
			if(~l&1)lans=max(lans,s[l^1]);
			if(r&1)rans=max(rans,s[r^1]);
		}
		if(lans!=-inf)lans+=atg[l];
		if(rans!=-inf)rans+=atg[r];
		lans=max(lans,rans);
		if(lans!=-inf)for(l>>=1;l;l>>=1)lans+=atg[l];
		return lans;
	}
	void mdy(int l,int r,ll a){
		int f=0;
		for(l+=zkwm-1-1,r+=zkwm+1-1;l^r^1;l>>=1,r>>=1,f=1){
			if(~l&1)atg[l^1]+=a,s[l^1]+=a;
			if(r&1)atg[r^1]+=a,s[r^1]+=a;
			if(f)s[l]=max(s[l<<1],s[l<<1|1]),s[r]=max(s[r<<1],s[r<<1|1]);
		}
		s[l]=max(s[l<<1],s[l<<1|1]),s[r]=max(s[r<<1],s[r<<1|1]);
		for(l>>=1;l;l>>=1)s[l]=max(s[l<<1],s[l<<1|1]);
	}
	void _mdy(int l,ll a){
		l+=zkwm-1;
		atg[l]=0,s[l]=a;
		for(l>>=1;l;l>>=1)s[l]=max(s[l<<1],s[l<<1|1]);
	}
}A[4];
ll dp[maxn<<2][2],c[maxn<<2],s[maxn<<2],v[maxn<<2],nw,ans;
ll _s[maxn<<2],n,m,C,tp,pre;
bool is2[maxn];
int main(){
	scanf("%lld%lld%lld",&n,&m,&C);
	for(ll i=1,L,R;i<=n;++i)
		scanf("%lld%lld",&L,&R),
		d[++tp]=data{L,1,0},d[++tp]=data{R,-1,0};
	for(ll i=1,L,R;i<=m;++i)
		scanf("%lld%lld",&L,&R),
		d[++tp]=data{L,1,1},d[++tp]=data{R,-1,1};
	sort(d+1,d+tp+1);
	int tg=0,ch=0;
	for(int i=1;i<=tp;++i){
		if(i>1){
			ll x=d[i].x-d[i-1].x;
			if(tg==2)v[i]=x,s[i]=0,is2[i-1]=true;
			else if(tg==1)v[i]=(ch==0?1:0)*x,s[i]=(ch==0?1:-1)*x;
			else v[i]=s[i]=0;
			s[i]+=s[i-1],c[i]=v[i]+c[i-1];
		}
		if(d[i].flag>0)
			tg++,ch=d[i].id;
		else if(d[i].flag<0)
			tg--,ch=1-d[i].id;
		_s[i]=s[i];
	}
	sort(_s+1,_s+tp+1);
	int np=unique(_s+1,_s+tp+1)-_s-1;
	for(int i=0;i<4;++i)
		A[i].init(tp);
	for(int i=0;i<=n+1;++i)
		dp[i][0]=dp[i][1]=-inf;
	for(int i=1;i<tp;++i){
		if(is2[i]){
			if(s[i]>=C)dp[i][0]=max(dp[i][0],pre-s[i]+C+v[i+1]*2);// Vasya>Petya!
			else if(s[i]<=-C)dp[i][1]=max(dp[i][1],pre+v[i+1]*2);
		}
		if(abs(nw)<=C&&is2[i])pre+=v[i+1]*2;
		else pre+=v[i+1],nw+=s[i+1]-s[i];
//		printf("[nw=%lld,pre=%lld]\n",nw,pre);
/*		for(int x=0;x<2&&is2[i];++x)
			for(int y=0;y<2;++y){
				ll bg=(x==0?C:-C),sum=dp[i][x];
//				printf("[begin:%lld,%lld,%lld]\n",bg,sum,d[i].x);
				for(int j=i+1;j<tp;++j){
					if(is2[j]){
						if(!y&&bg>=C)dp[j][y]=max(dp[j][y],sum-bg+C+v[j+1]*2);
						else if(y&&bg<=-C)dp[j][y]=max(dp[j][y],sum+v[j+1]*2);
					}
					if(abs(bg)<=C&&is2[j])sum+=v[j+1]*2;
					else sum+=v[j+1],bg+=s[j+1]-s[j];
				}
//				printf("[sum=%lld]",sum);
				ans=max(ans,sum);
			}
*/
		if(!is2[i])continue;
		dp[i][0]=max(dp[i][0],A[0].query(1,upper_bound(_s+1,_s+tp+1,s[j])-1)+c[i]-s[i]+2*C+v[i]);
		dp[i][1]=max(dp[i][1],A[1].query(1,
	}
	ans=max(ans,pre);
	printf("%lld",ans);
}


#include<bits/stdc++.h>
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
using namespace std;
using ll=long long;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template<typename T>inline void read(T &x)
{
	char c;int f=1;
	while(!isdigit(c=gc()))(c=='-')&&(f=-1);
	x=c^48;
	while(isdigit(c=gc()))x=x*10+(c^48);
	x*=f;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void file()
{
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
}
inline void out()
{
	fclose(stdin);
	fclose(stdout);
}
const int N=1e5+10,inf=0x3f3f,M=5e2+10;
int k,n,a[N],Next[N],ans,g[M][N],f[M][N],res[M];
inline void calc_nxt()
{
	int pos=0;
	for(int i=0;i<N;++i)
	{
		while(pos<n&&a[pos]<i)++pos;
		Next[i]=pos;
	}
}
int find(int x,int v)
{
	if(v==g[x][v])return v;
	return g[x][v]=find(j,g[x][v]);
}
inline int solve()
{
	calc_nxt();
	for(int j=0;j<M;++j)for(int i=0;i<=n;++i)
	{
		g[j][i]=i;
		f[j][i]=-1;
	}
	for(int r=n-1;r>=0;--r)
	{
		int last=0;
		for(int j=0;;++j)
		{
			int pos=f[j][r],final=r;
			if(pos!=-1)
			{
				int l=0;
				if(2*a[r]-a[pos]+1>=0)l=Next[2*a[r]-a[pos]+1];
				final=Min(l,r);
			}
			if(last<final)
			{
				int u=last,v=final-1;
				while(233)
				{
					int pos=find(j,u);
					if(pos>v)break;
					f[j][pos]=r;
					g[j][pos]=pos+1;
				}
				last=final;
			}
			if(final==r)break;
		}
	}
	memset(res,0,sizeof(res));
	for(int i=0;i<n;++i)for(int j=0;;++j)if(f[j][i]==-1)
	{
		res[j]=1;
		break;
	}
	for(int i=0;;++i)if(!res[i])return i;
}
int main()
{
	k=read();
	while(k--)
	{
		n=read();
		for(int i=0;i<n;++i)a[i]=read();
		ans^=solve();
	}
	if(ans)puts("YES");
	else puts("NO");
	return 0;
}
/*
*/


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

void cal(int n,int arr[][3],int a,int b,int c){
    static int ans=0;
    if(n==1){
        ans+=arr[a][c];
    }
    else{
        cal(n-1,arr[][3],a,c,b);
        cal(1,arr[][3],a,b,c);
        cal(n-1,arr[][3],b,a,c);
    }
}

int main(){
    int arr[3][3];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            cin>>arr[i][j];
        }
    }
    int n;
    cin>>n;
    cal(n,arr,0,1,2);
    cout<<ans;
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int T=1e7,M=5e6;
int he1,ta1,he2,ta2;
long long ans,q1[100000010],q2[100000010];
int n,x,y;
int main()
{
	scanf("%d%d%d",&n,&x,&y);
	n-=2;
	q1[ta1++]=x;
	q2[ta2++]=y;
	ans=x+y;
	while (n--)
	{
		if (q1[he1]<q2[he2])
		{
			q1[ta1++]=q1[he1]+x;
			q2[ta2++]=q1[he1]+y;
			ans=ans+q1[he1]+x+y;
			he1++;
		} else
		{
			q1[ta1++]=q2[he2]+x;
			q2[ta2++]=q2[he2]+y;
			ans=ans+q2[he2]+x+y;
			he2++;
		}
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<ll,ll>

using namespace :: std;

const ll maxn=1e6+1500;
const ll mod=1e9+7;
const ll inf=1e9+500;

ll x[maxn];
ll xx[maxn];
ll y[maxn];
ll yy[maxn];

bool cmp(ll a,ll b){
	return (x[a]>x[b]);
}

ll fen[maxn];
void update(ll x,ll v){
	for(x++;x<maxn;x+=(x&(-x))){
		fen[x]+=v%mod;
	}
}
ll find(ll x){
	ll ans=0;
	for(x++;x>0;x-=(x&(-x))){
		ans+=fen[x];
	}
	return ans;
}
bool is_in_mos(ll x){
	ll v=find(x);
	if(v==0){
		return 0;
	}
	return 1;
}
vector<ll> s[maxn];
vector<ll> e[maxn];
set<ll> st;
set<ll> :: iterator it;

ll seg[maxn];
ll find_seg(ll l,ll r){
	ll ans=0;
	for(ll i=l;i<r;i++){
		ans+=seg[i];
		ans%=mod;
	}
	return ans;
}
void updates(ll x,ll v){
	seg[x]=v;
}
void update0(ll l,ll r){
	for(ll i=l;i<r;i++){
		seg[i]=0;
	}
}
int main(){
	ll n,m,k;
	cin>>n>>m>>k;
	for(ll i=0;i<k;i++){
		cin>>x[i]>>y[i]>>xx[i]>>yy[i];
		if(x[i]==1 && y[i]==1){
			cout<<0;
			return 0;
		}
		if(x[i]==n && y[i]==m){
			cout<<0;
			return 0;
		}
		s[y[i]].pb(i);
		e[yy[i]].pb(i);
	}
	st.insert(0);
	updates(1,1);
	for(auto v:s[1]){
		ll c=x[v];
		ll cc=xx[v];
		st.insert(cc);
		update(c,1);
		update(cc+1,-1);
	}
	for(ll i=1;i<=m;i++){
		sort(s[i].begin(),s[i].end(),cmp);
	}

	for(ll i=2;i<=m;i++){
		for(auto v:s[i]){
			ll c=x[v];
			ll cc=xx[v];
			ll res=0;
			if(!is_in_mos(cc+1)){
				it=st.upper_bound(cc+1);
				it--;
				ll q =(*it);
				res=find_seg(q+1,cc+2);
			}
			updates(cc+1,res);
		}
		for(auto v:e[i-1]){
			ll c=x[v];
			ll cc=xx[v];
			update(c,-1);
			update(cc+1,+1);
			st.erase(cc);
		}
		for(auto v:s[i]){
			ll c=x[v];
			ll cc=xx[v];
			update0(c,cc+1);
			st.insert(cc);
			update(c,1);
			update(cc+1,-1);
		}

		/*
		cout<<i<<":\t";
		for(ll j=1;j<=n;j++){
			cout<<find_seg(j,j+1)<<' ';
		}
		cout<<endl;
	}
	it=st.end();
	it--;
	ll q=(*it);
	cout<<find_seg(q+1,n+1);
}
/*
	  .      _______    __    ___     ________      ________       _________     _________   ________
	  .     /       \  |  |  /  /    /        \    |        \     /         \   |        |  |   __   \
	  .    /   _____/  |  | /  /    /    ___   \   |   ___   \   |   _______/   |  ______|  |  |  \   \
	  .   /   /        |  |/  /    /    /   \   \  |  |   \   \  |  (______     |  |_____   |  |__/   /
	  .   |  |         |     /     |   /     \  |  |  |    |  |   \        \    |        |  |      __/
	  .   |  |         |     \     |   \     /  |  |  |    |  |    \______  \   |  ______|  |      \
	  .   \   \_____   |  |\  \    \    \___/   /  |  |___/   /    _______) |   |  |_____   |   |\  \
	  .    \        \  |  | \  \    \          /   |         /    /         /   |        |  |   | \  \
	  .     \_______/  |__|  \__\    \________/    |________/     \________/    |________|  |___|  \__\
 */


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <list>
#define mkp make_pair
#define err cout<<"err"<<endl
using namespace std;
const double EPS=1e-12;
typedef long long lon;
typedef unsigned long long ull;
const lon SZ=55,SSZ=21,APB=52,one=11;
const lon INF=0x7f7f7f7f,mod=1000000007;
lon n,m;
char ch[SZ][SSZ];
bool vst[1<<20],ok[1<<20];
vector<int> vct;
int core[SZ][1<<20],same[SZ][SSZ];
double dp[1<<20];
int last[1<<20],num[1<<20];

bool chk(int who,int x)
{
	return __builtin_popcount(core[who][x])==1;
}

void dfs1(int who,int x)
{
	if(vst[x])return;
	vst[x]=1;
	//cout<<x<<endl;
	if(chk(who,x))
	{
		ok[x]=1;
		vct.push_back(x);
		return;
	}
	else
	{
		for(int i=0;i<m;++i)
		{
			if(!(x&(1<<i)))
			{
				dfs1(who,x^(1<<i));
			}
		}
	}
}

double dfs2(lon x)
{
	if(dp[x]>=-EPS)return dp[x];
	double res=0;
	for(lon i=0;i<m;++i)
	{
		if(x&(1<<i))
		{
			lon tmp=((1<<m)-1)^(x^(1<<i));
			lon num=__builtin_popcount(tmp);
			if(!ok[x^(1<<i)])res+=dfs2(x^(1<<i))/num;
		}
	}
	return dp[x]=res;
}

void init()
{
	cin>>n;
	double res=0;
	for(int i=0;i<n;++i)cin>>ch[i];
	m=strlen(ch[0]);
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			for(int k=0;k<n;++k)
			{
				if(ch[i][j]==ch[k][j])
				{
					same[i][j]|=1<<k;
					//if(i==0&&j==)
				}
			}
			//cout<<"i: "<<i<<" "<<j<<" "<<same[i][j]<<endl;
		}
	}
	for(int i=1;i<(1<<m);++i)
	{
		last[i]=__builtin_ctz(i);
		num[i]=__builtin_popcount(i);
	}
	for(int i=0;i<n;++i)
	{
		core[i][0]=(1<<n)-1;
		for(int j=1;j<(1<<m);++j)
		{
			int k=last[j];
				//if(!(j&(1<<k)))
				//{
					core[i][j]=core[i][j^(1<<k)]&same[i][k];
//					if(__builtin_popcount(core[i][j^(1<<k)])==1)
//					{
//						//cout<<i<<" "<<(j^(1<<k))<<endl;
//					}
			//	}
		}
	}

	for(int i=0;i<n;++i)
	{
		if(n==50)
		{
			if(i>=25)return;
		}
		memset(dp,0xc9,sizeof(dp));
		double ans=0;
		dp[0]=1;
		memset(vst,0,sizeof(vst));
		memset(ok,0,sizeof(ok));
		vct.clear();
		dfs1(i,0);
		for(int j=0;j<vct.size();++j)
		{
			double tmp=dfs2(vct[j]);
			//if(i==0)cout<<vct[j]<<" "<<tmp<<endl;
			//ans+=tmp;
			ans+=tmp*num[vct[j]];
		}
		res+=ans;
		//cout<<"sum: "<<sum<<endl;
	}
	res/=n;
	cout<<fixed<<setprecision(10)<<res<<endl;
}

void work()
{

}

int main()
{
	std::ios::sync_with_stdio(0);
	//freopen("d:\\1.txt","r",stdin);
	int casenum;
	//cin>>casenum;
	//cout<<casenum<<endl;
	//for(int time=1;time<=casenum;++time)
	//for(int tim=1;cin>>n;++tim)
	{
		init();
		work();
	}
	return 0;
}


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

int main ()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	set<int> st;
	set<int>:: iterator it;
	string ans = "YES";
	while (n > 0) {
		int num;
		if (n < m) num = n;
		else num = n % m;
		it = st.find(num);
		if (it == st.end()) {
			it.insert(num);
		} else {
			ans = "NO";
		}
		n /= m;
	}

	cout << ans;
	return 0;
}