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

Num::Num(int value, int modulo) : value(value), modulo(modulo) {}

Num &Num::operator=(const Num &other) = default;

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

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

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

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

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

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

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

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

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

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

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

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