1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once


#include <cassert><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdint><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/aliases.hpp"

#include "internal/dev_env.hpp"
#include "internal/types.hpp"

#include "numeric/modular/modint_interface.hpp"

#include "template/debug.hpp"


namespace uni {


namespace internal {


template<class, bool>
struct modint_base {};


template<class Mint>
struct modint_base<Mint, false> {
    static constexpr Mint zero = Mint::_raw(0);
    static constexpr Mint one = Mint::_one();
};


template<class Mint>
struct modint_base<Mint, true> {
    static constexpr Mint zero = Mint::_raw(0);
    static inline Mint one = Mint::_one();
};


} // internal



template<internal::modular_context Context>
struct modint : internal::modint_base<modint<Context>, Context::dynamic> {
    using value_type = typename Context::value_type;
    using context = Context;

  private:
    using base = internal::modint_base<modint, context::dynamic>;

    friend base;

    value_type _val = 0;


    static constexpr auto _raw(const value_type v) noexcept(NO_EXCEPT) {
        modint res;
        res._val = v;
        return res;
    }


    static constexpr auto _one() noexcept(NO_EXCEPT)
        requires internal::has_static_one<typename modint::context::reductor>
    {
        return modint::_raw(modint::context::reduction.one);
    }

    static constexpr auto _one() noexcept(NO_EXCEPT)
        requires (!internal::has_static_one<typename modint::context::reductor>)
    {
        return modint::_raw(1);
    }

  public:
    static constexpr int digits = modint::context::reductor::digits;
    static constexpr value_type max() noexcept { return modint::context::reductor::max(); }


    static constexpr void set_mod(const value_type mod) noexcept(NO_EXCEPT)
        requires requires(value_type mod) { modint::context::set_mod(mod); }
    {
        modint::context::set_mod(mod);
        modint::one = modint::_one();
    }


    static constexpr auto mod() noexcept(NO_EXCEPT) { return modint::context::reduction.mod(); }

    static constexpr auto raw(const value_type v) noexcept(NO_EXCEPT)
    {
        modint res;
        res._val = modint::context::reduction.convert_raw(v);
        return res;
    };


    constexpr modint() noexcept = default;

    template<std::integral T>
    constexpr modint(const T v) noexcept(NO_EXCEPT) : _val(modint::context::reduction.convert(v)) {}


    inline constexpr auto val() const noexcept(NO_EXCEPT) {
        return modint::context::reduction.revert(this->_val);
    }

    inline constexpr explicit operator value_type() const noexcept(NO_EXCEPT) { return this->_val; }


    inline constexpr auto& operator+=(const modint& rhs) noexcept(NO_EXCEPT) {
        this->_val = modint::context::reduction.add(this->_val, rhs._val);
        return *this;
    }

    inline constexpr auto& operator-=(const modint& rhs) noexcept(NO_EXCEPT) {
        this->_val = modint::context::reduction.subtract(this->_val, rhs._val);
        return *this;
    }


    inline constexpr auto& operator*=(const modint& rhs) noexcept(NO_EXCEPT) {
        this->_val = modint::context::reduction.multiply(this->_val, rhs._val);
        return *this;
    }

    inline constexpr auto& operator/=(const modint& rhs) noexcept(NO_EXCEPT) { return *this *= rhs.inv(); }


    template<std::integral K>
    constexpr auto pow(const K n) const noexcept(NO_EXCEPT) {
        if constexpr(std::signed_integral<K>) assert(n >= 0);

        return modint::_raw(modint::context::reduction.pow(this->_val, n));
    }


    constexpr auto inv() const noexcept(NO_EXCEPT) {
        using signed_value_type = std::make_signed_t<value_type>;

        signed_value_type x = this->val(), y = modint::mod(), u = 1, v = 0;

        while(y > 0) {
            signed_value_type t = x / y;
            std::swap(x -= t * y, y);
            std::swap(u -= t * v, v);
        }

        assert(x == 1);

        if(u < 0) u += v / x;
        return modint::raw(u);
    }


    friend inline constexpr auto operator<=>(const modint& lhs, const modint& rhs) noexcept(NO_EXCEPT) {
        return modint::context::reduction.compare(lhs._val, rhs._val);
    }

    friend inline constexpr bool operator==(const modint& lhs, const modint& rhs) noexcept(NO_EXCEPT) {
        return lhs <=> rhs == 0;
    }


    inline constexpr auto& operator++() noexcept(NO_EXCEPT) { return *this += modint::one; }
    inline constexpr auto& operator--() noexcept(NO_EXCEPT) { return *this -= modint::one; }

    inline constexpr auto operator++(int) noexcept(NO_EXCEPT) { const modint res = *this; return ++*this, res; }
    inline constexpr auto operator--(int) noexcept(NO_EXCEPT) { const modint res = *this; return --*this, res; }

    inline constexpr auto operator+() const noexcept(NO_EXCEPT) { return *this; }
    inline constexpr auto operator-() const noexcept(NO_EXCEPT) { return modint::zero - *this; }

    friend inline constexpr auto operator+(modint lhs, const modint& rhs) noexcept(NO_EXCEPT) { return lhs += rhs; }
    friend inline constexpr auto operator-(modint lhs, const modint& rhs) noexcept(NO_EXCEPT) { return lhs -= rhs; }
    friend inline constexpr auto operator*(modint lhs, const modint& rhs) noexcept(NO_EXCEPT) { return lhs *= rhs; }
    friend inline constexpr auto operator/(modint lhs, const modint& rhs) noexcept(NO_EXCEPT) { return lhs /= rhs; }
};


} // namespace uni