import java.io.*;
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.StringTokenizer;

public class Main {

    static int N, M;
    static int[] J;
    static int[] K;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        J = sc.nextIntArray(N, -1);
        K = sc.nextIntArray(M);

        System.out.println(solve() ? "YES" : "NO");
    }

    static boolean solve() {
        int len = 0;
        for (int i = 0; i < M; i++) {
            len += K[i];
        }

        int[] cnt = new int[M];
        for (int i = 0; i < len; i++) {
            cnt[J[i]]++;
        }

        if( Arrays.equals(cnt, K) ) return true;

        for (int i = len; i < N; i++) {
            cnt[J[i-len]]--;
            cnt[J[i]]++;

            if( Arrays.equals(cnt, K) ) return true;
        }
        return false;
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        int[] nextIntArray(int n, int delta) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt() + delta;
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    static void writeLines(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int a : as) pw.println(a);
        pw.flush();
    }

    static void writeLines(long[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (long a : as) pw.println(a);
        pw.flush();
    }

    static void writeSingleLine(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int i = 0; i < as.length; i++) {
            if (i != 0) pw.print(" ");
            pw.print(as[i]);
        }
        pw.println();
        pw.flush();
    }

    static int max(int... as) {
        int max = Integer.MIN_VALUE;
        for (int a : as) max = Math.max(a, max);
        return max;
    }

    static int min(int... as) {
        int min = Integer.MAX_VALUE;
        for (int a : as) min = Math.min(a, min);
        return min;
    }

    static void debug(Object... args) {
        StringJoiner j = new StringJoiner(" ");
        for (Object arg : args) {
            if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
            else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
            else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
            else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
            else j.add(arg == null ? "null" : arg.toString());
        }
        System.err.println(j.toString());
    }

    static int lowerBound(int[] array, int value) {
        int low = 0, high = array.length, mid;
        while (low < high) {
            mid = ((high - low) >>> 1) + low;
            if (array[mid] < value) low = mid + 1;
            else high = mid;
        }
        return low;
    }

    static int upperBound(int[] array, int value) {
        int low = 0, high = array.length, mid;
        while (low < high) {
            mid = ((high - low) >>> 1) + low;
            if (array[mid] <= value) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}


import java.io.*;
import static java.lang.Math.*;
import java.util.*;
import java.util.function.*;
import java.lang.*;

public class Main {
    final static boolean debug = false;
    final static String fileName = "";
    final static boolean useFiles = false;

    public static void main(String[] args) throws FileNotFoundException {
        long start;
        if (debug)
            start = System.nanoTime();
        InputStream inputStream;
        OutputStream outputStream;
        if (useFiles) {
            inputStream = new FileInputStream(fileName + ".in");
            outputStream = new FileOutputStream(fileName + ".out");
        } else {
            inputStream = System.in;
            outputStream = System.out;
        }
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task(in, out);
        solver.solve();
        if(debug)
            out.println((System.nanoTime() - start) / 1e+9);
        out.close();
    }
}
class Task {
    public void solve() {
        int[][] a = new int[3][3];
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                a[i][j] = in.nextInt();
        int n = in.nextInt();
        long[][][] dp = new long[3][3][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int from = 0; from < 3; from++) {
                for (int to = 0; to < 3; to++) {
                    if (from == to)
                        continue;
                    int other = 3 - (from + to);
                    dp[from][to][i] = min(dp[from][other][i - 1] + a[from][to] + dp[other][to][i - 1],
                            dp[from][to][i - 1] + a[from][other] + dp[to][from][i - 1] + a[other][to] + dp[from][to][i - 1]);
                }
            }
        }
        out.println(dp[0][2][n]);
    }

    private InputReader in;
    private PrintWriter out;

    Task(InputReader in, PrintWriter out) {
        this.in = in;
        this.out = out;
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public double nextDouble(){
        return Double.parseDouble(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong(){
        return Long.parseLong(next());
    }

    public byte nextByte(){
        return Byte.parseByte(next());
    }
}


import java.util.*;
import java.io.*;
public class c
{
	public static void main(String[] arg) throws IOException
	{
		new c();
	}
	public c() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = in.nextInt();
		out.println(Math.max(1, 4*(int)(Math.sqrt(2)*n)));
		in.close(); out.close();
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
	}
}


public class H {
	public static void main(String[] args) {
		int n = new java.util.Scanner(System.in).nextInt();
		for (int i = 1; i < n; ++i) {
			for (int j = 1; j < n; ++j) {
				System.out.print(Integer.toString(i * j, n) + " ");
			}
			System.out.println();
		}
	}
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MonthsYears {

	public static void main(String[] args){
		List<Integer> year = new ArrayList<>();
		year.add(31);
		year.add(28);
		year.add(31);
		year.add(30);
		year.add(31);
		year.add(30);
		year.add(31);
		year.add(31);
		year.add(30);
		year.add(31);
		year.add(30);
		year.add(31);

		List<Integer> values = new ArrayList<>();
		boolean containsLeap = false;
		boolean done = false;
		boolean match = true;
		Scanner sc = new Scanner(System.in);
		int sum =0;
		int n  = sc.nextInt();
		for(int counter =0;counter<n;counter++){
			int x = sc.nextInt();
			if(x==29 && containsLeap){
				System.out.println("NO");
				done = true;
				break;
			}else if(x==29 && !containsLeap){
				sum = sum+28;
				containsLeap = true;
				values.add(28);
			}else{
				sum = sum+x;
				values.add(x);
			}
		}

		int size = year.size();
		if(!done){
			for(int i=0;i<size;i++){
				int secondSum = year.get(i);

				for(int counter =1;counter<n;counter++){
					int indexMe = (i+counter)%size;
					secondSum = secondSum + year.get(indexMe);

				}

				if(sum==secondSum){
					match =true;
					for(int count =0;count<values.size();count++){
						int indexSum = (i+count)%12;

						if(year.get(indexSum)!=values.get(count)){
							match =false;
							break;
						}
					}

					if(match){
						done = true;
						break;
					}

					//done =true;
					//break;
				}

			}
			if(done){
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}
		}

	}

}


import java.io.*;
import java.util.*;
public class TwentyFive {
	public static void main(String[] args) throws Exception {
		FastIO in = new FastIO();
		String s = in.next();
		int ans = 0;
		for (int i=0; i<100000000; i+=25) {
			String test = i+"";
			if (test.length()!=s.length()) continue;
			int xValue = -1;
			boolean works = true;
			for (int j=0; j<s.length(); j++) {
				if (s.charAt(j)=='X') {
					if (xValue==-1) {
						xValue = test.charAt(j);
					}
					if (test.charAt(j)!=xValue) {
						works = false;
						break;
					}
				}
				else if (s.charAt(j)!='_') {
					if (test.charAt(j)!=s.charAt(j)) {
						works = false;
						break;
					}
				}
			}
			if (works) ans++;
		}
		System.out.println(ans);
	}
	static class FastIO {
		BufferedReader br;
	    StringTokenizer st;
	    public FastIO() throws IOException
	    {
	        br = new BufferedReader(
	            new InputStreamReader(System.in));

	    }

	    public String next() throws IOException
	    {
	        while (st == null || !st.hasMoreElements()) {

	            st = new StringTokenizer(br.readLine());
	        }
	        return st.nextToken();
	    }

	    public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); }

	    public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); }

	    public double nextDouble() throws NumberFormatException, IOException
	    {
	        return Double.parseDouble(next());
	    }

	    public String nextLine() throws IOException
	    {
	        String str = br.readLine();
	        return str;
	    }
	}
}


import java.util.Scanner;

public class Main {
	public static int N;
	public static int[] array;

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		N = s.nextInt();
		array = new int[N];
		int min = 1 << 29;
		int count = 0;
		for (int i = 0; i < N; i++) {
			array[i] = s.nextInt();
			min = Math.min(min, array[i]);
		}
		for (int i : array) {
			if (i == min) {
				count++;
			}
		}
		if (count <= N / 2) {
			System.out.println("Alice");
		} else {
			System.out.println("Bob");
		}
	}
}


//1184A1
import java.util.*;
public class heidiA1{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long r=sc.nextLong();
    if(r%2==0||r<=3)
    System.out.println("NO");
    else{
      System.out.println("1"+" "+((r-3)/2));
    }
  }
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.Vector;

public class Main {

	static Vector<Vector<State> > v = new Vector<Vector<State> >();
	public static void main(String[] args) throws IOException {
		v.setSize(12);

		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(cin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int m = Integer.parseInt(tok.nextToken());
		int k = Integer.parseInt(tok.nextToken());
		for (int i = 0; i < n; i++) {
			v.set(i, new Vector<State>());
			cin.readLine();
			for (int j = 0; j < m; j++) {
				State s = new State();
				tok = new StringTokenizer(cin.readLine());
				s.a = Integer.parseInt(tok.nextToken());
				s.b = Integer.parseInt(tok.nextToken());
				s.c = Integer.parseInt(tok.nextToken());
				v.get(i).add(s);
			}
		}

		int mx=0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if(i == j) continue;
				int val = 0;
				Vector<Point> pro = new Vector<Point>();
				for (int kk = 0; kk < v.get(i).size(); kk++) {
					if(v.get(j).get(kk).b <= v.get(i).get(kk).a) continue;
					pro.add(new Point(v.get(j).get(kk).b - v.get(i).get(kk).a, v.get(i).get(kk).c));
				}
				int lim = k;
				Collections.sort(pro, new cmp());
				for (int kk = 0; kk < pro.size(); kk++) {
					if(pro.get(kk).y <= lim){
						lim -= pro.get(kk).y;
						val += pro.get(kk).x*pro.get(kk).y;
					}
					else{
						val += pro.get(kk).x*lim;
						break;
					}
				}
				mx = Math.max(mx,val);
			}
		}
		System.out.println(mx);
	}

}
class State{
	public int a,b,c;

}

class cmp implements Comparator<Point>{

	@Override
	public int compare(Point o1, Point o2) {
		if(o1.x > o2.x) return -1;
		if(o1.x < o2.x) return 1;
		if(o1.y > o2.y) return -1;
		if(o1.y < o2.y) return 1;
		return 0;
	}

}


import java.io.*;
import java.util.*;

public class G {

	static final int P = 1_000_000_007;

	void submit() {
		int n = nextInt();
		int m = nextInt();
		long exp = nextLong();
		int[] a = new int[n];
		int[] b = new int[m];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		int maxCoef = 0;
		for (int i = 0; i < m; i++) {
			b[i] = nextInt();
			maxCoef = Math.max(maxCoef, b[i]);
		}

		int[] bPoly = new int[maxCoef + 1];
		bPoly[maxCoef] = 1;
		for (int pow : b) {
			bPoly[maxCoef - pow]--;
			if (bPoly[maxCoef - pow] < 0) {
				bPoly[maxCoef - pow] += P;
			}
		}

		int[] aPoly = {};
		for (int pow : a) {
			aPoly = sum(aPoly, quotPow(new int[] { 0, 1 }, bPoly, pow));
		}
		int[] ans = quotPow(aPoly, bPoly, exp);

		ans = prod(ans, quotPow(new int[] { 0, 1 }, bPoly, maxCoef));
		ans = quot(ans, bPoly);

		out.println((long)ans[0] * inv(P - bPoly[0]) % P);
	}

	int inv(int x) {
		return x == 1 ? 1 : P - (int)((long)(P / x) * inv(P % x) % P);
	}

	int[] quotPow(int[] a, int[] b, long n) {
		a = quot(a, b);
		int[] ret = { 1 };
		for (; n > 0; n >>= 1) {
			if ((n & 1) == 1) {
				ret = quot(prod(ret, a), b);
			}
			a = quot(prod(a, a), b);
		}
		return ret;
	}

	int[] sum(int[] a, int[] b) {
		int[] c = Arrays.copyOf(a, Math.max(a.length, b.length));
		for (int i = 0; i < b.length; i++) {
			c[i] += b[i];
			if (c[i] >= P) {
				c[i] -= P;
			}
		}
		return c;
	}

	int[] prod(int[] a, int[] b) {
		int[] c = new int[a.length + b.length - 1];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < b.length; j++) {
				c[i + j] += (int) ((long) a[i] * b[j] % P);
				if (c[i + j] >= P) {
					c[i + j] -= P;
				}
			}
		}
		return c;
	}

	int[] quot(int[] a, int[] b) {
		a = a.clone();
		// leading coef of b is 1
		if (a.length < b.length) {
			return a;
		}
		int degB = b.length - 1;
		for (int ptr = a.length - 1; ptr >= degB; ptr--) {
			long coef = a[ptr];
			for (int i = 0, j = ptr - degB; i < degB; i++, j++) {
				a[j] -= (int) (coef * b[i] % P);
				if (a[j] < 0) {
					a[j] += P;
				}
			}
		}
		return Arrays.copyOf(a, degB);
	}

	void preCalc() {

	}

	void stress() {

	}

	void test() {

	}

	G() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

	public static void main(String[] args) throws IOException {
		new G();
	}

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}
}


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Codeforces911C {

    public static void main(String[] args) {
        Integer[] array = new Integer[3];
        boolean possible = false;
        Scanner scanner = new Scanner(System.in);
        array[0] = scanner.nextInt();
        array[1] = scanner.nextInt();
        array[2] = scanner.nextInt();
        List<Integer> list = Arrays.asList(array);
        Collections.sort(list);
        array = (Integer[]) list.toArray();
        int sum = array[0] + array[1] + array[2];
        if (sum > 7) {

            if (array[0] == 1) {
                possible = true;
            } else if (array[1] <= 2) {
                possible = true;
            } else if (array[1] == 3) {
                if (array[0] == 3 && array[2] == 3) {
                    possible = true;
                } else {
                    possible = false;
                }
            } else if (array[1] == 4) {
                if (array[2] == 4 && array[0] == 2) {
                    possible = true;
                } else {
                    possible = false;
                }
            }

        } else {
            possible = true;
        }
        if(possible){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
//        int gcd = BigInteger.valueOf(array[0]).gcd(BigInteger.valueOf(array[1])).gcd(BigInteger.valueOf(array[2])).intValue();
//        gcd = array[0]*array[1]*array[2]/gcd;

    }
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Main implements Runnable {

    static String filename = "";

    int[] factorize(int n) {
        ArrayList<Integer> ans = new ArrayList<Integer>();
        for (int i = 2; i <= n; i++) {
            while (n % i == 0) {
                ans.add(i);
                n /= i;
            }
        }
        int[] ansa = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            ansa[i] = ans.get(i);
        }
        return ansa;
    }

    int gcd(int a, int b) {
        while (b != 0) {
            a %= b;
            int temp = a;
            a = b;
            b = temp;
        }
        return a;
    }

    int digitsum(int n, int b) {
        int ans = 0;
        while (n > 0) {
            ans += n % b;
            n /= b;
        }
        return ans;
    }

    int get3(int b) {
        Random rnd = new Random();
        int ans = 0;
        for (int i = 0; i < 10000; i++) {
            int num = rnd.nextInt(1000000000);
            ans = gcd(ans, num - digitsum(num, b));
        }
        return ans;
    }

    int is2type(int b, int d) {
        int[] bfactors = factorize(b);
        int[] dfactors = factorize(d);

        boolean type2 = true;
        TreeMap<Integer, Integer> bf = new TreeMap<Integer, Integer>();
        for (int i : bfactors) {
            if (!bf.containsKey(i)) {
                bf.put(i, 0);
            }
            bf.put(i, bf.get(i) + 1);
        }
        TreeMap<Integer, Integer> df = new TreeMap<Integer, Integer>();
        for (int i : dfactors) {
            if (!df.containsKey(i)) {
                df.put(i, 0);
            }
            df.put(i, df.get(i) + 1);
        }
        for (int i : dfactors) {
            if (!bf.containsKey(i)) {
                type2 = false;
            }
        }

        if (type2) {
            int ans = 0;
            long pow = 1;
            while (pow % d != 0) {
                pow *= b;
                ans++;
                /*
                for (Map.Entry<Integer, Integer> e : df.entrySet()) {
                    System.out.println(e.getKey() + " " + e.getValue());
                }
                System.out.println();
                */
            }
            return ans;
        } else {
            return -1;
        }
    }

    boolean is3type(int b, int d) {
        return (b - 1) % d == 0;
    }

    boolean is11type(int b, int d) {
        return (b + 1) % d == 0;
    }

    HashMap<Integer, Boolean> type6 = new HashMap<Integer, Boolean>();

    boolean is6type(int b, int d) {
        if (type6.containsKey(d)) {
            return type6.get(d);
        }
        for (int i = 2; i < d; i++) {
            if (d % i == 0) {
                int di = d / i;
                if (i % di == 0 || di % i == 0) {
                    continue;
                }
                boolean[] m1 = getMask(b, i);
                boolean[] m2 = getMask(b, di);
                for (int ii = 0; ii < 4; ii++) {
                    for (int jj = 0; jj < 4; jj++) {
                        if (ii == jj) {
                            continue;
                        }
                        if (m1[ii] && m2[jj]) {
                            type6.put(d, true);
                            return true;
                        }
                    }
                }
                /*
                if (is2type(b, i) != -1 || is3type(b, i) || is11type(b, i) || is6type(b, i)) {
                    if (is2type(b, di) != -1 || is3type(b, di) || is11type(b, di) || is6type(b, di)) {
                        if (is3type(b, i) && is3type(b, di))
                            continue;
                        type6.put(d, true);
                        return true;
                    }
                }
                */
            }
        }
        type6.put(d, false);
        return false;
    }

    boolean[] getMask(int b, int d) {
        boolean[] ans = new boolean[4];
        ans[0] = is2type(b, d) != -1;
        ans[1] = is3type(b, d);
        ans[2] = is11type(b, d);
        ans[3] = is6type(b, d);
        return ans;
    }

    public void solve() throws Exception {
        int b = sc.nextInt(), d = sc.nextInt();
        int ans;
        if ((ans = is2type(b, d)) != -1) {
            out.println("2-type");
            out.println(ans);
        } else if (is3type(b, d)) {
            out.println("3-type");
        } else if (is11type(b, d)) {
            out.println("11-type");
        } else if (is6type(b, d)) {
            out.println("6-type");
        } else {
            out.println("7-type");
        }
    }

    BufferedReader in;
    PrintWriter out;
    FastScanner sc;

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 25).start();
    }

    @Override
    public void run() {
        try {
            init();
            solve();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }

    void init() throws Exception {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        sc = new FastScanner(in);
    }
}

class FastScanner {

    BufferedReader reader;
    StringTokenizer strTok;

    public FastScanner(BufferedReader reader) {
        this.reader = reader;
    }

    public String nextToken() throws IOException {
        while (strTok == null || !strTok.hasMoreTokens()) {
            strTok = new StringTokenizer(reader.readLine());
        }

        return strTok.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public BigInteger nextBigInteger() throws IOException {
        return new BigInteger(nextToken());
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @author Don Li
 */
public class ArtemArray {
    void solve() {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();

        long ans = 0;
        int[] b = new int[n];
        int m = 0;
        for (int i = 0; i < n; i++) {
            while (m > 1 && b[m - 1] <= Math.min(b[m - 2], a[i])) {
                ans += Math.min(b[m - 2], a[i]);
                m--;
            }
            b[m++] = a[i];
        }

        for (int i = 1; i < m - 1; i++) ans += Math.min(b[i - 1], b[i + 1]);

        out.println(ans);
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new ArtemArray().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskE {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n=in.readInt(), sx=in.readInt()%n, sy=in.readInt()%n, dx=in.readInt()%n, dy=in.readInt()%n;
        long t=in.readLong(), Sx=(2L*sx+dx+sy)%n, Sy=(2L*sy+dy+sx)%n, Dx=(dx+sx+sy)%n, Dy=(dy+sy+sx)%n, t0=(t%2==0?t/2:t)%n, t1=(t%2==1?(t-1)/2:t-1)%n;
        Matrix.mod=n;
        Matrix base=new Matrix(new long[][] {
            {2, 1, 1, 1, 0, 0},
            {1, 2, 1, 1, 0, 0},
            {1, 0, 1, 0, 0, 0},
            {0, 1, 0, 1, 0, 0},
            {1, 1, 1, 1, 1, 0},
            {0, 0, 0, 0, 1, 1}
        }), matrix=new Matrix(new long[][]{
            {Sx, Sy, Dx, Dy, 1, 1},
            {sx, sy, dx, dy, 0, 1}
        });
        long[][] data=Matrix.multiply(matrix, base.power(t)).data;
        Sx=(data[1][0]+n)%n;
        Sy=(data[1][1]+n)%n;
        //out.printLine(data[1][0], data[1][1]);
        out.printLine(Sx==0?n:Sx, Sy==0?n:Sy);
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int readInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

}

class Matrix {
    public static long mod = Long.MAX_VALUE;
    public final long[][] data;
    public final int rowCount;
    public final int columnCount;

    public Matrix(int rowCount, int columnCount) {
        this.rowCount = rowCount;
        this.columnCount = columnCount;
        this.data = new long[rowCount][columnCount];
    }

    public Matrix(long[][] data) {
        this.rowCount = data.length;
        this.columnCount = data[0].length;
        this.data = data;
    }

    public static Matrix multiply(Matrix first, Matrix second) {
        Matrix result = new Matrix(first.rowCount, second.columnCount);
        for (int i = 0; i < first.rowCount; i++) {
            for (int j = 0; j < second.rowCount; j++) {
                for (int k = 0; k < second.columnCount; k++)
                    result.data[i][k] = (result.data[i][k] + first.data[i][j] * second.data[j][k]) % mod;
            }
        }
        return result;
    }

    public static Matrix identityMatrix(int size) {
        Matrix result = new Matrix(size, size);
        for (int i = 0; i < size; i++)
            result.data[i][i] = 1;
        return result;
    }

    public Matrix power(long exponent) {
        if (exponent == 0)
            return identityMatrix(rowCount);
        if (exponent == 1)
            return this;
        Matrix result = power(exponent >> 1);
        result = multiply(result, result);
        if ((exponent & 1) == 1)
            result = multiply(result, this);
        return result;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Dot {

    static int N, D;
    static int[] vx, vy;
    static Boolean[][][][] dp;

    static long sq(long x) {
	return x * x;
    }

    static boolean solve(int p1, int p2, int x, int y) {
	if (sq(x) + sq(y) > sq(D))
	    return true;
	if (dp[p1][p2][x + 200][y + 200] != null)
	    return dp[p1][p2][x + 200][y + 200];
	boolean ans = false;
	if (p1 == 0)
	    ans |= !solve(p2, 1, y, x);
	for (int i = 0; i < N; i++)
	    ans |= !solve(p2, p1, x + vx[i], y + vy[i]);
	return dp[p1][p2][x + 200][y + 200] = ans;
    }

    public static void main(String[] args) throws IOException {
	MyScanner sc = new MyScanner(System.in);
	PrintWriter out = new PrintWriter(System.out);
	int x = sc.nextInt(), y = sc.nextInt();
	N = sc.nextInt();
	D = sc.nextInt();
	vx = new int[N];
	vy = new int[N];
	for (int i = 0; i < N; i++) {
	    vx[i] = sc.nextInt();
	    vy[i] = sc.nextInt();
	}
	dp = new Boolean[2][2][402][402];
	out.println(solve(0, 0, x, y) ? "Anton" : "Dasha");
	out.flush();
	out.close();
    }

    static class MyScanner {

	BufferedReader br;
	StringTokenizer st;

	MyScanner(InputStream is) {
	    br = new BufferedReader(new InputStreamReader(is));
	}

	String next() throws IOException {
	    while (st == null || !st.hasMoreTokens())
		st = new StringTokenizer(br.readLine());
	    return st.nextToken();
	}

	int nextInt() throws IOException {
	    return Integer.parseInt(next());
	}

	long nextLong() throws IOException {
	    return Long.parseLong(next());
	}

	boolean ready() throws IOException {
	    return br.ready();
	}
    }
}


import java.io.*;
import java.util.*;

public class C_submit {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static long[][] c = new long[61][];
	static {
		for (int i = 0; i < c.length; i++) {
			c[i] = new long[i + 1];
			c[i][0] = c[i][i] = 1;
			for (int j = 1; j < i; j++)
				c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
		}
	}

	long c(int n, int k) {
		return n < 0 || k < 0 || k > n ? 0 : c[n][k];
	}

	void solve() throws IOException {
		long n = nextLong();
		long t = nextLong();
		if (Long.bitCount(t) != 1) {
			out.println(0);
			return;
		}

		int need = Long.numberOfTrailingZeros(t) + 1;

		// return numbers of x : 2 <= x <= n + 1, bitcount(x) = need

		n += 2;

		String s = Long.toBinaryString(n);

		int prefBitCnt = 0;

		long ans = 0;

		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '1') {
				int sufLen = s.length() - i - 1;
				ans += c(sufLen, need - prefBitCnt);
				prefBitCnt++;
			}
		}

		if (need == 1)
			ans--;

		out.println(ans);
	}

	C_submit() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new C_submit();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}


import java.util.Scanner;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author @zhendeaini6001
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Scanner in = new Scanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
     public final long MOD = 1000000007;
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        String a = in.next();
        String b = in.next();
        long[][] dp = new long[2][5001];
        ArrayUtils.fill(dp, 0);
        int flag = 0;
        long res = 0;
        for (int i = 0; i < a.length(); ++i){
            Arrays.fill(dp[i&1], 0);
            for (int j = 0; j < b.length(); ++j){
                if (j >= 1){
                    dp[i&1][j] = dp[i&1][j - 1];
                }
                if (a.charAt(i) == b.charAt(j)){
                    ++dp[i&1][j];
                    if (i >= 1 && j >= 1){
                        dp[i&1][j] += dp[(i-1)&1][j-1];
                    }
                    dp[i&1][j] %= MOD;
                }
            }
            res += dp[i&1][b.length() -  1];       res %= MOD;
        }
        out.println(res);
        return;
    }

}

class ArrayUtils {

    public static void fill(long[][] array, long value) {
        for (long[] row : array)
            Arrays.fill(row, value);
    }

    }


//package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(input);

        String s = in.readLine();
        String ss[] = s.split(" ");

        int n = Integer.parseInt(ss[0]);
        int k = Integer.parseInt(ss[1]);

        String number = in.readLine();
        if (k == 0)
        {
            System.out.println(number);
        }
        else if(n == 1)
        {
            System.out.println("0");
        }
        else
        {
            StringBuffer tmp = new StringBuffer();
            tmp.append("1");

            if(number.charAt(0) != '1')
                k--;

            int counter = 1;
            while(k > 0 && counter < n)
            {
                if(number.charAt(counter) != '0')
                {
                     k--;
                }
                counter++;
                tmp.append("0");
            }

            if(counter < n)
                tmp.append(number.substring(counter));

            System.out.println(tmp.toString());
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.StringTokenizer;

public class DimaAndHorses2 {
    public static void main(String[] args) {
        InputReader r = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int n = r.nextInt(), m = r.nextInt();
        ArrayList<Integer>[] adj = new ArrayList[n];
        for (int i = 0; i < adj.length; i++) {
            adj[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < m; i++) {
            int from = r.nextInt() - 1;
            int to = r.nextInt() - 1;
            adj[from].add(to);
            adj[to].add(from);
        }
        int[] res = new int[n];
        Queue<Integer> q = new LinkedList<Integer>();
        for (int i = 0; i < n; i++)
            q.add(i);
        while (!q.isEmpty()) {
            int f = q.remove();
            int enemies = 0;
            for (int x : adj[f])
                if (res[x] == res[f])
                    enemies++;
            if (enemies < 2)
                continue;
            res[f] = 1 - res[f];
            for (int x : adj[f])
                q.add(x);
            q.add(f);
        }
        for (int x : res)
            out.print(x);
        out.close();
        return;
    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}


import java.io.*;
import java.util.*;

public class B {

    long n, c0, c1;

    public void solve() {
        n = in.nextInt() - 1;
        c0 = in.nextInt();
        c1 = in.nextInt();
        if (c0 > c1) {
            long tmp = c0;
            c0 = c1;
            c1 = tmp;
        }

        if (c0 == 0) {
            out.println(1L * n * (c0 + c1));
            return;
        }

        long left = 0, right = (long) 5e17;

        while (left < right - 1) {
            long mid = (left + right) >>> 1;

            long[] calc = calc(mid);
            if (calc[0] < n) {
                left = mid;
            } else {
                right = mid;
            }
        }
        System.err.println(left);
        long[] calc2 = calc(left);

        long result = calc2[1] + (n - calc2[0]) * (left + 1);
        result += 1L * n * (c0 + c1);
        out.println(result);
    }

    long[] calc(long x) {
        long count = 0, sum = 0;
        for (long j = 0; c1 * j <= x; j++) {
            long maxI = (x - c1 * j) / c0;
            if (j == 0) {
                count += (maxI + 1);
                sum += c0 * maxI * (maxI + 1) / 2;
            } else {
                long curChoose = 1;
                for (long i = 0; i * c0 + j * c1 <= x; i++) {
                    if (i > 0) {
                        curChoose *= (i + j);
                        curChoose /= i;
                    }
                    count += curChoose;
                    sum += curChoose * (i * c0 + j * c1);
                    if (count >= n) {
                        return new long[]{count, sum};
                    }
                }
            }
        }
        return new long[]{count, sum};
    }

    public void run() {
        in = new FastScanner();
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }

    FastScanner in;
    PrintWriter out;

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String fileName) {
            try {
                br = new BufferedReader(new FileReader(fileName));
            } catch (FileNotFoundException e) {
            }
        }

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }

    public static void main(String[] args) {
        new B().run();
    }
}


import java.io.*;
import java.util.*;

public class Codeforces
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]);
        int i; double p=Double.parseDouble(s[1]);

        for(i=0;i<=n;i++)
        if(probab(n,i)>=p) break;
        System.out.println(i);
    }

    static int ncr(int n,int r)
    {
        if(r>n) return 0;
        int ans=1,i;
        for(i=n;i>n-r;i--) ans*=i;
        for(i=2;i<=r;i++) ans/=i;
        return ans;
    }

    static double probab(int n,int x)
    {
        int a=ncr(x,3)*ncr(n-x,0);
        int b=ncr(x,2)*ncr(n-x,1);
        int c=ncr(x,1)*ncr(n-x,2);
        return 1.0*((3.0/6)*c+a+b)/ncr(n,3);
    }
}


import java.io.*;
import java.util.Locale;
import java.util.Scanner;

/**
 * Author: Sergey Paramonov
 * Date: 27.07.14
 * Time: 19:47
 */
public class MemSQL_20140727_C {

    Scanner scanner = new Scanner(System.in);
    BufferedReader buffered = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer input = new StreamTokenizer(buffered);

    static PrintWriter output = new PrintWriter(new BufferedOutputStream(System.out));

    int nextInt() throws Exception {
        input.nextToken();
        return (int) input.nval;
    }

    static void flushAndClose() {
        output.flush();
        System.out.flush();
        output.close();
    }

    void main() throws Exception {
        double n = nextInt();
        double m = nextInt();
        double ans = 1.0/n;
        if (m > 1) {
            ans += (1.0 - 1.0/n) * (m-1.0)/(n*m - 1.0);
        }
        System.out.printf(Locale.ENGLISH, "%.10f", ans);
    }

    public static void main(String[] args) throws Exception {
        new MemSQL_20140727_C().main();
        flushAndClose();
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author dmytro.prytula
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DTriFiguri solver = new DTriFiguri();
        solver.solve(1, in, out);
        out.close();
    }

    static class DTriFiguri {
        static int N;
        int M = 305;
        int INF = (int) 1e9;
        DTriFiguri.Point[] possibleKnight = new DTriFiguri.Point[]{
                DTriFiguri.Point.of(2, 1),
                DTriFiguri.Point.of(-2, 1),
                DTriFiguri.Point.of(2, -1),
                DTriFiguri.Point.of(-2, -1),
                DTriFiguri.Point.of(1, 2),
                DTriFiguri.Point.of(-1, 2),
                DTriFiguri.Point.of(1, -2),
                DTriFiguri.Point.of(-1, -2)
        };
        DTriFiguri.Point[][] dist = new DTriFiguri.Point[M][M];

        {

        }

        private boolean in(DTriFiguri.Point p) {
            return 0 <= p.x && p.x < N && 0 <= p.y && p.y < N;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {

            N = in.nextInt();
            DTriFiguri.Point[] pos = new DTriFiguri.Point[N * N];
            int[][] A = new int[N][N];
            for (int x = 0; x < N; x++) {
                for (int y = 0; y < N; y++) {
                    A[x][y] = in.nextInt() - 1;
                    pos[A[x][y]] = DTriFiguri.Point.of(x, y);

                }
            }

            for (int i = 0; i < M; i++) {
                for (int j = 0; j < M; j++) {
//                Arrays.fill(dist[i], Point.of(INF, INF));
                    dist[i][j] = DTriFiguri.Point.of(INF, INF);
                }
            }
            for (int i = 0; i < M; i++)
//            for (int j = 0; j < M; j++)
//                if (i == j)
                dist[i][i] = DTriFiguri.Point.of(0, 0);

            for (int x = 0; x < N; x++) {
                for (int y = 0; y < N; y++) {
                    //knight
                    for (int i = 0; i < possibleKnight.length; i++) {
                        int nx = x + possibleKnight[i].x;
                        int ny = y + possibleKnight[i].y;
                        if (in(DTriFiguri.Point.of(nx, ny))) {
                            dist[DTriFiguri.Point.hash(x, y, 0)][DTriFiguri.Point.hash(nx, ny, 0)] = DTriFiguri.Point.of(1, 0);
                        }
                    }
                    //bishop
                    for (int i = -N + 1; i <= N - 1; i++) {
                        int nx = x + i;
                        int ny = y + i;
                        if (in(DTriFiguri.Point.of(nx, ny)))
                            dist[DTriFiguri.Point.hash(x, y, 1)][DTriFiguri.Point.hash(nx, ny, 1)] = DTriFiguri.Point.of(1, 0);

                        ny = y - i;
                        if (in(DTriFiguri.Point.of(nx, ny)))
                            dist[DTriFiguri.Point.hash(x, y, 1)][DTriFiguri.Point.hash(nx, ny, 1)] = DTriFiguri.Point.of(1, 0);
                    }

                    //rook
                    for (int i = 0; i < N; i++) {
                        int nx = i;
                        int ny = y;
//                    if (in(Point.of(nx, ny)))
                        dist[DTriFiguri.Point.hash(x, y, 2)][DTriFiguri.Point.hash(nx, ny, 2)] = DTriFiguri.Point.of(1, 0);
                        nx = x;
                        ny = i;
//                    if (in(Point.of(nx, ny)))
                        dist[DTriFiguri.Point.hash(x, y, 2)][DTriFiguri.Point.hash(nx, ny, 2)] = DTriFiguri.Point.of(1, 0);

                    }

                    for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 3; j++) {
                            if (i != j) {
                                dist[DTriFiguri.Point.hash(x, y, i)][DTriFiguri.Point.hash(x, y, j)] = DTriFiguri.Point.of(1, 1);
                            }
                        }
                    }

                }
            }

            for (int k = 0; k < M; k++) {
                for (int i = 0; i < M; i++) {
                    for (int j = 0; j < M; j++) {
                        dist[i][j] = DTriFiguri.Point.min(dist[i][j], DTriFiguri.Point.sum(dist[i][k], dist[k][j]));
                    }
                }
            }

            DTriFiguri.Point[][] dp = new DTriFiguri.Point[N * N][3];
            for (int i = 0; i < N * N; i++) {
                for (int j = 0; j < 3; j++) {
                    dp[i][j] = DTriFiguri.Point.of(INF, INF);
                }
            }

            dp[0][0] = dp[0][1] = dp[0][2] = DTriFiguri.Point.of(0, 0);

            for (int i = 0; i < N * N - 1; i++)
                for (int j = 0; j < 3; j++)
                    for (int k = 0; k < 3; k++)
                        dp[i + 1][k] = DTriFiguri.Point.min(
                                dp[i + 1][k],
                                DTriFiguri.Point.sum(dp[i][j], dist[DTriFiguri.Point.hash(pos[i].x, pos[i].y, j)][DTriFiguri.Point.hash(pos[i + 1].x, pos[i + 1].y, k)])
                        );

            DTriFiguri.Point ans = DTriFiguri.Point.of(INF, INF);
            ans = DTriFiguri.Point.min(ans, dp[N * N - 1][0]);
            ans = DTriFiguri.Point.min(ans, dp[N * N - 1][1]);
            ans = DTriFiguri.Point.min(ans, dp[N * N - 1][2]);

            out.print(ans.toString());
        }

        private static class Point {
            int x;
            int y;

            private Point(int x, int y) {
                this.x = x;
                this.y = y;
            }

            public static DTriFiguri.Point of(int x, int y) {
                return new DTriFiguri.Point(x, y);
            }

            public static int hash(int x, int y, int p) {
                return x * N * 3 + y * 3 + p;
            }

            public static DTriFiguri.Point min(DTriFiguri.Point a, DTriFiguri.Point b) {
                if (a.x == b.x) {
                    return a.y < b.y ? a : b;
                }
                return a.x < b.x ? a : b;
            }

            public static DTriFiguri.Point sum(DTriFiguri.Point point1, DTriFiguri.Point point) {
                return DTriFiguri.Point.of(point1.x + point.x, point1.y + point.y);
            }

            public String toString() {
                return x + " " + y;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String nextToken() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

    }
}


// practice with rainboy
import java.io.*;
import java.util.*;

public class CF513C extends PrintWriter {
	CF513C() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF513C o = new CF513C(); o.main(); o.flush();
	}

	double prob(int l, int r, int a) {
		return Math.max(r - Math.max(l, a) + 1, 0) / (r - l + 1.0);
	}
	void main() {
		int n = sc.nextInt();
		int[] ll = new int[n];
		int[] rr = new int[n];
		for (int i = 0; i < n; i++) {
			ll[i] = sc.nextInt();
			rr[i] = sc.nextInt();
		}
		double ans = 0;
		for (int a = 1; a <= 10000; a++) {
			double p = 1;
			for (int j = 0; j < n; j++)
				p *= 1 - prob(ll[j], rr[j], a);
			double q = p;
			for (int i = 0; i < n; i++) {
				p = prob(ll[i], rr[i], a);
				for (int j = 0; j < n; j++)
					if (j != i)
						p *= 1 - prob(ll[j], rr[j], a);
				q += p;
			}
			ans += 1 - q;
		}
		printf("%.9f\n", ans);
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskD {
		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			long n = in.nextLong();
			long inputL = in.nextLong() - 1;
			long inputR = in.nextLong() - 1;
			long k = in.nextLong();
			long d = dist(inputL, inputR, n);

			final long BIG = (long) 3e5;
			long ans = -1;
			if (n >= BIG) {
//		if (true) {
//		if (false) {
				// c full circles.
				for (long c = 0; n * c <= k; c++) {
					long l = -1;
					long r = n + 1;
					while (r - l > 1) {
						long m = l + (r - l) / 2;
						if (canLB(n, k, d, c, m)) {
							l = m;
						} else {
							r = m;
						}
					}
					if (l >= 0 && canUB(n, k, d, c, l)) {
						ans = Math.max(ans, l);
					}
				}
			} else {
				// s sweet lovers.
				for (long s = 0; s <= n; s++) {
					long minC = 0;
					long maxC = 0;
					{
						long b = Math.min(d, n - s);
						long a = d - b;
						long p = k - a - d;
						if (a > 0) {
							++p;
						}
						if (p < 0) {
							continue;
						}
						maxC = p / (n + s);
					}
					{
						long a = Math.min(d, s);
						long b = d - a;
						long p = k - a - d;
						p += n + s - 1;
						if (p < 0) {
							continue;
						}
						minC = p / (n + s);
					}
					if (minC <= maxC) {
						ans = Math.max(ans, s);
					}
				}
			}
			out.println(ans);
		}

		private boolean canLB(long n, long k, long d, long c, long s) {
			long minK = -1;
			long b = Math.min(d, n - s);
			long a = d - b;
			minK = (n + s) * c + d + a;
			if (a > 0 && minK > 0) {
				// Last "a" takes one.
				--minK;
			}
			return k >= minK;
		}

		private boolean canUB(long n, long k, long d, long c, long s) {
			long maxK = -1;
			long a = Math.min(d, s);
			long b = d - a;
			maxK = (n + s) * c + d + a;
			return k <= maxK;
		}

		private long dist(long l, long r, long n) {
			while (r < l) {
				r += n;
			}
			return r - l + 1;
		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

	}
}


import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskE solver = new TaskE();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskE {
        final int MAX_N = 101_000;
        @SuppressWarnings("uncheked")
        ArrayList<Integer>[] divs = new ArrayList[MAX_N];
        long modulo;

        public void solve(int testNumber, FastScanner in, PrintWriter out) {
                init();
                int n = in.nextInt();
                int m = in.nextInt();
                long L = in.nextInt();
                long R = in.nextInt();
                modulo = in.nextInt();
                long res = 0;
                for (long dx = 1; dx <= n; dx++) {
                        long low = L * L - dx * dx;
                        long high = R * R - dx * dx;
                        if (low < 0)
                                low = 0;
                        if (high < 0)
                                continue;
                        long left = (int)Math.sqrt(low);
                        long right = (int)Math.sqrt(high);
                        while (left > 0 && left * left + dx * dx >= L * L)
                                left--;
                        while (left * left + dx * dx < L * L)
                                left++;
                        left--;
                        while (right > 0 && right * right + dx * dx >= R * R)
                                right--;
                        while (right * right + dx * dx <= R * R)
                                right++;
                        right--;
                        if (left < 0)
                                left = 0;
                        if (right > m)
                                right = m;
                        if (left >= right)
                                continue;
                        long cntR = right - getCount((int)dx, (int)right);
                        long cntL = left - getCount((int)dx, (int)left);
                        long cnt = cntR - cntL;
                        cnt %= modulo;
                        cnt += modulo;
                        cnt %= modulo;
                        long sumR = right * (right + 1) / 2 - getSum((int)dx, (int)right);
                        long sumL = left * (left + 1) / 2 - getSum((int)dx, (int)left);
                        long sum = sumR - sumL;
                        sum = cnt * (m + 1) % modulo - sum;
                        sum %= modulo;
                        sum += modulo;
                        sum %= modulo;
                        res += (2 * sum * (n - dx + 1)) % modulo;
                        res %= modulo;
                }
                if (L <= 1)
                        res += (n + 1) * 1L * m + n * 1L * (m + 1);
                out.println(res % modulo);
        }

        void init() {
                for (int i = 1; i < MAX_N; i++) {
                        divs[i] = new ArrayList<>();
                        divs[i].add(1);
                }
                for (int i = 2; i < MAX_N; i++)
                        for (int j = i; j < MAX_N; j += i)
                                divs[j].add(i);
                for (int i = 1; i < MAX_N; i++)
                        Collections.reverse(divs[i]);
        }

        long getCount(int n, int R) {
                if (n == 1)
                        return 0L;
                if (R <= 1)
                        return 0L;
                int cnt = divs[n].size();
                int[] with = new int[cnt];
                long res = 0;
                for (int i = 0; i < cnt - 1; i++) {
                        int cur = divs[n].get(i);
                        with[i] += R / cur;
                        res += with[i];
                        for (int j = cnt - 1; j >= 0 && divs[n].get(j) < cur; j--)
                                if (cur % divs[n].get(j) == 0)
                                        with[j] -= with[i];
                }
                return res;
        }

        long getSum(int n, int R) {
                if (R <= 1)
                        return 0L;
                if (n == 1)
                        return 0L;
                int cnt = divs[n].size();
                long[] with = new long[cnt];
                long res = 0;
                for (int i = 0; i < cnt - 1; i++) {
                        int cur = divs[n].get(i);
                        with[i] += R / cur * 1L * (R / cur + 1) / 2 * cur;
                        res += with[i];
                        for (int j = cnt - 1; j >= 0 && divs[n].get(j) < cur; j--)
                                if (cur % divs[n].get(j) == 0)
                                        with[j] -= with[i];
                }
                return res;
        }
}

class FastScanner {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public FastScanner(InputStream inputStream)  {
                reader = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String nextToken() {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                        String line;
                        try {
                                line = reader.readLine();
                        } catch (IOException e) {
                                return null;
                        }
                        tokenizer = new StringTokenizer(line);
                }
                return tokenizer.nextToken();
        }

        public int nextInt() {
                return Integer.parseInt(nextToken());
        }

        }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.stream.IntStream;

public class d {
    public static void main(String[] args) {
        FS in = new FS(System.in);
        PrintWriter out = new PrintWriter(System.out);

        new d().solve(in, out);

        out.close();
    }

    public void solve(FS in, PrintWriter out) {
        int x1 = in.nextInt();
        int x2 = in.nextInt();
        int d = x2-x1;
        int a = in.nextInt();
        int b = in.nextInt();
        if(Integer.signum(a) != Integer.signum(b)) {
            if(d >= a && d <= b) {
                System.out.println("FIRST");
                System.out.println(x2);
            } else {
                System.out.println("DRAW");
            }
            return;
        }
        int has = 1;
        if(a < 0) {
            a *= -1;
            b *= -1;
            d *= -1;
            has *= -1;
            a ^= b;
            b ^= a;
            a ^= b;
        }
        if(b == 0) {
            System.out.println("DRAW");
            return;
        }
        if(d < 0) {
            System.out.println("DRAW");
            return;
        }

        if(d%(a+b) == 0) {
            System.out.println("SECOND");
            return;
        }

        if(d%(a+b) < a || d%(a+b) > b) {
            System.out.println("DRAW");
            return;
        }

        System.out.println("FIRST");
        System.out.println(x1+has*(d%(a+b)));
    }

    static class FS {

        BufferedReader in;
        StringTokenizer token;

        public FS(InputStream str) {
            in = new BufferedReader(new InputStreamReader(str));
        }

        public String next() {
            if (token == null || !token.hasMoreElements()) {
                try {
                    token = new StringTokenizer(in.readLine());
                } catch (IOException ex) {
                }
                return next();
            }
            return token.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}


/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class ACMIND
{
    static FastReader scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 1_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null,null,"BaZ",1<<25)
        {
            public void run()
            {
                try
                {
                    solve();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }
    static int n, g[], t[], T;
    static int dp[][];
    static void solve() throws IOException
    {
        scan = new FastReader();
        pw = new PrintWriter(System.out,true);
        StringBuilder fast = new StringBuilder();
        n = ni();
        T = ni();
        g = new int[n];
        t = new int[n];
        for(int i=0;i<n;++i) {
            t[i] = ni();
            g[i] = ni();
        }
        int MAX = (1<<n);
        dp = new int[MAX][4];
        for(int i=0;i<MAX;++i) {
            for(int j=0;j<4;++j) {
                dp[i][j] = -1;
            }
        }
        pl(f((1<<n)-1,0));
        pw.flush();
        pw.close();
    }

    static int f(int mask, int prev) {
        if(dp[mask][prev]!=-1) {
            return dp[mask][prev];
        }
        int left = T;
        for(int i=0;i<n;++i) {
            if((mask&(1<<i))==0) {
                left-=t[i];
            }
        }
        if(left==0) {
            return 1;
        }
        int cnt = 0;
        for(int i=0;i<n;++i) {
            if((mask&(1<<i))!=0) {
                if(g[i]!=prev && left>=t[i]) {
                    cnt+=f(mask^(1<<i), g[i]);
                    if(cnt>=MOD) {
                        cnt-=MOD;
                    }
                }
            }
        }
        return dp[mask][prev] = cnt;
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static class FastReader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public FastReader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public FastReader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[1000000];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
            if (neg) return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null) return;
            din.close();
        }
    }
}


import java.io.*;

/**
 * Codeforces Beta Round #72 (Div. 1 Only), D
 * @author Roman Kosenko <madkite@gmail.com>
 */
public class Main {
	public static void main(String... args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] ss = br.readLine().split(" ");
		int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), k = Integer.parseInt(ss[2]), result = !Utils.isPrime(k) ? 0 : solve(a, b, k);
		System.out.println(result);
	}
	private static int primes[];
	private static int solve(int a, int b, int k) {
		final int start = (a - 1) / k + 1, end = b / k;
		if(end < start)
			return 0;
		if(start == 1 && end == 1)
			return 1;
		if(primes == null)
			primes = Utils.primes(Math.min(k - 1, end), 0);
		int result = end - start + 1;
		for(int i = 0; i < primes.length && primes[i] < k; i++)
			result -= solve(start, end, primes[i]);
		return result;
	}
}
/**
 * @author Roman Kosenko <madkite@gmail.com>
 */
abstract class Utils {
	public static boolean isPrime(int x) {
		if(x <= 2)
			return true;
		if((x & 1) == 0)
			return false;
		int hi = (int)Math.sqrt(x);
		for(int i = 3; i <= hi; i += 2) {
			if(x % i == 0)
				return false;
		}
		return true;
	}
	public static int[] primes(int m, int n) {
		boolean[] primes = new boolean[m + 1];
		int count = 0;
		for(int i = 2; i <= m; i++) {
			if(!primes[i]) {
				for(int j = i * 2; j <= m; j += i)
					primes[j] = true;
				count++;
			}
		}
		int result[] = new int[n > 0 ? Math.min(n, count) : count];
		for(int i = 0, j = 2; i < result.length && j <= m; j++) {
			if(!primes[j])
				result[i++] = j;
		}
		return result;
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        long a = Long.parseLong(st.nextToken());
        long b = Long.parseLong(st.nextToken());
        int p = Integer.parseInt(st.nextToken());
        long x = Long.parseLong(st.nextToken());
        long[] inverse = new long[p];
        inverse[p - 1] = b;
        for(int i = p - 2; i >= 0; i--){
            inverse[i] = (inverse[i + 1] * a) % p;
        }
        //System.out.println(inverse[0]);
        long result = 0;
        for(int i = 0; i < p - 1; i++){
            int start = (i - (int)inverse[i] + p) % p;
            long first = start * ((long)(p - 1)) + i;
            if(x >= first)
                result += ((x - first) / ((long)p * (p - 1)) + 1);
        }
        System.out.println(result);
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class LuckyTickets {
    // 2^23*7*17+1
    private static final int MOD = 998244353;
    // ROOT^(2^23)=1 & ROOT^(2^22)!=1
    private static final int ROOT = 15311432;
    private static final int INVROOT = 469870224;

    private static int bitReverse(int n, int k) {
        return Integer.reverse(n) >>> (32 - k);
    }

    private static long modInv(int a) {
        return modPow(a, MOD - 2);
    }

    private static long modPow(int a, int p) {
        if (p == 0) {
            return 1;
        }
        long half = modPow(a, p / 2);
        long ans = (half * half) % MOD;
        if (p % 2 == 1) {
            ans = (ans * a) % MOD;
        }
        return ans;
    }

    // my first fft, LETS DO THIS!!
    // coef.length = 2^k
    private static void numberTheoreticTransform(long[] coef, int k, boolean inverse) {
        int baseRoot = inverse ? INVROOT : ROOT;
        long root = modPow(baseRoot, (int) modPow(2, 23 - k));
        long[] roots = new long[k];
        roots[k - 1] = root;
        for (int i = k - 1; i > 0; i--) {
            roots[i - 1] = (roots[i] * roots[i]) % MOD;
        }
        for (int s = 0; s < k; s++) {
            int len = 1 << s;
            for (int st = 0; st < coef.length; st += 2 * len) {
                // combine the vectors starting at st and st + len
                long curRoot = 1;
                for (int j = st; j < st + len; j++) {
                    long t = (curRoot * coef[j + len]) % MOD;
                    long u = coef[j];
                    coef[j] = (u + t) % MOD;
                    coef[j + len] = (u - t) % MOD;
                    curRoot = (roots[s] * curRoot) % MOD;
                }
            }
        }
        return;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt() / 2;
        int m = sc.nextInt();
        int[] nums = new int[m];
        for (int i = 0; i < m; i++) {
            nums[i] = sc.nextInt();
        }
        Arrays.sort(nums);
        int min_size = n * nums[m - 1] + 2;
        int k = 0;
        int size = 1;
        while (size < min_size) {
            size *= 2;
            k++;
        }
        long[] a = new long[size];
        for (int i = 0; i < nums.length; i++) {
            a[bitReverse(nums[i], k)] = 1;
        }
        numberTheoreticTransform(a, k, false);
        for (int i = 0; i < size; i++) {
            a[i] = modPow((int) a[i], n);
        }
        long[] temp = new long[size];
        for (int i = 0; i < size; i++) {
            temp[bitReverse(i, k)] = (a[i]) % MOD;
        }
        a = temp;
        numberTheoreticTransform(a, k, true);
        long ans = 0;
        for (int i = 0; i < size; i++) {
            ans = (ans + a[i] * a[i]) % MOD;
        }
        long multiplier = modInv(size);
        System.out.println((((ans * multiplier) % MOD) * multiplier) % MOD);
    }
}


import java.io.*;
import java.util.*;

public class A {
    private FastScanner in;
    private PrintWriter out;

    private void solve() {
        String f0 = "What are you doing at the end of the world? Are you busy? Will you save us?";
        final int MAX = (int) 1e5 + 10;
        long[] size = new long[MAX];
        size[0] = f0.length();
        String add0 = "What are you doing while sending \"";
        String add1 = "\"? Are you busy? Will you send \"";
        String add2 = "\"?";
        long inf = (long) 1e18 + 100;
        int a0 = add0.length(), a1 = add1.length(), a2 = add2.length();
        for (int i = 1; i < MAX; i++) {
            size[i] = a0 + a1 + a2 + size[i - 1] * 2;
            size[i] = Math.min(size[i], inf);
        }
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            int n = in.nextInt();
            long k = in.nextLong() - 1;
            boolean f = false;
            while (n > 0) {
                if (k < a0) {
                    out.print(add0.charAt((int) k));
                    f = true;
                    break;
                }
                k -= a0;
                if (k < size[n - 1]) {
                    n--;
                    continue;
                }
                k -= size[n - 1];
                if (k < a1) {
                    out.print(add1.charAt((int) k));
                    f = true;
                    break;
                }
                k -= a1;
                if (k < size[n - 1]) {
                    n--;
                    continue;
                }
                k -= size[n - 1];
                if (k >= a2) {
                    out.print(".");
                    f = true;
                    break;
                }
                out.print(add2.charAt((int) k));
                f = true;
                break;
            }
            if (f) {
                continue;
            }
            if (n == 0) {
                out.print(k >= f0.length() ? "." : f0.charAt((int) k));
            }
        }
    }

    private void run() {
        try {
            in = new FastScanner(new File("A.in"));
            out = new PrintWriter(new File("A.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new A().runIO();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.AbstractCollection;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DPriganieLyagushki solver = new DPriganieLyagushki();
        solver.solve(1, in, out);
        out.close();
    }

    static class DPriganieLyagushki {

        static final int N = (int) 2e5;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int m = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            int[] earliest = new int[N + 1];
            Arrays.fill(earliest, -1);
            earliest[0] = 0;
            PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.comparingInt(x -> earliest[x]));
            queue.add(0);
            while (!queue.isEmpty()) {
                int val = queue.poll();
                if (val + a <= N && earliest[val + a] == -1) {
                    earliest[val + a] = Math.max(val + a, earliest[val]);
                    queue.add(val + a);
                }
                if (val > b && earliest[val - b] == -1) {
                    earliest[val - b] = earliest[val];
                    queue.add(val - b);
                }
            }
            long ans = 0;
            for (int i = 0; i < a; i++) {
                if (m < earliest[i] || earliest[i] == -1) {
                    continue;
                }
                ans += ((earliest[i] * 1L - i) / a + 1) * (m - earliest[i] * 1L + 1);
                long st = i + ((earliest[i] * 1L - i) / a + 1) * a;
                if (st > m) {
                    continue;
                }
                long ct = (m * 1L - st) / a + 1;
                ans += (m - st + 1) * ct - (ct - 1) * ct / 2 * a;
            }
            out.println(ans);
        }
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}


/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class x1204E
   {
      static long MOD = 998244853L;
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int N = Integer.parseInt(st.nextToken());
         int M = Integer.parseInt(st.nextToken());
         fac = new long[4001];
         invfac = new long[4001];
         fac[0] = invfac[0] = 1L;
         for(int i=1; i <= 4000; i++)
         {
            fac[i] = (fac[i-1]*i)%MOD;
            invfac[i] = power(fac[i], MOD-2, MOD);
         }
         long[][] zero = new long[N+1][M+1];
         Arrays.fill(zero[0], 1L);
         for(int a=1; a <= N; a++)
            for(int b=a; b <= M; b++)
               zero[a][b] = (zero[a-1][b]+zero[a][b-1])%MOD;
         long[][] dp = new long[N+1][M+1];
         for(int a=0; a <= N; a++)
            dp[a][0] = a;
         for(int a=1; a <= N; a++)
            for(int b=1; b <= M; b++)
            {
               long temp = (dp[a-1][b]+dp[a][b-1])%MOD;
               temp = (temp+cnt(a-1,b)-cnt(a,b-1)+MOD)%MOD;
               dp[a][b] = (temp+zero[a][b-1])%MOD;
            }
         System.out.println(dp[N][M]);
      }
      static long fac[];
      static long invfac[];
      public static long cnt(int a, int b)
      {
         long val = fac[a+b];
         val = (val*invfac[a])%MOD;
         return (val*invfac[b])%MOD;
      }
      public static long power(long x, long y, long p)
      {
          long res = 1L;
          x = x % p;
          while (y > 0)
          {
              if((y & 1)==1)
                  res = (res * x) % p;
              y = y >> 1;
              x = (x * x) % p;
          }
          return res;
      }
   }


//package codeforces.cfr330div2;

import java.io.*;
import java.util.StringTokenizer;
import java.util.function.Function;

/**
 * Created by raggzy on 27-Jan-16.
 */
public class D {
    private static class Reader implements Closeable {
        private BufferedReader br;
        private StringTokenizer st;

        public Reader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        public void readLine() {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public String next() {
            if (st == null || !st.hasMoreTokens()) {
                readLine();
            }
            return st.nextToken();
        }

        public <T> T next(Function<String, T> parser) {
            return parser.apply(next());
        }

        public int nextInt() {
            return next(Integer::valueOf);
        }

        public long nextLong() {
            return next(Long::valueOf);
        }

        public double nexDouble() {
            return next(Double::valueOf);
        }

        @Override
        public void close() throws IOException {
            br.close();
        }
    }

    public static final double TEPS = 0.0000005;

    private static double findT(int s, int f, int r, int v, double a0) {
        double halfDist = (f - s) / 2.0;
        double halfDistDivV = halfDist / v;
        final double VdivR = 1.0 * v / r;
        final double RDivV = 1.0 * r / v;
        double targetX = r * Math.cos(a0) + halfDist;
        double angleRange = 2 * Math.PI * RDivV;
        double tLeft = halfDistDivV - angleRange;
        double tRight = halfDistDivV + angleRange;
        while (tRight - tLeft >= TEPS * Math.max(1.0, tLeft)) {
            double tMid = (tLeft + tRight) / 2;
            if (tMid * v + Math.cos(a0 + tMid * VdivR) * r > targetX) {
                tRight = tMid;
            } else {
                tLeft = tMid;
            }
        }
        return 2 * tLeft;
    }

    private static double findT(int s, int f, int r, int v) {
        return Math.min(findT(s, f, r, v, Math.PI / 2), findT(s, f, r, v, 3 * Math.PI / 2));
    }

    public static void main(String[] args) throws FileNotFoundException {
//        Scanner in = new Scanner(new File("C:/Temp/test.out"));
//        PrintStream out = new PrintStream(new File("C:/Temp/test2.out"));
        Reader in = new Reader(System.in);
        PrintStream out = System.out;
        int n = in.nextInt();
        int r = in.nextInt();
        int v = in.nextInt();
        for (int i = 0; i < n; i++) {
            out.println(findT(in.nextInt(), in.nextInt(), r, v));
        }
    }
}


//package round85;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class D {
	Scanner in;
	PrintWriter out;
	String INPUT = "3 2 2";
	int mod = 1000000007;

	// 1列の場合はなんでもありなのでk^n
	// 2列の場合はどっちも同じi色であればいいので、
	// \sum_i (C(k,i)F(n,i))^2
	// 3列以上の場合は両端が共通部分を1色以上持つ同じ色数の配色、真ん中はその色内ならなんでもよい。
	// |A|<=|A\cup C|=|B|, |B|<=|B\cup C|=|A|
	// |A|=|B|, C\in A\cup B
	// \sum_i (F(n,i)^2)\sum_p=1^i C(k,p)p^{n(m-2)}C(k-p,i-p)C(k-i,i-p)
	// C(k,p)C(k-p,i-p)C(k-i,i-p)
	// =k!/p!(i-p)!(k-2i+p)!(i-p)!
	// F(n,i)はn行をちょうどi色で塗る塗り方
	// F(n,i)=i^n-C(i,1)(i-1)^n+C(i,2)(i-2)^n-...
	// これを求めるのにO(i)
	// 全体でO(k^2)かかってしまうので普通の計算では間に合わない。
	//
	//

	void solve()
	{
		int n = ni();
		int m = ni();
		int K = ni();

		int q = Math.max(K, n);
		long[] A = new long[q+1];
		long[] IA = new long[q+1];
		A[0] = 1;
		IA[0] = 1;
		for(int i = 1;i <= q;i++){
			A[i] = A[i-1] * i % mod;
			IA[i] = inv(A[i], mod);
		}

		long[] I = new long[1001];
		long[] IM = new long[1001];
		for(int i = 0;i <= 1000;i++){
			I[i] = modpow(i, n, mod);
			IM[i] = modpow(i, n*(m-2), mod);
		}

		// F(n,i)=i^n-C(i,1)(i-1)^n+C(i,2)(i-2)^n-...
		long[] F = new long[n+1];
		for(int i = 0;i <= n;i++){
			long val = 0;
			long minus = 1;
			for(int j = i;j >= 0;j--){
				val += I[j] * minus * A[i]%mod*IA[i-j]%mod*IA[j] % mod;
				minus = -minus;
			}
			F[i] = (val%mod+mod)%mod;
		}

		if(m == 1){
			out.println(modpow(K, n, mod));
		}else if(m == 2){
			long sum = 0;
			for(int i = 1;i <= Math.min(K, n);i++){
				long cfm = A[K]*IA[i]%mod*IA[K-i]%mod*F[i]%mod;
				sum += cfm*cfm%mod;
			}
			out.println(sum%mod);
		}else{
			long sum = 0;
			// \sum_i (F(n,i)^2)\sum_p=1^i C(k,p)p^{n(m-2)}C(k-p,i-p)C(k-i,i-p)
			// =k!/p!(i-p)!(k-2i+p)!(i-p)!
			for(int i = 1;i <= Math.min(K, n);i++){
				long psum = 0;
				for(int p = 1;p <= i;p++){
					if(K-2*i+p >= 0){
						psum += A[K]*IA[p]%mod*IA[i-p]%mod*IA[i-p]%mod*IA[K-2*i+p]%mod*IM[p]%mod;
					}
				}
				psum%=mod;
				long all = F[i]*F[i]%mod*psum%mod;
				sum += all;
			}
			out.println(sum%mod);
		}
	}

	public static long inv(long a, int p)
	{
		long ret = 1;
		long mul = a;
		for(long n = p-2;n > 0;n >>>= 1){
			if((n&1)==1)ret = ret * mul % p;
			mul = mul * mul % p;
		}
		return ret;
	}

	public static long modpow(long a, long n, long mod)
	{
		long ret = 1;
		long mul = a;
		for(;n > 0;n >>>= 1){
			if((n&1)==1){
				ret = (ret * mul) % mod;
			}
			mul = (mul * mul) % mod;
		}
		return ret;
	}

	void run() throws Exception
	{
		in = oj ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{
		new D().run();
	}

	int ni() { return Integer.parseInt(in.next()); }
	long nl() { return Long.parseLong(in.next()); }
	double nd() { return Double.parseDouble(in.next()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}