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



Sample input:
5
oof


Sample output:
575111451

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from math import factorial

stdin = sys.stdin
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))


def nCr(n,r):
    return (factorial(n) // factorial(r) // factorial(n - r))


def main():
    k = ni()
    s = ns()
    n = len(s)
    mod = 1000000007
    ans = 0
    tmp1 = factorial(n+k)%mod
    tmp2 = 1
    tmp3 = tmp1
    for m in range(n):
        ans += ((nCr(n+k,m)%mod)*pow(25,n+k-m,mod))%mod
    ans = pow(26,n+k,mod) - ans
    ans %= mod
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
S = input()
N = len(S)

MOD = 10**9+7
n = N+K
fac = [1]*(n+1)
rev = [1]*(n+1)
 
for i in range(1,n+1):
  fac[i] = i*fac[i-1]%MOD
  rev[i] = pow(fac[i], MOD-2, MOD)
 
comb = lambda a,b:(fac[a]*rev[a-b]*rev[b])%MOD

ans = 0
for i in range(K+1):
    ans += pow(26, K-i, MOD) * pow(25, i, MOD) * comb(N+i-1, i)
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit


def comb(n, r, p):
    r = min(r, n - r)
    ret = fac[n]
    ret = ret * facinv[r] % p
    return ret * facinv[n - r] % p


@njit
def comb_init(N_UNTIL, p):
    N_UNTIL += 1
    inv = np.empty(N_UNTIL, np.int64)
    fac = np.empty(N_UNTIL, np.int64)
    facinv = np.empty(N_UNTIL, np.int64)
    inv[0] = 0
    inv[1] = 1
    fac[0] = 1
    facinv[0] = 1
    for i in range(2, N_UNTIL):
        inv[i] = -inv[p % i] * (p // i) % p
    for i in range(1, N_UNTIL):
        fac[i] = fac[i - 1] * i % p
    for i in range(1, N_UNTIL):
        facinv[i] = facinv[i - 1] * inv[i] % p
    return inv, fac, facinv


K = int(input())
S = input()
N = len(S)
MOD = 1_000_000_007
inv, fac, facinv = comb_init(N + K - 1, MOD)
ans = 0
for i in range(K + 1):
    tmp = pow(25, K - i, MOD)
    tmp *= comb(N + K - i - 1, K - i, MOD)
    tmp %= MOD
    tmp *= pow(26, i, MOD)
    tmp %= MOD
    ans += tmp
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
s = input()
L = len(s)
mod = 10**9+7

com = [1]*(k+1)
p25 = [1]*(k+1)
p26 = [1]*(k+1)

for i in range(k):
  com[i+1] = com[i]*(L+i)//(i+1)
  p25[i+1] = p25[i]*25%mod
  p26[i+1] = p26[i]*26%mod

ans = 0
for p in range(k+1):
  chk = com[p]%mod
  chk *= p25[p]%mod
  chk *= p26[k-p]%mod
  ans += chk
  ans %= mod
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
s = input()
L = len(s)
mod = 10**9+7

com = [1]*(k+1)
p25 = [1]*(k+1)
p26 = [1]*(k+1)

for i in range(k):
  com[i+1] = com[i]*(L+i)//(i+1)
  p25[i+1] = p25[i]*25%mod
  p26[i+1] = p26[i]*26%mod

ans = 0
for p in range(k+1):
  chk = p25[p]*com[p]%mod
  chk *= p26[k-p]%mod
  ans += chk
  ans %= mod
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

def perm(n,k):
    if k > n or k < 0: return 0
    return fact[n] * fact_inv[n-k] % MOD

def cmb(n, k):
    if k < 0 or k > n: return 0
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 2 * 10 ** 6 + 100 # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)
K = ir()
S = sr()
length = len(S)
# Sの文字の最後の位置
answer = 0
power26 = [1] * (K+1)
for i in range(K, 0, -1):
    power26[i-1] = power26[i] * 26 % MOD

power25 = [1] * (K+1)
for i in range(K):
    power25[i+1] = power25[i] * 25 % MOD

for i in range(K+1):
    temp = cmb(i+length-1, length-1) * power26[i] % MOD * power25[i] % MOD
    answer += temp
    answer %= MOD
    
print(answer%MOD)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

def perm(n,k):
    if k > n or k < 0: return 0
    return fact[n] * fact_inv[n-k] % MOD

def cmb(n, k):
    if k < 0 or k > n: return 0
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 2 * 10 ** 6 + 100 # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)
K = ir()
S = sr()
length = len(S)
# Sの文字の最後の位置
answer = 0
power26 = [1] * (K+1)
for i in range(K, 0, -1):
    power26[i-1] = power26[i] * 26 % MOD

power25 = [1] * (K+1)
for i in range(K):
    power25[i+1] = power25[i] * 25 % MOD

for i in range(K+1):
    temp = cmb(i+length-1, length-1) * power26[i] % MOD * power25[i] % MOD
    answer += temp
    
print(answer%MOD)

Submission status: Time Limit Exceeded

Solution code:
def comb(n, r, p):
    return fac[n] * facinv[r] * facinv[n - r] % p


def comb_pre(N_UNTIL, p):
    for i in range(2, N_UNTIL + 1):
        fac.append(fac[i - 1] * i % p)
        inv.append(-inv[p % i] * (p // i) % p)
        facinv.append(facinv[-1] * inv[-1] % p)


fac = [1, 1]
facinv = [1, 1]
inv = [0, 1]

K = int(input())
S = input()
N = len(S)
MOD = 1_000_000_007
comb_pre(N + K - 1, MOD)
ans = 0
for i in range(K + 1):
    tmp = pow(25, K - i, MOD)
    tmp *= comb(N + K - i - 1, K - i, MOD)
    tmp %= MOD
    tmp *= pow(26, i, MOD)
    tmp %= MOD
    ans += tmp
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
K=int(input())
s=input()
S=len(s)
mod=10**9+7
MAX_N=10**7
fact=[1]
fact_inv=[0]*(MAX_N+4)
for i in range(MAX_N+3):
  fact.append(fact[-1]*(i+1)%mod)

fact_inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(MAX_N+2,-1,-1):
  fact_inv[i]=fact_inv[i+1]*(i+1)%mod

def mod_comb_k(n,k,mod):
  return fact[n]*fact_inv[k]%mod*fact_inv[n-k] %mod

cnt=0
for i in range(K+1):
  a=(pow(26,i,mod)*pow(25,K-i,mod))*(mod_comb_k(K+S-i-1,S-1,mod))
  a%=mod
  cnt+=a
  cnt%=mod
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7

class Combination:
    def __init__(self, size, MOD):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    S = input()
    N = len(S)

    comb = Combination(N + K + 100, MOD)

    ans = 0
    L = 1
    R = pow(26, K, MOD)
    inv26 = comb.inv[26]
    for l in range(K + 1):
        ans += L * comb.nhr(N, l) * R
        ans %= MOD

        L = L * 25 % MOD
        R = R * inv26 % MOD

    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    S = input()
    N = len(S)

    comb = Combination(N + K + 100)

    ans = 0
    L = 1
    R = pow(26, K, MOD)
    inv26 = comb.inv[26]
    for l in range(K + 1):
        ans += L * comb.nhr(N, l) * R
        ans %= MOD

        L = L * 25 % MOD
        R = R * inv26 % MOD

    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    S = input()
    N = len(S)

    comb = Combination(N + K + 100)

    ans = 0
    L = 1
    R = pow(26, K, MOD)
    for l in range(K + 1):
        ans += L * comb.nhr(N, l) * R
        ans %= MOD

        L = L * 25 % MOD
        R = R * comb.inv[26] % MOD

    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

K = int(input())
S = input()
N = len(S)

comb = Combination(N + K + 100)

ans = 0
L = 1
R = pow(26, K, MOD)
for l in range(K + 1):
    r = K - l
    ans += L * comb.nhr(N, l) * R
    ans %= MOD

    L = L * 25 % MOD
    R = R * comb.inv[26] % MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
class Combination:
    def __init__(self, size, MOD):
        self.MOD = MOD
        self.size = size + 2

        f = 1
        self.fact = fact = [f]
        for i in range(1, size+1):
            f = f * i % MOD
            fact.append(f)
        f = pow(f, MOD-2, MOD)
        self.factInv = factInv = [f]
        for i in range(size, 0, -1):
            f = f * i % MOD
            factInv.append(f)
        factInv.reverse()

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.MOD) % self.MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    N = len(input())
    MOD = 10**9 + 7
    comb = Combination(N + K + 10, MOD)

    ans = 0
    n = 1
    for l in range(K + 1):
        r = K - l
        ans += comb.nhr(N, l) * n * pow(26, r, MOD)
        ans %= MOD
        n = n * 25 % MOD
    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
class Combination:
    def __init__(self, size, MOD):
        self.MOD = MOD
        self.size = size + 2

        f = 1
        self.fact = fact = [f]
        for i in range(1, size+1):
            f = f * i % MOD
            fact.append(f)
        f = pow(f, MOD-2, MOD)
        self.factInv = factInv = [f]
        for i in range(size, 0, -1):
            f = f * i % MOD
            factInv.append(f)
        factInv.reverse()

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.MOD) % self.MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    N = len(input())
    MOD = 10**9 + 7
    comb = Combination(N + K + 10, MOD)

    ans = 0
    for l in range(K + 1):
        r = K - l
        ans += comb.nhr(N, l) * pow(25, l, MOD) * pow(26, r, MOD)
    print(ans % MOD)
sol()

Submission status: Time Limit Exceeded

Solution code:
class Combination:
    def __init__(self, size, MOD):
        self.MOD = MOD
        self.size = size + 2

        f = 1
        self.fact = fact = [f]
        for i in range(1, size+1):
            f = f * i % MOD
            fact.append(f)
        f = pow(f, MOD-2, MOD)
        self.factInv = factInv = [f]
        for i in range(size, 0, -1):
            f = f * i % MOD
            factInv.append(f)
        factInv.reverse()

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.MOD) % self.MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    N = len(input())
    MOD = 10**9 + 7
    comb = Combination(N + K + 10, MOD)

    ans = 0
    for l in range(K + 1):
        r = K - l
        ans += comb.nhr(N, l) * pow(25, l, MOD) * pow(26, r, MOD)
        ans %= MOD
    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
class Combination:
    def __init__(self, size, MOD):
        self.MOD = MOD
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.MOD) % self.MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

def sol():
    K = int(input())
    N = len(input())
    MOD = 10**9 + 7
    comb = Combination(N + K + 10, MOD)

    ans = 0
    for l in range(K + 1):
        r = K - l
        ans += comb.nhr(N, l) * pow(25, l, MOD) * pow(26, r, MOD)
        ans %= MOD
    print(ans)
sol()

Submission status: Time Limit Exceeded

Solution code:
class Combination:
    def __init__(self, size, MOD):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

K = int(input())
N = len(input())
MOD = 10**9 + 7
comb = Combination(N + K + 10, MOD)

ans = 0
for l in range(K + 1):
    r = K - l
    ans += comb.nhr(N, l) * pow(25, l, MOD) * pow(26, r, MOD)
    ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
s = input()
L = len(s)
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

mod = 10**9+7
ans = 0
for p in range(L-1,L+k):
  chk = 1
  fact, fact_inv = make_array_for_comb(p,mod)
  chk *= pow(25,p-L+1,mod)
  chk *= comb(p,L-1,mod)
  chk %= mod
  chk *= pow(26,L+k-1-p,mod)
  ans += chk
  ans %= mod
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
 
# from math import factorial as fac
from collections import defaultdict
# from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
# print(f.readline())
sys.setrecursionlimit(10**2)

	
def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline().strip())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())
def ri():
	return int(f.readline().strip())
def ria():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def rs():
	return list(f.readline().strip())
def bi(x):
	return bin(x)[2:]





from collections import deque
import math
NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) : 
	M = NUMBER
	f = 1
  
	for i in range(1, n + 1):  
		f = (f * i) % M # Now f never can  
						# exceed 10^9+7  
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
def choose(n,k):
	if n < k:
		assert false
	return  mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER
from collections import deque, defaultdict 
import heapq


from types import GeneratorType
def bootstrap(f, stack=[]):
	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to

	return wrappedfunc


def dfs(g, timeIn, timeOut,depths,parents): 
	# assign In-time to node u 
	cnt = 0
	# node, neig_i, parent, depth
	stack = [[1,0,0,0]]
	while stack:
		v,neig_i,parent,depth = stack[-1]
		parents[v] = parent
		depths[v] = depth
		# print (v)
		if neig_i == 0:
			timeIn[v] = cnt    
			cnt+=1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i+=1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u,0,v,depth+1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
  
# def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str: 
# 	return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u] 

cnt = 0
@bootstrap
def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt+=1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1)
	timeOut[v] = cnt
	cnt+=1
	yield 


def solution(s,n):
	# fact = [1]*(10**6+1)
	# for i in range(1,10**6+1):
	# 	fact[i] = (fact[i-1]*i)%NUMBER
	p = NUMBER
	summ = 0
	k = len(s)
	n = n+k
	d = 26
	mxn = 2*10**6
	pows = [0]*(mxn+1)
	for i in range(1,mxn+1):
		pows[i] = pow(i,p-2,p)
	chooses = [1]*(mxn+1)
	for i in range(1,mxn+1):
		chooses[i] = (chooses[i-1] * (n-(i-1)) *(pows[i]))%p
	dpows = [0]*(mxn+1)
	for i in range(1,mxn+1):
		dpows[i] = pow(d-1,i,p)	
	# def chooseb(l,r):
	# 	return mult(fact[l],pow(mult(fact[r],fact[l-r]),p-2,p))
	for j in range(n-k+1):
		# print(j)
		summ=plus(summ,mult(chooses[j+k],dpows[n-k-j]))
		# summ=mult(summ,)
	return summ + 1
	
def main():
	T = 1
	# T = ri()
	for i in range(T):
		n = ri()
		s = rs()
		# a = ria()
		# n,k = ria()
		# a=ria()

		x = solution(s,n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			print >>output,str(x)# "Case #"+str(i+1)+':',
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()

if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
#example usage:
#    for l in res:
#       print >>output, str(len(l)) + ' ' +  ' '.join(l)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
 
# from math import factorial as fac
from collections import defaultdict
# from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
# print(f.readline())
sys.setrecursionlimit(10**2)

	
def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline().strip())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())
def ri():
	return int(f.readline().strip())
def ria():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def rs():
	return list(f.readline().strip())
def bi(x):
	return bin(x)[2:]





from collections import deque
import math
NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) : 
	M = NUMBER
	f = 1
  
	for i in range(1, n + 1):  
		f = (f * i) % M # Now f never can  
						# exceed 10^9+7  
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
def choose(n,k):
	if n < k:
		assert false
	return  mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER
from collections import deque, defaultdict 
import heapq


from types import GeneratorType
def bootstrap(f, stack=[]):
	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to

	return wrappedfunc


def dfs(g, timeIn, timeOut,depths,parents): 
	# assign In-time to node u 
	cnt = 0
	# node, neig_i, parent, depth
	stack = [[1,0,0,0]]
	while stack:
		v,neig_i,parent,depth = stack[-1]
		parents[v] = parent
		depths[v] = depth
		# print (v)
		if neig_i == 0:
			timeIn[v] = cnt    
			cnt+=1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i+=1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u,0,v,depth+1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
  
def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str: 
	return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u] 

cnt = 0
@bootstrap
def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt+=1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1)
	timeOut[v] = cnt
	cnt+=1
	yield 


def solution(s,n):
	# fact = [1]*(10**6+1)
	# for i in range(1,10**6+1):
	# 	fact[i] = (fact[i-1]*i)%NUMBER
	p = NUMBER
	summ = 0
	k = len(s)
	n = n+k
	d = 26
	mxn = 10**6
	pows = [0]*(mxn+1)
	for i in range(1,mxn+1):
		pows[i] = pow(i,p-2,p)
	chooses = [1]*(mxn+1)
	for i in range(1,mxn+1):
		chooses[i] = (chooses[i-1] * (n-(i-1)) *(pows[i]))%p
	dpows = [0]*(mxn+1)
	for i in range(1,mxn+1):
		dpows[i] = pow(d-1,i,p)	
	# def chooseb(l,r):
	# 	return mult(fact[l],pow(mult(fact[r],fact[l-r]),p-2,p))
	for j in range(n-k+1):
		# print(j)
		summ=plus(summ,mult(chooses[j+k],dpows[n-k-j]))
		# summ=mult(summ,)
	return summ
	
def main():
	T = 1
	# T = ri()
	for i in range(T):
		n = ri()
		s = rs()
		# a = ria()
		# n,k = ria()
		# a=ria()

		x = solution(s,n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			print >>output,str(x)# "Case #"+str(i+1)+':',
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()

if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
#example usage:
#    for l in res:
#       print >>output, str(len(l)) + ' ' +  ' '.join(l)

if __name__ == '__main__':
	main()