import math


def n_permutations(n, r=None):
    """Number of permutations (unique by position)

    :param n: population length
    :param r: sample length
    :return: int
    """
    if r is None:
        r = n
    if n < 0 or r < 0:
        raise ValueError("n and r must be positive")
    if n == 0 or r > n:
        return 0

    return math.factorial(n) // math.factorial(n - r)