#include "num.h"
#include <cstdint>

inline Num::Num(int value, int modulo) : value(value), modulo(modulo) {
    if (modulo != 0) {
        this->value = value % modulo;
    }
}

inline Num& Num::operator=(const Num& other) {
    this->value = other.value;
    return *this;
}

inline Num Num::operator+(const Num& other) {
    return Num((this->value + other.value) % modulo, modulo);
}

inline Num Num::operator-(const Num& other) {
    return Num(this->value - other.value, modulo);
}

inline Num Num::operator*(const Num& other) {
    return Num(((this->value * other.value) % modulo), modulo);
}

inline Num Num::operator+(int num) {
    this->value = (value + num % modulo) % modulo;
    return *this;
}

inline Num Num::operator-(int num) {
    this->value = value - num % modulo;
    return *this;
}

inline Num Num::operator*(int num) {
    this->value = (value * num % modulo) % modulo;
    return *this;
}

inline Num& Num::operator+=(const Num& other) {
    this->value = (value + other.value)% modulo;
    return *this;
}

inline Num& Num::operator-=(const Num& other) {
    if (value - other.value < 0) {
        this->value = value - other.value + modulo;
    } else {
        this->value = value - other.value;
    }
    return *this;
}

inline Num& Num::operator*=(const Num& other) {
    this->value = (value * other.value) % modulo;
    return *this;
}

inline Num& Num::operator+=(int num) {
    this->value = (value + num % modulo) % modulo;
    return *this;
}

inline Num& Num::operator-=(int num) {
    if (value - num % modulo < 0) {
        this->value = value - num % modulo + modulo;
    } else {
        this->value = value - num % modulo;
    }
    return *this;
}

inline Num& Num::operator*=(int num) {
    int64_t a = static_cast<uint64_t>(value);
    int64_t b = static_cast<uint64_t>(num);
    this->value = (a * b % modulo) % modulo;
    return *this;
}