Problem:
Score: 600 points

Problem Statement

How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"?

The answer can be enormous, so print it modulo (10^9+7).

Constraints

K is an integer between 1 and 10^6 (inclusive).

S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

K
S

Output

Print the number of strings satisfying the condition, modulo (10^9+7).

Sample Input 1

5
oof

Sample Output 1

575111451

For example, we can obtain proofend, moonwolf, and onionpuf, while we cannot obtain oofsix, oofelevennn, voxafolt, or fooooooo.

Sample Input 2

37564
whydidyoudesertme

Sample Output 2

318008117

Solution in Rust (1.42.0) (judge verdict: Accepted)

use num::traits::One;
use proconio::input;
use std::{
    fmt,
    iter::Sum,
    ops::{Add, Mul, Neg},
};

fn main() {
    input! {
        k: usize,
        s: String,
    }

    let binomial = Znz::binomial_table(k + s.len());

    let ans = (0..=k)
        .map(|t| {
            num::pow(Znz::new(25), k - t)
                * num::pow(Znz::new(26), t)
                * binomial(s.len() + k - t - 1, s.len() - 1)
        })
        .sum::<Znz>();

    println!("{}", ans);
}

#[derive(Clone, Copy, Debug)]
struct Znz {
    repr: usize,
}

impl Znz {
    const MODULUS: usize = 1_000_000_007;

    fn binomial_table(max: usize) -> impl Fn(usize, usize) -> Self {
        let mut fact = vec![Self::new(0); max + 1];
        let mut fact_inv = vec![Self::new(0); max + 1];
        let mut inv = vec![Self::new(0); max + 1];

        fact[0] = Self::new(1);
        fact[1] = Self::new(1);
        fact_inv[0] = Self::new(1);
        fact_inv[1] = Self::new(1);
        inv[1] = Self::new(1);

        for i in 2..=max {
            fact[i] = fact[i - 1] * Self::new(i);
            inv[i] = -inv[Self::MODULUS % i] * Self::new(Self::MODULUS / i);
            fact_inv[i] = fact_inv[i - 1] * inv[i];
        }

        move |n: usize, k: usize| -> _ {
            if n < k {
                Self::new(0)
            } else {
                fact[n] * fact_inv[k] * fact_inv[n - k]
            }
        }
    }

    fn new(val: usize) -> Self {
        Self {
            repr: val % Self::MODULUS,
        }
    }
}

impl fmt::Display for Znz {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.repr, fmt)
    }
}

impl Neg for Znz {
    type Output = Self;

    fn neg(self) -> Self {
        Self::new(Self::MODULUS - self.repr)
    }
}

impl Add for Znz {
    type Output = Self;

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

impl Mul for Znz {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        Self::new(self.repr * rhs.repr)
    }
}

impl Sum for Znz {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Znz::new(0), Add::add)
    }
}

impl One for Znz {
    fn one() -> Self {
        Self { repr: 1 }
    }
}