QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#336405#8279. Segment Treeucup-team133#WA 1ms3836kbC++1731.8kb2024-02-24 16:03:482024-02-24 16:03:49

Judging History

你现在查看的是最新测评结果

  • [2024-02-24 16:03:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3836kb
  • [2024-02-24 16:03:48]
  • 提交

answer

// -fsanitize=undefined,
//#define _GLIBCXX_DEBUG


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <chrono>


#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        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;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
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;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
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;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        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);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
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};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    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

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
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 <algorithm>

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

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
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 <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder





using namespace std;
using namespace atcoder;

using mint = modint998244353;




#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << " )\n";

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


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

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

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<endl;
}



template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const set<T>& S){
  os << "set{";
  for (auto a:S){
    os << a;
    auto it = S.find(a);
    it++;
    if (it!=S.end()){
      os << ", ";
    }
  }
  os << "}";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const map<int,T>& A){
  os << "map{";
  for (auto e:A){
    os << e.first;
    os << ":";
    os << e.second;
    os << ", ";
  }
  os << "}";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ", ";
    }
  }
  os << "]" ;
  return os;
}

ostream& operator<<(ostream& os, const mint& a){
  os << a.val();
  return os;
}

using S = int;

S op(S a,S b){
  return min(a,b);
}

S e(){
  return 1e9;
}

using S2 = mint;

S2 op2(S2 a,S2 b){
  return a + b;
}

S2 e2(){
  return 0;
}

using F = mint;

F composition(F f,F g){
  return f * g;
}

S2 mapping(F f,S2 x){
  return f * x;
}

F id(){
  return 1;
}




void solve(){
  int N,M;
  cin>>N>>M;
  vec<int> X(N-1);
  rep(i,N-1) cin>>X[i];

  vec<pair<int,int>> node_to_lr(2*N-1,{-1,-1});
  vec<int> left_child(N-1),right_child(N-1);
  
  auto dfs = [&](auto self,int v,pair<int,int> lr)->pair<int,int> {
    if (lr.first == lr.second - 1){
      int a = lr.first;
      node_to_lr[N-1+a] = {a,a+1};
      return {v-1,N-1+a};
    };
    node_to_lr[v] = lr;
    auto [last,c1] = self(self,v+1,{lr.first,X[v]});
    left_child[v] = c1;
    auto [last2,c2] = self(self,last+1,{X[v],lr.second});
    right_child[v] = c2;
    return {last2,v};
  };
  dfs(dfs,0,{0,N});

  //debug(node_to_lr);

  vec<set<int>> need_left(2*N-1),need_right(2*N-1);
  vec<int> need_segment(2*N-1,0);
  segtree<S,op,e> seg(N);
  rep(i,N-1){
    seg.set(X[i],i);
  }

  rep(i,M){
    int L,R;
    cin>>L>>R;
    if (R-L==1){
      need_segment[N-1+L] = 1;
      continue;
    }
    int idx = seg.prod(L+1,R);
    //debug(idx);
    if (node_to_lr[idx].first == L && node_to_lr[idx].second == R){
      need_segment[idx] = 1;
      continue;
    }
    int lc = left_child[idx],rc = right_child[idx];
    need_right[rc].insert(R);
    need_left[lc].insert(L);
  }

  auto dfs_need = [&](auto self,int v)->void {
    if (N-1 <= v){
      if (!need_right[v].empty() || !need_left[v].empty()){
        need_segment[v] = 1;
      }
      return ;
    }



    while (!need_right[v].empty()){
      auto r0 = *need_right[v].begin();
      auto r1 = *need_right[v].rbegin();

      if (r1 == node_to_lr[v].second){
        need_segment[v] = 1;
        need_right[v].erase(r1);
        continue;
      }

      if (r0 <= X[v] && X[v] < r1){
        need_segment[left_child[v]] = 1;
        need_right[left_child[v]].insert(r0);
        need_right[right_child[v]].insert(r1);
        need_right[v].erase(r0);
        need_right[v].erase(r1);
        continue;
      }

      if (r0 <= X[v]){
        if (need_right[left_child[v]].size() < need_right[v].size()) swap(need_right[left_child[v]],need_right[v]);
        for (auto r:need_right[v]){
          need_right[left_child[v]].insert(r);
        }
      }
      else if (X[v] < r1){
        need_segment[left_child[v]] = 1;
        if (need_right[right_child[v]].size() < need_right[v].size()) swap(need_right[right_child[v]],need_right[v]);
        for (auto r:need_right[v]){
          need_right[right_child[v]].insert(r);
        }
      }
      break;
    }


    while (!need_left[v].empty()){
      auto l0 = *need_left[v].begin();
      auto l1 = *need_left[v].rbegin();

      

      if (l0 == node_to_lr[v].first){
        need_segment[v] = 1;
        need_left[v].erase(l0);
        continue;
      }


      if (X[v] <= l1 && l0 < X[v]){
        need_segment[right_child[v]] = 1;
        need_left[left_child[v]].insert(l0);
        need_left[right_child[v]].insert(l1);
        need_left[v].erase(l0);
        need_left[v].erase(l1);
        continue;
      }

      if (X[v] <= l1){
        if (need_left[right_child[v]].size() < need_left[v].size()) swap(need_left[right_child[v]],need_left[v]);
        for (auto l:need_left[v]){
          need_left[right_child[v]].insert(l);
        }
      }
      else{
        if (need_left[left_child[v]].size() < need_left[v].size()) swap(need_left[left_child[v]],need_left[v]);
        need_segment[right_child[v]] = 1;
        for (auto l:need_left[v]){
          need_left[left_child[v]].insert(l);
        }
      }
      break;
    }


    self(self,left_child[v]);
    self(self,right_child[v]);
  };

  dfs_need(dfs_need,0);

  //debug(node_to_lr);
  //debug(need_segment);

  /*
  1:vの情報は求められる
  2:vの情報はわからないが、部分木内で必要な情報はすべて求まっている
  3:vの情報はわからず、vの情報が分かった場合ok

  遷移
  vをSに含める場合:
    子が両方3の場合:out
    子が片方2で片方3:out
    これ以外:ok
  vをSに含めない場合:
    子が両方3の場合:out
    子が片方3で片方1の場合:3に遷移
    子が片方3で片方2の場合:out
    子が両方1の場合:1に遷移
    それ以外の場合:vの情報が必要なら3に遷移 そうでないなら2に遷移
  */

  vec<mint> dp1(2*N-1,0);
  vec<mint> dp2(2*N-1,0);
  vec<mint> dp3_sum(2*N-1,0);

  auto dfs_calc_dp = [&](auto self,int v)->void {
    if (N-1 <= v){
      if (need_segment[v]){
        dp1[v] = 1;
        dp3_sum[v] = 1;
      }
      else{
        dp1[v] = 1;
        dp2[v] = 1;
      }
      return ;
    }

    int lc = left_child[v],rc = right_child[v];

    self(self,left_child[v]);
    self(self,right_child[v]);

    dp1[v] += (dp1[lc]+dp2[lc]+dp3_sum[lc]) * (dp1[rc]+dp2[rc]+dp3_sum[rc]) - (dp3_sum[lc] * dp3_sum[rc] + dp3_sum[lc] * dp2[rc] + dp2[lc] * dp3_sum[rc]);

    dp3_sum[v] += dp1[rc] * dp3_sum[lc] + dp1[lc] * dp3_sum[rc];
    dp1[v] += dp1[lc] * dp1[rc];

    if (need_segment[v]){
      dp3_sum[v] += (dp1[lc]+dp2[lc]) * (dp1[rc] + dp2[rc]) - dp1[lc] * dp1[rc];
    }
    else{
      dp2[v] = (dp1[lc]+dp2[lc]) * (dp1[rc] + dp2[rc]) - dp1[lc] * dp1[rc];
    }
  };

  dfs_calc_dp(dfs_calc_dp,0);

  //debug(dp1);
  //debug(dp2);
  //debug(dp3);


  mint ans = dp1[0] + dp2[0];

  cout << ans << "\n";







  


}

  
  







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

  
  int T = 1;
  //cin>>T;
  while (T--){
    solve();
  }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3836kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3612kb

input:

2 1
1
1 2

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

5 2
2 1 4 3
1 3
2 5

output:

193

result:

ok 1 number(s): "193"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

10 10
5 2 1 3 4 7 6 8 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

70848

result:

ok 1 number(s): "70848"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3512kb

input:

2 2
1
0 1
0 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

3 3
1 2
0 1
0 2
0 3

output:

14

result:

ok 1 number(s): "14"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

4 4
1 2 3
0 1
0 2
0 3
0 4

output:

48

result:

ok 1 number(s): "48"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

5 5
3 1 2 4
0 1
0 2
0 3
0 4
0 5

output:

164

result:

ok 1 number(s): "164"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

6 6
4 2 1 3 5
0 1
0 2
0 3
0 4
0 5
0 6

output:

544

result:

ok 1 number(s): "544"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

7 7
3 2 1 5 4 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7

output:

1856

result:

ok 1 number(s): "1856"

Test #11:

score: 0
Accepted
time: 1ms
memory: 3572kb

input:

8 8
3 1 2 4 7 5 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8

output:

6528

result:

ok 1 number(s): "6528"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

9 9
3 1 2 4 7 6 5 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

output:

21520

result:

ok 1 number(s): "21520"

Test #13:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

10 10
8 2 1 3 4 6 5 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10

output:

71296

result:

ok 1 number(s): "71296"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

2 3
1
0 1
0 2
1 2

output:

4

result:

ok 1 number(s): "4"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

3 6
1 2
0 1
0 2
0 3
1 2
1 3
2 3

output:

14

result:

ok 1 number(s): "14"

Test #16:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

4 10
1 2 3
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

48

result:

ok 1 number(s): "48"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

5 15
1 4 3 2
0 1
0 2
0 3
0 4
0 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

output:

164

result:

ok 1 number(s): "164"

Test #18:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

6 21
5 3 1 2 4
0 1
0 2
0 3
0 4
0 5
0 6
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

output:

544

result:

ok 1 number(s): "544"

Test #19:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

7 28
4 1 2 3 6 5
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 2
1 3
1 4
1 5
1 6
1 7
2 3
2 4
2 5
2 6
2 7
3 4
3 5
3 6
3 7
4 5
4 6
4 7
5 6
5 7
6 7

output:

1912

result:

ok 1 number(s): "1912"

Test #20:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

8 36
5 2 1 3 4 7 6
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
1 2
1 3
1 4
1 5
1 6
1 7
1 8
2 3
2 4
2 5
2 6
2 7
2 8
3 4
3 5
3 6
3 7
3 8
4 5
4 6
4 7
4 8
5 6
5 7
5 8
6 7
6 8
7 8

output:

6304

result:

ok 1 number(s): "6304"

Test #21:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

9 45
6 2 1 4 3 5 7 8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 3
2 4
2 5
2 6
2 7
2 8
2 9
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9

output:

20736

result:

ok 1 number(s): "20736"

Test #22:

score: 0
Accepted
time: 1ms
memory: 3824kb

input:

10 55
6 3 2 1 4 5 8 7 9
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 4
3 5
3 6
3 7
3 8
3 9
3 10
4 5
4 6
4 7
4 8
4 9
4 10
5 6
5 7
5 8
5 9
5 10
6 7
6 8
6 9
6 10
7 8
7 9
7 10
8 9
8 10
9 10

output:

70784

result:

ok 1 number(s): "70784"

Test #23:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

2 1
1
0 2

output:

5

result:

ok 1 number(s): "5"

Test #24:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

3 1
2 1
2 3

output:

21

result:

ok 1 number(s): "21"

Test #25:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

4 1
2 1 3
0 1

output:

85

result:

ok 1 number(s): "85"

Test #26:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

5 1
4 1 3 2
0 5

output:

341

result:

ok 1 number(s): "341"

Test #27:

score: -100
Wrong Answer
time: 0ms
memory: 3608kb

input:

6 1
5 1 2 3 4
0 2

output:

1155

result:

wrong answer 1st numbers differ - expected: '1260', found: '1155'