using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Numerics;

class Program
{
    static TextReader reader;
    static TextWriter writer;
    static Program()
    {
#if ONLINE_JUDGE
        reader = Console.In;
        writer = Console.Out;
#else
        reader = new StreamReader("input.txt");
        writer = new StreamWriter("output.txt");
#endif
    }
    static long gcd(long a, long b)
    {
        if (a < b)
        {
            long t = a;
            a = b;
            b = t;
        }
        return (b > 0) ? gcd(b, a % b) : a;
    }
    static void _235A()
    {
        long n = long.Parse(reader.ReadLine()), ans = n * (n - 1);
        if (n < 3)
            ans = n;
        else if (n % 2 == 1)
            ans *= n - 2;
        else
        {
            long t = (n - 2) / 2;
            for (long i = n - 3; i > (n - 2) / 2; --i)
            {
                long g = gcd(n, i);
                if (g == 1)
                    t = Math.Max(t, i);
            }
            ans *= t;
        }
        writer.Write(Math.Max(ans, (n - 1) * (n - 2) * (n - 3)));
    }
    static void _106C()
    {
        string[] s = reader.ReadLine().Split();
        int n = int.Parse(s[0]), m = int.Parse(s[1]), c0 = int.Parse(s[2]), d0 = int.Parse(s[3]);
        int[] a = new int[m + 1], b = new int[m + 1], c = new int[m + 1], d = new int[m + 1];
        for (int i = 1; i <= m; ++i)
        {
            s = reader.ReadLine().Split();
            a[i] = int.Parse(s[0]);
            b[i] = int.Parse(s[1]);
            c[i] = int.Parse(s[2]);
            d[i] = int.Parse(s[3]);
        }
        int[,] dp = new int[m + 1, n + 1];
        for (int i = 1; i * c0 <= n; ++i)
            dp[0, i * c0] = i * d0;
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                if (j < c[i])
                    dp[i, j] = dp[i - 1, j];
                else
                {
                    for (int k = 0; k * b[i] <= a[i]; ++k)
                    {
                        int t = c[i] * k;
                        if (t <= j)
                            dp[i, j] = Math.Max(d[i] * k + dp[i - 1, j - t], dp[i, j]);
                    }
                }
                dp[i, j] = Math.Max(dp[i, j - 1], dp[i, j]);
            }
        }
        writer.Write(dp[m, n]);
    }
    static long pow(long a, long x, long mod)
    {
        long res = 1;
        while (x > 0)
        {
            if ((x & 1) != 0)
            {
                res = (res * a) % mod;
                --x;
            }
            else
            {
                a = (a * a) % mod;
                x >>= 1;
            }
        }
        return res;
    }
    static void _300C()
    {
        string[] s = reader.ReadLine().Split();
        long a = long.Parse(s[0]), b = long.Parse(s[1]), n = long.Parse(s[2]);
        if (a > b)
        {
            long t = a;
            a = b;
            b = t;
        }
        long[] fact = new long[n + 1];
        fact[0] = fact[1] = 1;
        long mod = 1000000007;
        for (long i = 2; i <= n; ++i)
            fact[i] = fact[i - 1] * i % mod;
        long an = n, bn = 0;
        long ans = 0;
        while (an >= 0)
        {
            long num = an * a + bn * b;
            bool ok = true;
            for (long p = 10; ok && p <= 10000000; p *= 10)
            {
                long t = num % p / (p / 10);
                if (t != a && t != b && (t != 0 || p <= num))
                    ok = false;
            }
            if (ok)
            {
                long c = (((fact[n] * pow(fact[an], mod - 2, mod)) % mod) * pow(fact[bn], mod - 2, mod)) % mod;
                ans += c;
                ans %= mod;
            }
            --an;
            ++bn;
        }
        writer.Write(ans);
    }
    static void _250B()
    {
        string s = reader.ReadLine();
        int n = int.Parse(s);
        for (int i = 0; i < n; ++i)
        {
            s = reader.ReadLine();
            string[] ip = new string[8];
            int nums = 0;
            for (int l = 0, j = 0; j < s.Length; ++j)
            {
                if (s[j] == ':')
                {
                    if (nums > 0)
                    {
                        //ip[l] = "";
                        for (int k = j - nums; k < j; ++k)
                            ip[l] += s[k];
                        ++l;
                        nums = 0;
                    }
                    else
                        break;
                }
                else
                    ++nums;
            }
            nums = 0;
            //if (ip[7] == null)
            {
                for (int r = 7, j = s.Length - 1; j >= 0; --j)
                {
                    if (ip[r] != null)
                        break;
                    if (s[j] == ':')
                    {
                        if (nums > 0)
                        {
                            for (int k = j + 1; k <= j + nums; ++k)
                                ip[r] += s[k];
                            --r;
                            nums = 0;
                        }
                        else
                            break;
                    }
                    else
                        ++nums;
                }
            }
            for (int j = 0; j < 8; ++j)
            {
                if (ip[j] == null)
                    ip[j] = "";
                while (ip[j].Length < 4)
                    ip[j] = '0' + ip[j];
            }
            for (int j = 0; j < 7; ++j)
                writer.Write(ip[j] + ':');
            writer.WriteLine(ip[7]);
        }
    }
    static void _316B2()
    {
        string[] s = reader.ReadLine().Split();
        int n = int.Parse(s[0]), x = int.Parse(s[1]);
        s = reader.ReadLine().Split();
        Dictionary<int, int> queue = new Dictionary<int, int>();
        LinkedList<int> undef = new LinkedList<int>();
        for (int i = 1; i <= n; ++i)
        {
            int h = int.Parse(s[i - 1]);
            if (h > 0)
                queue.Add(h, i);
            else
                undef.AddLast(i);
        }
        int[] lens = new int[undef.Count];
        int c = 0, index = 0;
        foreach (int v in undef)
        {
            int next = v;
            lens[c] = 1;
            while (true)
            {
                if (next == x)
                {
                    index = lens[c];
                    lens[c] = 0;
                    break;
                }
                if (queue.ContainsKey(next))
                {
                    next = queue[next];
                    ++lens[c];
                }
                else
                    break;
            }
            ++c;
        }
        bool[,] dp = new bool[2, n + 1];
        dp[0, 0] = true;
        for (int i = 0; i < lens.Length; ++i)
        {
            if (lens[i] > 0)
            {
                dp[1, lens[i]] = true;
                for (int j = 1; j + lens[i] <= n; ++j)
                {
                    if (dp[0, j])
                    {
                        dp[1, j + lens[i]] = true;
                    }
                }
                for (int j = 1; j <= n; ++j)
                {
                    dp[0, j] = dp[0, j] || dp[1, j];
                    dp[1, j] = false;
                }
            }
        }
        for (int i = 0; i <= n; ++i)
            if (dp[0, i])
                writer.WriteLine(i + index);
    }
    static void _121A()
    {
        string[] lr = reader.ReadLine().Split();
        long l = long.Parse(lr[0]), r = long.Parse(lr[1]);
        SortedSet<long> set = new SortedSet<long>();
        set.Add(4);
        set.Add(7);
        for (long p = 10; p <= 1000000000; p *= 10)
        {
            SortedSet<long> s = new SortedSet<long>(set);
            foreach (long e in s)
            {
                if (e / (p / 10) > 0)
                {
                    set.Add(e + p * 4);
                    set.Add(e + p * 7);
                }
            }
        }
        while (set.ElementAt(0) < l)
            set.Remove(set.ElementAt(0));
        long max = set.Max;
        while (set.Max > r)
        {
            max = set.Max;
            set.Remove(set.Max);
        }
        if (set.Max < r)
            set.Add(max);
        else
            max = set.Max;
        long[] happy = new long[set.Count];
        long c = 0;
        foreach (long e in set)
            happy[c++] = e;
        long min = set.Min;
        long sum = 0;
        if (happy.Length == 1)
            sum = (r - l + 1) * happy[0];
        else
            sum = min * (min - l + 1);// +max * (max - r + 1);
        for (long i = 1; i < happy.Length; ++i)
            sum += (Math.Min(r, happy[i]) - happy[i - 1]) * happy[i];
        writer.Write(sum);
    }
    static void _150A()
    {
        long q = long.Parse(reader.ReadLine()), m1 = 0, m2 = 0, c = 0;
        for (long i = 2; i * i <= q && c < 2; ++i)
            while (q % i == 0)
            {
                q /= i;
                if (q == 1)
                    break;
                ++c;
                if (c == 1)
                    m1 = i;
                else if (c == 2)
                {
                    m2 = i;
                    break;
                }
            }
        if (c == 1)
            writer.Write(2);
        else
        {
            writer.WriteLine(1);
            if (c == 0)
                writer.Write(0);
            else if (c == 2)
                writer.Write(m1 * m2);
        }
    }
    static void _297B()
    {
        string[] s = reader.ReadLine().Split();
        int n = int.Parse(s[0]), m = int.Parse(s[1]), k = int.Parse(s[2]);

        string ans = "", yes = "YES", no = "NO";
        if (n > m)
            ans = yes;
        else
        {
            s = reader.ReadLine().Split();
            SortedDictionary<int, int> alice = new SortedDictionary<int, int>(), robert = new SortedDictionary<int, int>();
            //int[] alice = new int[k+1], robert = new int[k+1];
            for (int i = 0; i < n; ++i)
            {
                int t = int.Parse(s[i]);
                if (alice.ContainsKey(t))
                    ++alice[t];
                else
                    alice.Add(t, 1);
            }
            s = reader.ReadLine().Split();
            int acount = n, rcount = m;
            for (int i = 0; i < m; ++i)
            {
                int t = int.Parse(s[i]);
                if (alice.ContainsKey(t) && alice[t] > 0)
                {
                    --alice[t];
                    --acount;
                }
                else
                {
                    if (robert.ContainsKey(t))
                        ++robert[t];
                    else
                        robert.Add(t, 1);
                }
            }
            rcount -= n - acount;
            int[] a = new int[acount], r = new int[rcount];
            int ac = 0;
            foreach (var v in alice)
                for (int i = 0; i < v.Value; ++i)
                    a[ac++] = v.Key;
            /*for (int i = 1; i <= k; ++i)
                for (int j = 0; j < alice[i]; ++j)
                    a[ac++] = i;*/
            int rc = 0;
            /*for (int i = 1; i <= k; ++i)
                for (int j = 0; j < robert[i]; ++j)
                    r[rc++] = i;*/
            foreach (var v in robert)
                for (int i = 0; i < v.Value; ++i)
                    r[rc++] = v.Key;
            int d = m - n;
            for (int i = rcount - 1; i >= d; --i)
                if (a[i - d] > r[i])
                {
                    ans = yes;
                    break;
                }
            if (ans == "")
                ans = no;
        }
        writer.Write(ans);
    }
    static void _271D()
    {
        const long prime = 71;
        string s = reader.ReadLine(), alphabet = reader.ReadLine();
        int k = int.Parse(reader.ReadLine());
        HashSet<long> hashes = new HashSet<long>();
        for (int i = 0; i < s.Length; ++i)
        {
            long bad = 0, hash = 0, p = 1;
            for (int j = i; j < s.Length; ++j)
            {
                if (alphabet[s[j] - 'a'] == '0')
                    ++bad;
                if (bad > k)
                    break;
                else
                {
                    hash += ((long)(s[j] - 'a') + 1L) * p;
                    p *= prime;
                    //if (!hashes.Contains(hash))
                    hashes.Add(hash);
                }
            }
        }
        writer.Write(hashes.Count);
    }
    static void _196A()
    {
        string s = reader.ReadLine();
        int r = 0;
        StringBuilder ans = new StringBuilder();
        for (char c = 'z'; c >= 'a'; --c)
        {
            for (int i = r; i < s.Length; ++i)
            {
                if (s[i] == c)
                {
                    ans.Append(c);
                    r = i;
                }
            }
        }
        writer.Write(ans);
    }
    static void _275B()
    {
        string[] nm = reader.ReadLine().Split();
        int n = int.Parse(nm[0]), m = int.Parse(nm[1]);
        string[] table = new string[n];
        for (int i = 0; i < n; ++i)
            table[i] = reader.ReadLine();
        bool[, , ,] connected = new bool[n, m, n, m];
        int[] xdir = { 1, 0, -1, 0 }, ydir = { 0, 1, 0, -1 };
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                if (table[i][j] == 'B')
                {
                    for (int d = 0; d < 4; ++d)
                    {
                        for (int y = i, x = j; ; y += ydir[d], x += xdir[d])
                        {
                            if (x < 0 || x >= m || y < 0 || y >= n)
                                break;
                            else if (table[y][x] == 'B')
                                connected[i, j, y, x] = connected[y, x, i, j] = true;
                            else
                                break;
                        }
                    }
                }
        string ans = "YES";
        for (int i = 0; i < n && ans == "YES"; ++i)
            for (int j = 0; j < m && ans == "YES"; ++j)
                if (table[i][j] == 'B')
                    for (int k = 0; k < n && ans == "YES"; ++k)
                        for (int l = 0; l < m && ans == "YES"; ++l)
                            if (table[k][l] == 'B')
                                if (!((connected[i, j, i, l] && connected[i, l, k, l]) || (connected[i, j, k, j] && connected[k, j, k, l])))
                                    ans = "NO";
        writer.Write(ans);
        // TODO: BWB
    }
    static void _315A()
    {
        int n = int.Parse(reader.ReadLine()), ans = n;
        SortedSet<int> bottles = new SortedSet<int>();
        int[] a = new int[n], b = new int[n], c = new int[1001];
        for (int i = 0; i < n; ++i)
        {
            string[] ab = reader.ReadLine().Split();
            a[i] = int.Parse(ab[0]);
            b[i] = int.Parse(ab[1]);
            ++c[b[i]];
            bottles.Add(b[i]);
        }
        for (int i = 0; i < n; ++i)
            if (bottles.Contains(a[i]) && (a[i] != b[i] || c[a[i]] > 1))
                --ans;
        writer.Write(ans);
    }
    static void _264B()
    {
        int n = int.Parse(reader.ReadLine());
        string[] s = reader.ReadLine().Split();
        int[] a = new int[n];
        for (int i = 0; i < n; ++i)
            a[i] = int.Parse(s[i]);
        int[] dp = new int[n];
        dp[0] = 1;
        long num = a[0];
        for (int i = 1; i < n; ++i)
        {
            dp[i] = dp[i - 1];
            if (gcd(num, a[i]) == 1)
                num *= a[i];
            else
            {
                ++dp[i];
                num = a[i];
            }
        }
        writer.Write(dp[n - 1]);
    }
    struct C : IComparable<C>
    {
        public double x, y, angle;
        public C(double a, double b)
        {
            x = a;
            y = b;
            angle = Math.Atan(y / x) / Math.PI * 180.0;
            if (x < 0.0 && y > 0)
                angle += 180.0;
            if (x < 0.0 && y <= 0.0)
                angle += 180.0;
            else if (x >= 0.0 && y < 0.0)
                angle += 360.0;
            /*if (y < 0.0 || (y == 0.0 && x < 0.0))
                angle += 180.0;*/
        }
        public int CompareTo(C c)
        {
            return angle.CompareTo(c.angle);
        }
    }
    static void _222D()
    {
        string[] s = reader.ReadLine().Split();
        int n = int.Parse(s[0]), x = int.Parse(s[1]);
        int[] a = new int[n], b = new int[n];
        s = reader.ReadLine().Split();
        for (int i = 0; i < n; ++i)
            a[i] = int.Parse(s[i]);
        Array.Sort(a);
        Array.Reverse(a);
        s = reader.ReadLine().Split();
        for (int i = 0; i < n; ++i)
            b[i] = int.Parse(s[i]);
        Array.Sort(b);
        int min = 0, ia = 0, ib = 0;
        while (ib < n)
        {
            if (a[ia] + b[ib] < x)
                ++ib;
            else
            {
                ++min;
                ++ia;
                ++ib;
            }
        }
        writer.Write(1 + " " + min);
    }
    static void _257C()
    {
        int n = int.Parse(reader.ReadLine());
        SortedSet<C> set = new SortedSet<C>();
        for (int i = 0; i < n; ++i)
        {
            string[] xy = reader.ReadLine().Split();
            set.Add(new C(int.Parse(xy[0]), int.Parse(xy[1])));
        }
        double angle = 0.0;
        C prev = set.Min;
        foreach (C c in set)
        {
            if (!c.Equals(prev))
            {
                angle = Math.Max(angle, Math.Abs(c.angle - prev.angle));
                prev = c;
            }
        }
        angle = Math.Max(angle, 360.0 - (set.Max.angle - set.Min.angle));
        writer.Write((360.0 - angle).ToString().Replace(',', '.'));
    }
    static void swap(ref int a, ref int b)
    {
        int t = a;
        a = b;
        b = t;
    }
    static void _288B()
    {
        string[] kn = reader.ReadLine().Split();
        int n = int.Parse(kn[0]), k = int.Parse(kn[1]);
        writer.Write(BigInteger.Pow(k, k - 1) * BigInteger.Pow(n - k, n - k) % 1000000007);
    }
    static void Main(string[] args)
    {
        //_264B();
        _288B();
        reader.Close();
        writer.Close();
    }
}


#define Online

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

delegate double F(double x);
delegate decimal Fdec(decimal x);

class Program
{//

    static void Rec(int v, int[] a, ref long cnt)
    {
        if (v == a.Length)
        {
            for (int i = 0, j = 0, p = 0; i < a.Length; i++)
            {
                p = a[i];
                j = 0;
                while (j < a.Length)
                {
                    if (p == 0) { break; }
                    p = a[p];
                    j++;
                }
                if (j == a.Length)
                    return;
            }
            cnt++;
        }
        else
        {
            for (int i = 0; i < a.Length; i++)
            {
                a[v] = i;
                Rec(v + 1, a, ref cnt);
            }
        }
    }

    static void Main(string[] args)
    {

        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if FileIO
        StreamReader sr = new StreamReader("frozen.in");
        StreamWriter sw = new StreamWriter("frozen.out");
        Console.SetIn(sr);
        Console.SetOut(sw);
#endif

        int n, k;
        string[] input = ReadArray();
        const long mod=1000000007;
        n = int.Parse(input[0]);
        k = int.Parse(input[1]);
        long cnt = 0;
        int[] a = new int[k];
        Rec(0, a, ref cnt);
        for (int i = 0; i < n - k; i++)
        {
            cnt = (cnt * (n - k)) % mod;
        }
        Console.WriteLine(cnt);

#if Online
        Console.ReadLine();
#endif

#if FileIO
        sr.Close();
        sw.Close();
#endif
    }

    static string[] ReadArray()
    {
        return Console.ReadLine().Split(' ');
    }

    static int ReadInt()
    {
        return int.Parse(Console.ReadLine());
    }
}

static class ArrayUtils
{
    public static int BinarySearch(int[] a, int val)
    {
        int left = 0;
        int right = a.Length - 1;
        int mid;

        while (left < right)
        {
            mid = left + ((right - left) >> 1);
            if (val <= a[mid])
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }

        if (a[left] == val)
            return left;
        else
            return -1;
    }

    public static double Bisection(F f, double left, double right, double eps)
    {
        double mid;
        while (right - left > eps)
        {
            mid = left + ((right - left) / 2d);
            if (Math.Sign(f(mid)) != Math.Sign(f(left)))
                right = mid;
            else
                left = mid;
        }
        return (left + right) / 2d;
    }

    public static decimal TernarySearchDec(Fdec f, decimal eps, decimal l, decimal r, bool isMax)
    {
        decimal m1, m2;
        while (r - l > eps)
        {
            m1 = l + (r - l) / 3m;
            m2 = r - (r - l) / 3m;
            if (f(m1) < f(m2))
                if (isMax)
                    l = m1;
                else
                    r = m2;
            else
                if (isMax)
                    r = m2;
                else
                    l = m1;
        }
        return r;
    }

    public static double TernarySearch(F f, double eps, double l, double r, bool isMax)
    {
        double m1, m2;
        while (r - l > eps)
        {
            m1 = l + (r - l) / 3d;
            m2 = r - (r - l) / 3d;
            if (f(m1) < f(m2))
                if (isMax)
                    l = m1;
                else
                    r = m2;
            else
                if (isMax)
                    r = m2;
                else
                    l = m1;
        }
        return r;
    }

    public static void Merge(int[] A, int p, int q, int r, int[] L, int[] R)
    {
        for (int i = p; i <= q; i++)
            L[i] = A[i];
        for (int i = q + 1; i <= r; i++)
            R[i] = A[i];

        for (int k = p, i = p, j = q + 1; k <= r; k++)
        {
            if (i > q)
            {
                for (; k <= r; k++, j++)
                    A[k] = R[j];
                return;
            }
            if (j > r)
            {
                for (; k <= r; k++, i++)
                    A[k] = L[i];
                return;
            }
            if (L[i] <= R[j])
            {
                A[k] = L[i];
                i++;
            }
            else
            {
                A[k] = R[j];
                j++;
            }
        }
    }

    public static void Merge_Sort(int[] A, int p, int r, int[] L, int[] R)
    {
        if (!(p < r)) return;
        int q = p + ((r - p) >> 1);
        Merge_Sort(A, p, q, L, R);
        Merge_Sort(A, q + 1, r, L, R);
        Merge(A, p, q, r, L, R);
    }

    static void Shake(int[] a)
    {
        Random rnd = new Random(Environment.TickCount);
        int temp, b, c;
        for (int i = 0; i < a.Length; i++)
        {
            b = rnd.Next(0, a.Length);
            c = rnd.Next(0, a.Length);
            temp = a[b];
            a[b] = a[c];
            a[c] = temp;
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Polo_the_Penguin_and_Houses
{
    internal class Program
    {
        private const int mod = 1000000007;
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int n = Next();
            int k = Next();

            var first = new int[k];
            first[0] = 0;

            int count = 0;
            do
            {
                if (first[0] == 1)
                    break;

                var f = new bool[first.Length];
                bool ok = true;
                for (int i = 1; i < f.Length; i++)
                {
                    int index = i;
                    for (int j = 0; j < k; j++)
                    {
                        index = first[index];
                        if (index == 0)
                            break;
                    }
                    if (index != 0)
                    {
                        ok = false;
                        break;
                    }
                }
                if (ok)
                    count++;
            } while (Next(first, k));

            long ans = count*(k);
            for (int i = n - k; i >= 1; i--)
            {
                ans = (ans*(n - k))%mod;
            }

            writer.WriteLine(ans);
            writer.Flush();
        }

        private static bool Next(int[] nn, int k)
        {
            for (int i = nn.Length - 1; i >= 0; i--)
            {
                nn[i]++;
                if (nn[i] == k)
                {
                    nn[i] = 0;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;

namespace Codeforces.R289D
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            Contract.Requires(tr != null);
            Contract.Requires(tw != null);

            var nk = tr.ReadLine().Split().Select(v => int.Parse(v, CultureInfo.InvariantCulture)).ToArray();

            tw.WriteLine(Calc(nk[0], nk[1]));
        }

        private static int Calc(int n, int k)
        {
            var r = 0;
            var kk = (int)Math.Pow(k, k);
            for (int i = 0; i < kk; ++i)
            {
                if (Valid(k, Plaques(k, i)))
                {
                    ++r;
                }
            }
            var nmk = n - k;
            var mod = 1000000007;
            return (int)(r * ModPow(nmk, nmk, mod) % mod);
        }

        public static long ModPow(long x, int n, int mod)
        {
            var res = 1L;
            for (; n > 0; n >>= 1)
            {
                if ((n & 1) == 1)
                {
                    res = res * x % mod;
                }
                x = x * x % mod;
            }
            return res;
        }

        private static int[] Plaques(int k, int x)
        {
            var res = new int[k];
            for (int i = 0; i < k; ++i)
            {
                res[i] = x % k;
                x /= k;
            }
            return res;
        }

        private static bool Valid(int k, int[] plaques)
        {
            for (int i = 0; i < k; ++i)
            {
                if (!CanReachZero(i, k, plaques, new bool[k]))
                {
                    return false;
                }
            }
            return true;
        }

        private static bool CanReachZero(int current, int k, int[] plaques, bool[] visited)
        {
            if (current == 0)
            {
                return true;
            }

            if (visited[current])
            {
                return false;
            }

            visited[current] = true;
            return CanReachZero(plaques[current], k, plaques, visited);
        }
    }
}


using System;

namespace Олимпиада2
{
    class Program
    {
        static void Main(string[] args)
        {
            bool contin = true;
            bool start = true;
            string msg = Console.ReadLine();
            if (msg.Length == 1 || String.IsNullOrEmpty(msg) || msg.Length == 2)
            {
                Console.WriteLine("NO");
                start = false;
            }
            if (start == true)
            {
                for (int i = 0; i <= msg.Length / 2; i++)
                {
                    if (msg.Length % 2 != 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i + 1, msg.Length / 2 + i) && msg.Length / 2 + msg.Length / 2 + i + 1 > msg.Length && msg.Length / 2 - i + 1 != 0)
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i));
                        contin = false;
                        break;
                    }
                    else if (msg.Length % 2 == 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i, msg.Length / 2 + i) && msg.Length / 2 + msg.Length / 2 + i > msg.Length && msg.Length / 2 - i != 0)
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i) + "\n" + i);
                        contin = false;
                        break;
                    }
                    if (i >= msg.Length / 2)
                    {
                        Console.WriteLine("NO");
                        contin = false;
                        break;
                    }
                }
                if (contin)
                {
                    if (msg.Length % 2 == 0 && msg.Substring(0, msg.Length / 2 - 1) == msg.Substring(msg.Length / 2, msg.Length / 2 - 1))
                    {
                        Console.WriteLine("NO");
                    }
                }
            }
            Console.ReadLine();
        }
    }
}


using System;

namespace Олимпиада2
{
    class Program
    {
        static void Main(string[] args)
        {
            bool contin = true;
            bool start = true;
            string msg = Console.ReadLine();
            if (msg.Length == 1 || String.IsNullOrEmpty(msg))
            {
                Console.WriteLine("NO");
                start = false;
            }
            if (start == true)
            {
                for (int i = 0; i < msg.Length / 2; i++)
                {
                    if (msg.Length % 2 != 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i + 1, msg.Length / 2 + i))
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i));
                        contin = false;
                        break;
                    }
                    else if (msg.Length % 2 == 0 && i > 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i, msg.Length / 2 + i))
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i));
                        contin = false;
                        break;
                    }
                    if (i == msg.Length / 2 - 1)
                    {
                        Console.WriteLine("NO");
                        contin = false;
                        break;
                    }
                }
                if (contin)
                {
                    if (msg.Length % 2 == 0 && msg.Substring(0, msg.Length / 2 - 1) == msg.Substring(msg.Length / 2, msg.Length / 2 - 1))
                    {
                        Console.WriteLine("NO");
                    }
                }
            }
        }
    }
}


using System;

namespace Олимпиада2
{
    class Program
    {
        static void Main(string[] args)
        {
            bool contin = true;
            bool start = true;
            string msg = Console.ReadLine();
            if (msg.Length == 1 || String.IsNullOrEmpty(msg) || msg.Length == 2)
            {
                Console.WriteLine("NO");
                start = false;
            }
            if (start == true)
            {
                for (int i = 0; i <= msg.Length / 2; i++)
                {
                    if (msg.Length % 2 != 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i + 1, msg.Length / 2 + i) && msg.Length / 2 + msg.Length / 2 + i + 1 > msg.Length && msg.Length / 2 - i + 1 != 0)
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i));
                        contin = false;
                        break;
                    }
                    else if (msg.Length % 2 == 0 && msg.Substring(0, msg.Length / 2 + i) == msg.Substring(msg.Length / 2 - i, msg.Length / 2 + i) && msg.Length / 2 + msg.Length / 2 + i > msg.Length && msg.Length / 2 - i != 0)
                    {
                        Console.WriteLine("YES\n" + msg.Substring(0, msg.Length / 2 + i));
                        contin = false;
                        break;
                    }
                    if (i >= msg.Length / 2)
                    {
                        Console.WriteLine("NO");
                        contin = false;
                        break;
                    }
                }
                if (contin)
                {
                    if (msg.Length % 2 == 0 && msg.Substring(0, msg.Length / 2 - 1) == msg.Substring(msg.Length / 2, msg.Length / 2 - 1))
                    {
                        Console.WriteLine("NO");
                    }
                }
            }
            Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Codeforces
{

    class Program
    {
        public static readonly int MOD = Convert.ToInt32(1e9 + 7);
        public static readonly int oo = Convert.ToInt32(1e9);
        public static readonly long OO = Convert.ToInt64(1e18);

        static void Main(string[] args)
        {
            int weights = int.Parse(Console.ReadLine());
            int[] masses = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            int[] cnt = new int[101];
            foreach (int mass in masses)
            {
                cnt[mass]++;
            }
            int nonZero = 0;
            for (int i = 1; i <= 100; i++)
            {
                if (cnt[i] > 0)
                {
                    nonZero++;
                }
            }
            if (nonZero <= 2)
            {
                Console.WriteLine(weights);
                return;
            }

            int[,] C = new int[101, 101];
            C[0, 0] = 1;
            for (int i = 1; i <= 100; i++)
            {
                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]) % MOD;
                }
            }

            int sum = masses.Sum();
            int[,] ways = new int[weights + 1, sum + 1];
            ///
            /// dp(i, j, k) = dp(i - 1, j - 1, k - masses[j])
            ///

            ways[0, 0] = 1;
            for (int i = 0; i < weights; i++)
            {
                int[,] nways = new int[weights + 1, sum + 1];
                nways[0, 0] = 1;
                for (int j = 1; j <= i + 1; j++)
                {
                    for (int k = sum; k > 0; k--)
                    {
                        nways[j, k] = ways[j, k];
                        if (k >= masses[i])
                        {
                            nways[j, k] = (nways[j, k] + ways[j - 1, k - masses[i]]) % MOD;
                        }
                    }
                }

                ways = nways;
            }

            int ans = 1;
            for (int i = 1; i <= 100; i++)
            {
                if (cnt[i] > 0)
                {
                    for (int j = i; j <= sum; j += i)
                    {
                        if (j / i <= weights && ways[j / i, j] > 0 && ways[j / i, j] == C[cnt[i], j / i])
                        {
                            ans = Math.Max(ans, j / i);
                        }
                    }
                }
            }

            Console.WriteLine(ans);

        }

    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Codeforces
{

    class Program
    {
        public static readonly int MOD = Convert.ToInt32(1e9 + 7);
        public static readonly int oo = Convert.ToInt32(1e9);
        public static readonly long OO = Convert.ToInt64(1e18);

        static void Main(string[] args)
        {
            int weights = int.Parse(Console.ReadLine());
            int[] masses = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            int[] cnt = new int[101];
            foreach (int mass in masses)
            {
                cnt[mass]++;
            }

            if (cnt.Count(app => app > 0) <= 2)
            {
                Console.WriteLine(weights);
                return;
            }

            int[,] C = new int[101, 101];
            C[0, 0] = 1;
            for (int i = 1; i <= 100; i++)
            {
                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]) % MOD;
                }
            }

            int sum = masses.Sum();
            int[,] ways = new int[weights + 1, sum + 1];
            ///
            /// dp(i, j, k) = dp(i - 1, j - 1, k - masses[j])
            ///

            ways[0, 0] = 1;
            for (int i = 0; i < weights; i++)
            {
                int[,] nways = new int[weights + 1, sum + 1];
                nways[0, 0] = 1;
                for (int j = 1; j <= i + 1; j++)
                {
                    for (int k = sum; k > 0; k--)
                    {
                        nways[j, k] = ways[j, k];
                        if (k >= masses[i])
                        {
                            nways[j, k] = (nways[j, k] + ways[j - 1, k - masses[i]]) % MOD;
                        }
                    }
                }

                ways = nways;
            }

            int ans = 1;
            for (int i = 1; i <= 100; i++)
            {
                if (cnt[i] > 0)
                {
                    for (int j = i; j <= sum; j += i)
                    {
                        if (j / i <= weights && ways[j / i, j] > 0 && ways[j / i, j] == C[cnt[i], j / i])
                        {
                            ans = Math.Max(ans, j / i);
                        }
                    }
                }
            }

            Console.WriteLine(ans);

        }

    }

}


using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApplication1.CodeForces
{
    static class _4032123
    {
        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), Encoding.ASCII, false, 1024 * 10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), Encoding.ASCII, 1024 * 10);

        class Node
        {
            public int D { get; set; }
            public int F { get; set; }
            public int T { get; set; }
            public long C { get; set; }
        }
        static void Main(String[] args)
        {
            var n = int.Parse(Console.ReadLine().TrimEnd());
            var data = Console.ReadLine().TrimEnd().Split(' ').Select(long.Parse).ToList();
            var bob = new long[n,2];
            bob[n - 1, 0] = data[n - 1];
            bob[n - 1, 1] = -data[n - 1];
            var prev = new int[n];
            prev[n - 1] = 1;
            for (var i = n - 2; i >= 0; i--)
            {
                if(bob[i + 1, 0] > bob[i + 1, 1])
                {
                    prev[i] = 0;
                } else
                {
                    prev[i] = 1;
                }
                bob[i, 0] = data[i] - Math.Max(bob[i + 1, 0],bob[i + 1, 1] );
                bob[i, 1] = Math.Max(bob[i + 1, 0], bob[i + 1, 1]) - data[i];
            }
            long bobSum = 0;
            var max = Math.Max(bob[0, 0], bob[0, 1]);
            writer.WriteLine((data.Sum()/2.0 - max/2.0) + " " + (data.Sum() / 2.0 + max / 2.0));
            writer.Flush();

        }
    }
}


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = re.i();
        int[] A = re.ia();
        int[] D = new int[N];
        int[] UD = new int[N];
        D[N-1] = A[N-1];
        for(int i=N-2;i>=0;i--){
            D[i] = Math.Max(D[i+1],A[i]+UD[i+1]);
            UD[i] = Math.Min(D[i+1],A[i]+UD[i+1]);
        }
        sb.Append(UD[0]+" "+D[0]+"\n");
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();

        var dp = new int[n, 2];
        dp[n - 1, 0] = a[n - 1];
        for (int i = n - 2; i >= 0; i--)
        {
            if (dp[i + 1, 0] > dp[i + 1, 1] + a[i])
            {
                dp[i, 0] = dp[i + 1, 0];
                dp[i, 1] = dp[i + 1, 1] + a[i];
            }
            else
            {
                dp[i, 0] = dp[i + 1, 1] + a[i];
                dp[i, 1] = dp[i + 1, 0];
            }
        }

        Write(a.Sum() - dp[0, 0], dp[0, 0]);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CF
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var sr = new InputReader(Console.In))
//			using (var sr = new InputReader(new StreamReader("input.txt")))
            using (var sw = Console.Out)
                //using (var sw = new StreamWriter("output.txt"))
            using (var task = new Task(sr, sw)) {
                task.Solve();
//                Console.ReadKey();
            }
        }
    }

    internal class Task : IDisposable
    {
        private readonly InputReader sr;
        private readonly TextWriter sw;
        private bool isDispose;
        private int n;
        private int[] pies;
        private Dictionary<string, Tuple<long, long>> cache = new Dictionary<string, Tuple<long, long>>();

        public Task(InputReader sr, TextWriter sw)
        {
            this.sr = sr;
            this.sw = sw;
        }

        public void Solve()
        {
            n = sr.NextInt32();
            pies = sr.ReadArrayOfInt32();

            var answ = Solve(0, true, 0L, 0L);
            sw.WriteLine(answ.Item2 + " " + answ.Item1);
        }

        private Tuple<long, long> Solve(int i, bool isBob, long currBob, long currAlice)
        {
            if (i == n) {
                return new Tuple<long, long>(currBob, currAlice);
            }
            Tuple<long, long> r = null;

            var key = i + " " + isBob;
            if (cache.ContainsKey(key)) {
                r = cache[key];

                return Tuple.Create(currBob + r.Item1, currAlice + r.Item2);
            }

            if (isBob) {
                var r1 = Solve(i + 1, true, currBob, currAlice + pies[i]);
                var r2 = Solve(i + 1, false, currBob + pies[i], currAlice);
                r = r1.Item1 > r2.Item1 ? r1 : r2;
            }
            else {
                var r1 = Solve(i + 1, false, currBob + pies[i], currAlice);
                var r2 = Solve(i + 1, true, currBob, currAlice + pies[i]);

                r = r1.Item2 > r2.Item2 ? r1 : r2;
            }

            cache.Add(key, Tuple.Create(r.Item1 - currBob, r.Item2 - currAlice));

            return r;
        }

        ~Task()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!isDispose) {
                if (disposing) {
                    if(sr != null)
                        sr.Dispose();

                    if(sw != null)
                        sw.Dispose();
                }

                isDispose = true;
            }
        }
    }
}

internal class InputReader : IDisposable
{
    private bool isDispose;
    private readonly TextReader sr;
    private string[] buffer;
    private int seek;

    public InputReader(TextReader stream)
    {
        sr = stream;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public string NextString()
    {
        var result = sr.ReadLine();
        return result;
    }

    public int NextInt32()
    {
        return Int32.Parse(ReadNextToken());
    }

    private string ReadNextToken()
    {
        if (buffer == null || seek == buffer.Length) {
            seek = 0;
            buffer = NextSplitStrings();
        }

        return buffer[seek++];
    }

    public long NextInt64()
    {
        return Int64.Parse(ReadNextToken());
    }

    public int[] ReadArrayOfInt32()
    {
        return ReadArray(Int32.Parse);
    }

    public long[] ReadArrayOfInt64()
    {
        return ReadArray(Int64.Parse);
    }

    public string[] NextSplitStrings()
    {
        return NextString()
            .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
    {
        return NextSplitStrings()
            .Select(s => func(s, CultureInfo.InvariantCulture))
            .ToArray();
    }

    public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
    {
        return
            str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => func(s, CultureInfo.InvariantCulture))
                .ToArray();
    }

    protected void Dispose(bool dispose)
    {
        if (!isDispose)
        {
            if (dispose)
                sr.Close();

            isDispose = true;
        }
    }

    ~InputReader()
    {
        Dispose(false);
    }
}


using System;
using System.Collections.Generic;
using System.IO;

class Pie{
   Dictionary<int,int>winSize;

   int getSum(int[]arr,int start){
      int sum = 0;
      int len = arr.Length;
      for(int i=start;i<len;i++){
         sum += arr[i];
      }
      return sum;
   }

   int getWinSize(int[]arr, int ind){
      int len = arr.Length;
      int retV = 0;
      int retSum = getSum(arr,ind+1);
      if(ind==(len-1)){
         retV = arr[ind];
      }else if(ind==(len-2)){
         retV = Math.Max(arr[len-1],arr[len-2]);
      }else{
      //dynamic programming
         int remW = winSize[ind+1];
	 int s1 = arr[ind]+(retSum-remW);
	 int s2 = remW;
	 retV = Math.Max(s1,s2);
      }
      return retV;
   }

   public int[]getSizes(int[]slices){
      winSize = new Dictionary<int,int>();
      int nSlices = slices.Length;
      int totalSz = getSum(slices,0);
      for(int i=nSlices-1;i>=0;i--){
         int sz = getWinSize(slices,i);
	 winSize[i] = sz;
      }
      int bs = winSize[0];
      int az = totalSz-bs;
      int[]retArr = new int[]{az,bs};
      return retArr;
   }
       static int[]strToIntArr(String s){
          //s = s.Substring(1,s.Length-2);
	  string[] strArr = s.Split(new char[]{' '});
	  int len = strArr.Length;
          int[] retArr = new int[len];
	  for(int i=0;i<len;i++){
	     string sVal = strArr[i];
	     int intVal = int.Parse(sVal);
	     retArr[i] = intVal;
	  }
          return retArr;
       }
   public static void Main(string[]args){
      Pie p = new Pie();
      string nLn = Console.ReadLine();
      string arrLn= Console.ReadLine();
      int[] arr = strToIntArr(arrLn);
      int[]abSz = p.getSizes(arr);
      Console.WriteLine("{0} {1}",abSz[0],abSz[1]);
   }
};


using System;
using System.IO;
using System.Text;

namespace Pie_Rules
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int n = Next();

            var nn = new int[n];
            for (int i = 0; i < n; i++)
            {
                nn[i] = Next();
            }

            var dp = new int[2];
            int sum = nn[n - 1];
            dp[0] = nn[n - 1];

            for (int i = n - 2; i >= 0; i--)
            {
                var next = new int[2];

                next[0] = Math.Max(nn[i] + dp[1], dp[0]);
                next[1] = sum + nn[i] - Math.Max(nn[i] + sum - dp[0], sum - dp[1]);

                dp = next;
                sum += nn[i];
            }

            writer.Write(dp[1]);
            writer.Write(' ');
            writer.WriteLine(sum - dp[1]);
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CF
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var sr = new InputReader(Console.In))
//			using (var sr = new InputReader(new StreamReader("input.txt")))
            using (var sw = Console.Out)
                //using (var sw = new StreamWriter("output.txt"))
            using (var task = new Task(sr, sw)) {
                task.Solve();
//                Console.ReadKey();
            }
        }
    }

    internal class Task : IDisposable
    {
        private readonly InputReader sr;
        private readonly TextWriter sw;
        private bool isDispose;
        private int n;
        private int[] pies;
        private Dictionary<string, Tuple<long, long>> cache = new Dictionary<string, Tuple<long, long>>();

        public Task(InputReader sr, TextWriter sw)
        {
            this.sr = sr;
            this.sw = sw;
        }

        public void Solve()
        {
            n = sr.NextInt32();
            pies = sr.ReadArrayOfInt32();

            var answ = Solve(0, true, 0L, 0L);
            sw.WriteLine(answ.Item2 + " " + answ.Item1);
        }

        private Tuple<long, long> Solve(int i, bool isBob, long currBob, long currAlice)
        {
            if (i == n) {
                return new Tuple<long, long>(0L, 0L);
            }
            Tuple<long, long> r = null;

            var key = i + " " + isBob;
            if (cache.ContainsKey(key)) {
                r = cache[key];

                return Tuple.Create(currBob + r.Item1, currAlice + r.Item2);
            }

            if (isBob) {
                var r1 = Solve(i + 1, true, currBob, currAlice + pies[i]);
                var r2 = Solve(i + 1, false, currBob + pies[i], currAlice);
                r = r1.Item1 > r2.Item1 ? r1 : r2;
            }
            else {
                var r1 = Solve(i + 1, false, currBob + pies[i], currAlice);
                var r2 = Solve(i + 1, true, currBob, currAlice + pies[i]);

                r = r1.Item2 > r2.Item2 ? r1 : r2;
            }

            cache.Add(key, Tuple.Create(r.Item1 - currBob, r.Item2 - currAlice));

            return r;
        }

        ~Task()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!isDispose) {
                if (disposing) {
                    if(sr != null)
                        sr.Dispose();

                    if(sw != null)
                        sw.Dispose();
                }

                isDispose = true;
            }
        }
    }
}

internal class InputReader : IDisposable
{
    private bool isDispose;
    private readonly TextReader sr;
    private string[] buffer;
    private int seek;

    public InputReader(TextReader stream)
    {
        sr = stream;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public string NextString()
    {
        var result = sr.ReadLine();
        return result;
    }

    public int NextInt32()
    {
        return Int32.Parse(ReadNextToken());
    }

    private string ReadNextToken()
    {
        if (buffer == null || seek == buffer.Length) {
            seek = 0;
            buffer = NextSplitStrings();
        }

        return buffer[seek++];
    }

    public long NextInt64()
    {
        return Int64.Parse(ReadNextToken());
    }

    public int[] ReadArrayOfInt32()
    {
        return ReadArray(Int32.Parse);
    }

    public long[] ReadArrayOfInt64()
    {
        return ReadArray(Int64.Parse);
    }

    public string[] NextSplitStrings()
    {
        return NextString()
            .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
    {
        return NextSplitStrings()
            .Select(s => func(s, CultureInfo.InvariantCulture))
            .ToArray();
    }

    public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
    {
        return
            str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => func(s, CultureInfo.InvariantCulture))
                .ToArray();
    }

    protected void Dispose(bool dispose)
    {
        if (!isDispose)
        {
            if (dispose)
                sr.Close();

            isDispose = true;
        }
    }

    ~InputReader()
    {
        Dispose(false);
    }
}