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

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

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

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

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

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

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

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

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

Num Num::operator+(const Num &other) {
    int64_t val = static_cast<int64_t>(this->value)
                  + static_cast<int64_t>(other.value);
    val %= this->modulo;
    if (val < 0) {
        val += this->modulo;
    }
    return Num(static_cast<int>(val), this->modulo);
}

Num Num::operator-(const Num &other) {
    int64_t val = static_cast<int64_t>(this->value)
                  - static_cast<int64_t>(other.value);
    val %= this->modulo;
    if (val < 0) {
        val += this->modulo;
    }
    return Num(static_cast<int>(val), this->modulo);
}

Num Num::operator*(const Num &other) {
    int64_t val = static_cast<int64_t>(this->value)
                  * static_cast<int64_t>(other.value);
    val %= this->modulo;
    if (val < 0) {
        val += this->modulo;
    }
    return Num(static_cast<int>(val), this->modulo);
}

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

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

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