algorist 0.10.2

Algorithms and data structures for competitive programming
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Modular arithmetic
//!
//! Sometimes, especially in competitive programming, we need to perform
//! arithmetic operations under a modulo, for instance, the result is always
//! `actual_result % 1_000_000_007` i.e. result is always `< 1_000_000_007`.
//! This is useful for avoiding overflow when working with large numbers.
//!
//! # Mod7 (mod 1_000_000_007) arithmetic
//!
//! This module provides a [`Modulo`] type that represents numbers under a
//! certain modulo, with number of operations defined on it. By default, the
//! [`Mod7`] type is provided, which uses `1_000_000_007` as the modulo.
//!
//! ## Example
//!
//! ```
//! use algorist::math::modulo::{Mod7, Modulo};
//!
//! assert_eq!(Mod7::new(1_000_000_006).val(), 1_000_000_006);
//! assert_eq!(Mod7::new(1_000_000_007).val(), 0);
//! assert_eq!(Mod7::new(i64::MAX).val(), 291_172_003);
//!
//! assert_eq!(Mod7::new(1) + Mod7::new(2), Mod7::new(3));
//! assert_eq!(Mod7::new(1_000_000_006) + Mod7::new(1), Mod7::new(0));
//! ```
//!
//! To make it easier to work with, you can use the `ma!` (as in *m*odular
//! *a*rithmetic) macro to create a `Mod7` instance:
//!
//! ```
//! use algorist::math::modulo::{Mod7, ma};
//!
//! assert_eq!(ma!(42), Mod7::new(42));
//! assert_eq!(ma!(1_000_000_006).val(), 1_000_000_006);
//! assert_eq!(ma!(1_000_000_007).val(), 0);
//! assert_eq!(ma!(i64::MAX).val(), 291_172_003);
//!
//! assert_eq!(ma!(1) + ma!(2), ma!(3));
//! assert_eq!(ma!(1_000_000_006) + ma!(1), ma!(0));
//! ```
//!
//! # Custom modulo types
//!
//! You can define your own modulo types using the `modulo!` macro, which takes
//! the name of the type, the name of the constant value, the type of the
//! constant value, and the value of the constant.
//!
//! ## Example
//!
//! ```
//! use algorist::math::modulo::{modulo, modulo_alias, Modulo};
//!
//! modulo!(Mod13, Val13: i64 = 13);
//!
//! assert_eq!(Mod13::new(12).val(), 12);
//! assert_eq!(Mod13::new(13).val(), 0);
//! assert_eq!(Mod13::new(i64::MAX).val(), 7);
//!
//! assert_eq!(Mod13::new(1) + Mod13::new(2), Mod13::new(3));
//! assert_eq!(Mod13::new(12) + Mod13::new(1), Mod13::new(0));
//! assert_eq!(Mod13::new(12) - Mod13::new(1), Mod13::new(11));
//! assert_eq!(Mod13::new(12) * Mod13::new(2), Mod13::new(11));
//!
//! modulo_alias!(Mod13, ma);
//! assert_eq!(ma!(12) + ma!(2), ma!(1));
//! assert_eq!(ma!(12) * ma!(2), ma!(11));
//! ```

use {
    crate::{
        ext::slice::sum::{MaxSum, max_sum_from_iter},
        math::{ConstValue, Downcast, Invertible, Number, gcd::gcd_extended},
    },
    std::{
        cmp::PartialOrd,
        fmt::{Debug, Display},
        marker::PhantomData,
        ops::*,
        str::FromStr,
    },
};

/// A type representing numbers under a modulo `M`.
///
/// This type is generic over the number type `T` and a constant value type `M`
///
/// # Example
///
/// ```
/// use algorist::math::modulo::{Modulo};
/// use algorist::math::value;
///
/// value!(Val7: i64 = 1_000_000_007);
/// pub type Mod7 = Modulo<i64, Val7>;
///
/// assert_eq!(Mod7::new(1_000_000_006).val(), 1_000_000_006);
/// assert_eq!(Mod7::new(1_000_000_007).val(), 0);
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Hash, Default)]
pub struct Modulo<T, M: ConstValue<T>> {
    val: T,
    _phantom: PhantomData<M>,
}

impl<T: Number, M: ConstValue<T>> Modulo<T, M> {
    /// Creates a new `Modulo` instance without checking the value.
    ///
    /// # Panics
    ///
    /// If the value is not in the range `[0, M::val())`, it will panic.
    ///
    /// # Example
    ///
    /// ```
    /// use algorist::math::modulo::Mod7;
    ///
    /// assert_eq!(Mod7::new_unchecked(1_000_000_006).val(), 1_000_000_006);
    /// ```
    ///
    /// The following will panic:
    ///
    /// ``` should_panic
    /// use algorist::math::modulo::Mod7;
    ///
    /// Mod7::new_unchecked(1_000_000_007);
    /// ```
    pub fn new_unchecked(val: T) -> Self {
        assert!(
            val >= T::zero() && val < M::value(),
            "Invalid modulo value: {val}"
        );
        Self {
            val,
            _phantom: PhantomData,
        }
    }

    /// Creates a new `Modulo` instance, checking the value.
    ///
    /// # Example
    ///
    /// ```
    /// use algorist::math::modulo::Mod7;
    ///
    /// assert_eq!(Mod7::new(1_000_000_006).val(), 1_000_000_006);
    /// assert_eq!(Mod7::new(1_000_000_007).val(), 0);
    /// ```
    pub fn new(mut val: T) -> Self {
        if val < T::zero() {
            val += M::value();
            if val < T::zero() {
                val %= M::value();
                return Self::new(val);
            }
        } else if val >= M::value() {
            val -= M::value();
            if val >= M::value() {
                val %= M::value();
            }
        }
        Self::new_unchecked(val)
    }

    /// Returns the raw value of the modulo.
    ///
    /// # Example
    ///
    /// ```
    /// use algorist::math::modulo::Mod7;
    ///
    /// assert_eq!(Mod7::new(1_000_000_006).val(), 1_000_000_006);
    /// assert_eq!(Mod7::new(1_000_000_007).val(), 0);
    /// ```
    pub fn val(&self) -> T {
        self.val
    }
}

impl<T, M> Modulo<T, M>
where
    T: Number + Downcast + BitAnd<Output = T> + ShrAssign<T>,
    T::Source: Number,
    M: ConstValue<T>,
{
    /// Raises the modulo number to the power of `exp`.
    ///
    /// # Example
    ///
    /// ```
    /// use algorist::math::modulo::Mod7;
    ///
    /// assert_eq!(Mod7::new(2).pow(3).val(), 8);
    /// assert_eq!(Mod7::new(2).pow(1_000_000_006).val(), 1);
    /// ```
    #[must_use]
    pub fn pow(self, mut exp: T) -> Self {
        let mut result = Self::new(T::one());
        let mut base = self;
        while exp > T::zero() {
            if exp & T::one() == T::one() {
                result *= base;
            }
            base *= base;
            exp >>= T::one();
        }
        result
    }
}

impl<T: Number, M: ConstValue<T>> From<T> for Modulo<T, M> {
    fn from(num: T) -> Self {
        Self::new(num)
    }
}

impl<T: Number, M: ConstValue<T>> FromStr for Modulo<T, M> {
    type Err = <T as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        T::from_str(s).map(Self::new)
    }
}

impl<T: Number + Ord, M: ConstValue<T>> MaxSum for [Modulo<T, M>] {
    type Output = Modulo<T, M>;

    fn max_sum(&self) -> Self::Output {
        Modulo::<T, M>::from(max_sum_from_iter(self.iter().map(|m| m.val)))
    }
}

impl<T: Number, M: ConstValue<T>> Debug for Modulo<T, M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.val, f)
    }
}

impl<T: Number, M: ConstValue<T>> Display for Modulo<T, M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.val, f)
    }
}

impl<T, M> Invertible for Modulo<T, M>
where
    T: Number + Downcast,
    T::Source: Number,
    M: ConstValue<T>,
{
    type Output = Self;

    fn inverse(&self) -> Option<Self> {
        let (d, x, _) = gcd_extended(self.val, M::value());
        if d == T::one() {
            Some(Self::new(T::downcast(x % M::value().into())))
        } else {
            None
        }
    }
}

impl<T: Number, M: ConstValue<T>> Add for Modulo<T, M> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.val + rhs.val)
    }
}

impl<T: Number, M: ConstValue<T>> AddAssign for Modulo<T, M> {
    fn add_assign(&mut self, rhs: Self) {
        *self = Self::new(self.val + rhs.val);
    }
}

impl<T: Number, M: ConstValue<T>> Sub for Modulo<T, M> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.val - rhs.val)
    }
}

impl<T: Number, M: ConstValue<T>> SubAssign for Modulo<T, M> {
    fn sub_assign(&mut self, rhs: Self) {
        *self = Self::new(self.val - rhs.val);
    }
}

impl<T, M> Mul for Modulo<T, M>
where
    T: Number + Downcast,
    T::Source: Number,
    M: ConstValue<T>,
{
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        Self::new(T::downcast(
            T::Source::from(self.val) * T::Source::from(rhs.val) % T::Source::from(M::value()),
        ))
    }
}

impl<T, M> MulAssign for Modulo<T, M>
where
    T: Number + Downcast,
    T::Source: Number,
    M: ConstValue<T>,
{
    fn mul_assign(&mut self, rhs: Self) {
        *self = Self::new(T::downcast(
            T::Source::from(self.val) * T::Source::from(rhs.val) % T::Source::from(M::value()),
        ));
    }
}

impl<T, M> Div for Modulo<T, M>
where
    T: Number + Downcast,
    T::Source: Number,
    M: ConstValue<T>,
{
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn div(self, rhs: Self) -> Self {
        self * rhs.inverse().expect("Division by zero")
    }
}

impl<T, M> DivAssign for Modulo<T, M>
where
    T: Number + Downcast,
    T::Source: Number,
    M: ConstValue<T>,
{
    #[allow(clippy::suspicious_op_assign_impl)]
    fn div_assign(&mut self, rhs: Self) {
        *self *= rhs.inverse().expect("Division by zero");
    }
}

impl<T: Number, M: ConstValue<T>> Neg for Modulo<T, M> {
    type Output = Self;

    fn neg(self) -> Self {
        Self::new(M::value() - self.val)
    }
}

#[macro_export]
macro_rules! modulo_alias_impl {
    ($name:ident, $macro_name:ident) => {
        #[allow(non_local_definitions)]
        #[macro_export]
        macro_rules! $macro_name {
            ($val: expr) => {
                $name::new($val)
            };
        }
        pub use $macro_name;
    };
}
pub use modulo_alias_impl as modulo_alias;

#[macro_export]
macro_rules! modulo_impl {
    ($name:ident, $vname:ident : $t:ty = $val:expr) => {
        $crate::math::value!($vname: $t = $val);
        pub type $name = $crate::math::modulo::Modulo<$t, $vname>;
    };
}
pub use modulo_impl as modulo;

modulo!(Mod7, Val7: i64 = 1_000_000_007);
modulo_alias_impl!(Mod7, ma);

#[cfg(test)]
mod tests {
    use {super::*, std::i64};

    #[test]
    fn modulo_creation() {
        let test_cases = vec![
            (-1, 1_000_000_006),
            (-2_000_000_014, 0),
            (-2_000_000_013, 1),
            (i64::MIN, 708_828_003),
            (0, 0),
            (1_000_000_006, 1_000_000_006),
            (1_000_000_007, 0),
            (i64::MAX, i64::MAX % Val7::value()),
        ];

        for &(val, expected) in test_cases.iter() {
            let m = Mod7::new(val);
            assert_eq!(m.val, expected, "new()");
        }

        for (val, expected) in test_cases {
            let m = Mod7::from(val);
            assert_eq!(m.val, expected, "from()");
        }
    }

    #[test]
    fn modulo_addition() {
        let test_cases = vec![
            (1, 2, 3),
            (1_000_000_006, 1, 0),
            (1_000_000_006, 1_000_000_006, 1_000_000_005),
            (1_000_000_006, 1_000_000_007, 1_000_000_006),
            (1_000_000_007, 1_000_000_007, 0),
            (1_000_000_007, 1_000_000_008, 1),
            (i64::MAX, 1, i64::MAX % Val7::value() + 1),
            (i64::MAX, 1_000_000_007, i64::MAX % Val7::value()),
            (i64::MAX, 1_000_000_008, i64::MAX % Val7::value() + 1),
            (
                i64::MAX,
                i64::MAX,
                i64::MAX % Val7::value() * 2 % Val7::value(),
            ),
            (-1, 1, 0),
            (-1, -1, 1_000_000_005),
            (-1, -2, 1_000_000_004),
            (-1, -1_000_000_007, 1_000_000_006),
            (-1, -1_000_000_008, 1_000_000_005),
            (-1, i64::MIN, 708_828_002),
            (i64::MIN, i64::MIN, 417_655_999),
            (-1, -1_000_000_007, 1_000_000_006),
            (-1, -1_000_000_008, 1_000_000_005),
        ];

        for &(a, b, expected) in &test_cases {
            let m = Mod7::new(a) + Mod7::new(b);
            assert_eq!(m.val, expected, "add()");
        }

        for (a, b, expected) in test_cases {
            let mut m = Mod7::new(a);
            m += Mod7::new(b);
            assert_eq!(m.val, expected, "add_assign()");
        }
    }

    #[test]
    fn modulo_subtraction() {
        let test_cases = vec![
            (1, 2, 1_000_000_006),
            (1_000_000_006, 1, 1_000_000_005),
            (1_000_000_006, 1_000_000_006, 0),
            (1_000_000_006, 1_000_000_007, 1_000_000_006),
            (1_000_000_007, 1_000_000_007, 0),
            (1_000_000_007, 1_000_000_008, 1_000_000_006),
            (i64::MAX, 1, i64::MAX % Val7::value() - 1),
            (i64::MAX, 1_000_000_007, i64::MAX % Val7::value()),
            (i64::MAX, 1_000_000_008, i64::MAX % Val7::value() - 1),
            (i64::MAX, i64::MAX, 0),
            (-1, 1, 1_000_000_005),
            (-1, -1, 0),
            (-1, -2, 1),
            (-1, -1_000_000_007, 1_000_000_006),
            (-1, -1_000_000_008, 0),
            (-1, i64::MIN, 291_172_003),
            (i64::MIN, i64::MIN, 0),
            (-1, -1_000_000_007, 1_000_000_006),
            (-1, -1_000_000_008, 0),
        ];

        for &(a, b, expected) in &test_cases {
            let m = Mod7::new(a) - Mod7::new(b);
            assert_eq!(m.val, expected, "sub()");
        }

        for (a, b, expected) in test_cases {
            let mut m = Mod7::new(a);
            m -= Mod7::new(b);
            assert_eq!(m.val, expected, "sub_assign()");
        }
    }

    #[test]
    fn modulo_multiplication() {
        let test_cases = vec![
            (1, 2, 2),
            (1_000_000_006, 1, 1_000_000_006),
            (1_000_000_006, 2, 1_000_000_005),
            (1_000_000_006, 1_000_000_006, 1),
            (1_000_000_006, 1_000_000_007, 0),
            (1_000_000_007, 1_000_000_007, 0),
            (1_000_000_007, 1_000_000_008, 0),
            (i64::MAX, 1, i64::MAX % Val7::value()),
            (i64::MAX, 1_000_000_007, 0),
            (i64::MAX, 1_000_000_008, 291_172_003),
            (i64::MAX, i64::MAX, 737_564_071),
            (-1, 1, 1_000_000_006),
            (-1, -1, 1),
            (-1, -2, 2),
            (-1, -1_000_000_007, 0),
            (-1, -1_000_000_008, 1),
            (-1, i64::MIN, 291_172_004),
            (i64::MIN, i64::MIN, 319_908_071),
            (-1, -1_000_000_007, 0),
            (-1, -1_000_000_008, 1),
        ];

        for &(a, b, expected) in &test_cases {
            let m = Mod7::new(a) * Mod7::new(b);
            assert_eq!(m.val, expected, "mul()");
        }

        for (a, b, expected) in test_cases {
            let mut m = Mod7::new(a);
            m *= Mod7::new(b);
            assert_eq!(m.val, expected, "mul_assign()");
        }
    }

    #[test]
    fn modulo_inverse() {
        let test_cases = vec![
            (1, 1),
            (2, 500000004),
            (1_000_000_006, 1_000_000_006),
            (1_000_000_008, 1),
            (i64::MAX, 933_137_596),
            (-1, 1_000_000_006),
            (-2, 500000003),
            (-1_000_000_008, 1_000_000_006),
        ];

        for &(val, expected) in &test_cases {
            let m = Mod7::new(val);
            let inv = m.inverse().unwrap();
            assert_eq!(inv.val, expected, "inverse()");
            assert_eq!(m * inv, Mod7::new(1), "inverse()");
        }
    }

    #[test]
    fn modulo_division() {
        let test_cases = vec![
            (1, 1, 1),
            (2, 2, 1),
            (1_000_000_006, 1_000_000_006, 1),
            (1_000_000_008, 2, 500_000_004),
            (i64::MAX, 2, 645_586_005),
            (-1, 1, 1_000_000_006),
            (-2, 2, 1_000_000_006),
            (-1_000_000_008, 2, 500_000_003),
        ];

        for &(a, b, expected) in &test_cases {
            let m = Mod7::new(a) / Mod7::new(b);
            assert_eq!(m.val, expected, "div()");
        }

        for (a, b, expected) in test_cases {
            let mut m = Mod7::new(a);
            m /= Mod7::new(b);
            assert_eq!(m.val, expected, "div_assign()");
        }
    }

    #[test]
    fn modulo_negation() {
        let test_cases = vec![
            (1, 1_000_000_006),
            (1_000_000_006, 1),
            (1_000_000_008, 1_000_000_006),
            (i64::MAX, 708_828_004),
            (-1, 1),
            (-2, 2),
            (-1_000_000_008, 1),
        ];

        for &(val, expected) in &test_cases {
            let m = -Mod7::new(val);
            assert_eq!(m.val, expected, "neg()");
        }
    }

    #[test]
    fn modulo_pow() {
        let test_cases = vec![
            (1, 0i64, 1),
            (1, 1, 1),
            (1, 2, 1),
            (1, 1_000_000_006, 1),
            (1, 1_000_000_008, 1),
            (1, i32::MAX as i64, 1),
            (2, 1, 2),
            (2, 5, 32),
            (2, 1_000_000_006, 1),
            (2, 1_000_000_008, 4),
            (2, i32::MAX as i64, 914_893_544),
            (i64::MAX, 1, 291_172_003),
            (i64::MAX, 2, 737_564_071),
            (i64::MAX, 1_000_000_006, 1),
            (i64::MAX, 1_000_000_008, 737_564_071),
            (i64::MAX, i32::MAX as i64, 840_154_026),
            (-1, 1, 1_000_000_006),
            (-1, 2, 1),
            (-1, 1_000_000_006, 1),
            (-1, i32::MAX as i64, 1_000_000_006),
            (-2, 1, 1_000_000_005),
            (-2, 5, 999_999_975),
            (-2, 1_000_000_006, 1),
            (-2, 1_000_000_008, 4),
            (-i64::MAX, 10, 394_962_753),
            (-i64::MAX, 1_000_000_006, 1),
            (-i64::MAX, 1_000_000_008, 737_564_071),
            (-i64::MAX, i32::MAX as i64, 159_845_981),
        ];

        for &(base, exp, expected) in &test_cases {
            let m = Mod7::new(base).pow(exp);
            assert_eq!(m.val, expected, "pow()");
        }
    }

    #[test]
    fn modulo_from_str() {
        let test_cases = vec![
            ("0".to_string(), 0),
            ("1".to_string(), 1),
            ("1000000006".to_string(), 1_000_000_006),
            ("1000000007".to_string(), 0),
            ("1000000008".to_string(), 1),
            ("1000000009".to_string(), 2),
            ("1000000010".to_string(), 3),
            (format!("{}", i64::MAX), i64::MAX % Val7::value()),
        ];

        for (s, expected) in test_cases {
            let m: Mod7 = s.parse().unwrap();
            assert_eq!(m.val, expected, "from_str()");
        }
    }

    #[test]
    fn modulo_max_sum() {
        let test_cases = vec![
            (vec![1, 2, 3, 4, 5], 15),
            (vec![1, -2, 3, -4, 5], 3),
            (vec![-1, -2, -3, -4, -5], 999999992),
            (vec![1, 2, 3, 4, -1, 5, -1, -2, -3, -4, -5], 1000000006),
        ];

        for (arr, expected) in test_cases {
            let arr: Vec<Mod7> = arr.into_iter().map(Mod7::new).collect();
            let m = arr.max_sum();
            assert_eq!(m.val, expected, "max_sum()");
        }
    }

    #[test]
    fn custom_modulo() {
        modulo!(Mod13, Val13: i64 = 13);

        assert_eq!(Mod13::new(12).val(), 12);
        assert_eq!(Mod13::new(13).val(), 0);
        assert_eq!(Mod13::new(i64::MAX).val(), 7);

        assert_eq!(Mod13::new(1) + Mod13::new(2), Mod13::new(3));
        assert_eq!(Mod13::new(12) + Mod13::new(1), Mod13::new(0));
        assert_eq!(Mod13::new(12) - Mod13::new(1), Mod13::new(11));
        assert_eq!(Mod13::new(12) * Mod13::new(2), Mod13::new(11));

        modulo_alias!(Mod13, ma13);
        assert_eq!(ma13!(12) * ma13!(2), ma13!(11));
    }
}