import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// Press Shift twice to open the Search Everywhere dialog and type show whitespaces,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int L = Integer.parseInt(br.readLine());
String line = br.readLine();
char[] inputAlpha = line.toCharArray();
long M = 1234567891;
long pow =1;
int[] alphabet = new int[27];
for (int i = 1; i <=26 ; i++) {
alphabet[i] = i;
}
long total = 0;
for (int i = 0 ; i < L; i ++)
{
total= (total + (alphabet[inputAlpha[i]-'a'+1])*pow) % M;
pow = pow * 31 % M;
}
System.out.println(total);
}
}
이 문제는 브론즈2치고는 어려웠던 것 같다.. 값이 너무 커지는 것을 방지하기 위해 계산을 다 끝내고 나머지를 구하는 것이 아닌, 중간 중간에 계산을 할때부터 미리 나머지 계산을 해야한다. 또한, 원래는 31제곱 게산을 Math.pow를 이용해서 구했는데 이렇게 구하면 50점짜리로 값이 작을때만 계산이 된다. 그래서 앞서 말한 것처럼 미리미리 %M을 하여 값을 줄였어야 했다. 또한 alphabet[inputAlpha[i]-'a'+1]을 이용해서 문자a를 숫자1로 바꾸는 법을 알았다. 문자 a를 1, 문자 z를 26으로 설정하고 싶으면 이런 방식으로 구현하면 된다.