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

public class MainE {

    private FastScanner in;
    private PrintWriter out;

    long MOD = 998244353L;

    long[] gcd_ex(long a, long b) {
        if (a == 0) {
            return new long[]{0, 1};
        }
        long[] res = gcd_ex(b % a, a);
        long t = res[0];
        res[0] = res[1] - (b / a) * res[0];
        res[1] = t;
        return res;
    }

    long reverse(long a) {
        return (gcd_ex(a, MOD)[0] + 100 * MOD) % MOD;
    }

    long powMod(long a, long b) {
        long res = 1;
        long mul = a;
        while (b > 0) {
            if ((b & 1) > 0) {
                res = (res * mul) % MOD;
            }
            mul = (mul * mul) % MOD;
            b /= 2;
        }
        return res;
    }

    public void solve() {
        long N = in.nextInt();
        long[] combs = new long[(int)(N + 1)];
        combs[0] = 1;
        for (int i = 1; i <= N; i++) {
            long t = combs[i - 1];
            t = (t * (N - i + 1)) % MOD;
            t = (t * reverse(i)) % MOD;
            combs[i] = t;
        }
        long res = 0;
        long p1 = 1;
        long p2 = 1;
        long R = powMod(reverse(3), N);
        for (int i = 1; i <= N; i++) {
            p1 = (p1 * R) % MOD;
            p2 = (p2 * 3) % MOD;
            long t = combs[i] * p1 % MOD;
            t = (t * p2) % MOD;
            if ((i & 1) > 0) {
                res += 2 * t;
            } else {
                res = (res + 2 * MOD - 2 * t) % MOD;
            }
            res %= MOD;
        }
        res = res * powMod(3, N * N) % MOD;
        long i3 = 1;
        for (int i = 0; i < N; i++){
            long mi3 = (MOD - i3) % MOD;
            i3 = (i3 * 3) % MOD;
            long t = combs[i] * ((MOD + powMod(1 + mi3, N) - powMod(mi3, N)) % MOD) % MOD;
            if ((i & 1) > 0) {
                res += 3 * t;
            } else {
                res = (res + 3 * MOD - 3 * t) % MOD;
            }
            res %= MOD;
        }
        System.out.println(res);
    }

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

    class FastScanner {

        BufferedReader br;
        StringTokenizer st;

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

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

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

    public static void main(String[] arg) {
        new MainE().run();
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 *
 * @author ilyakor
 */
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);
    TaskC solver = new TaskC();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskC {

    static final int mod = 998244353;
    static final int base = 3;
    static final int maxn = 1010 * 1000;
    static int[] f = new int[maxn];
    static int[] rf = new int[maxn];

    static {
      f[0] = 1;
      rf[0] = 1;
      for (int i = 1; i < maxn; ++i) {
        f[i] = (int) (((long) f[i - 1] * (long) i) % mod);
        rf[i] = IntegerUtils.pow(f[i], mod - 2, mod);
      }
    }

    public void solve(int testNumber, InputReader in, OutputWriter out) {
      // for (int theta = -18; theta <= 18; ++theta)
      // for (int alpha = -18; alpha <= 18; ++alpha)
      //     for (int beta = -18; beta <= 18; ++beta)
      //         for (int gamma = -18; gamma <= 18; ++gamma)
      //             for (int delta = -18; delta <= 18; ++delta) {
      //                 //if (alpha <= 0) continue;
      //                 int c0 = 3 * theta + alpha * 2 + beta * 1 + gamma * 2 + delta * 0;
      //                 if (c0 != 0) continue;
      //                 int c1 = 81 * theta + alpha * 56 + beta * 49 + gamma * 64 + delta * 36;
      //                 if (c1 != 18) continue;
      //                 int c2 = 19683 * theta + alpha * 16064 + beta * 15625 + gamma * 17576 + delta * 13824;
      //                 if (c2 != 9750) continue;
      //                 System.err.println(theta + " " + alpha + " " + beta + " " + gamma + " " + delta);
      //             }

      // for (int i = 1; i < 5; ++i) {
      //     int val = stupid(i);
      //     int p3 = 1;
      //     for (int j = 0; j < i * i; ++j)
      //         p3 *= base;
      //     System.err.println(val + " " + (p3 - val) + " " + smart(i));
      // }
      // System.err.flush();

      int n = in.nextInt();
      // for (int i = 1; i < 1000; ++i)
      //     System.err.println(i + " " + doSolve(i));
      long res = doSolve(n);
      out.printLine(res);
    }

    public long doSolve(int n) {
      long res = smart(n);
      long p3 = IntegerUtils.pow(3L, (long) n * (long) n, mod);
      res = p3 - res;
      res %= mod;
      res += mod;
      res %= mod;
      return res;
    }

    static long C(int n, int k) {
      if (k < 0 || k > n) {
        return 0;
      }
      long res = f[n] * (long) rf[k];
      res %= mod;
      res *= rf[n - k];
      res %= mod;
      return res;
    }

    long smart(int n) {
      long res = 0;
      long cp = 1;

      for (int k = 0; k <= n; ++k) {
        long cur = C(n, k);
        long cpp = cp - 1;
        if (cpp < 0) {
          cpp += mod;
        }
        cur *= IntegerUtils.pow(cpp, n, mod);
        cur %= mod;
        cp *= base;
        cp %= mod;
        if ((n + k) % 2 == 0) {
          res += cur;
        } else {
          res -= cur;
        }
        res = (res + mod) % mod;
      }
      res %= mod;
      res += mod;
      res %= mod;
      //return res;
      long first = res;
      //res *= 2L;
      // res *= base;
      // res %= mod;
      long zero = IntegerUtils.pow(base, (long) n * (long) n, mod);
      //int zero = IntegerUtils.pow(base, n * n, mod);
      // res += zero;

      long bn = IntegerUtils.pow(base, n, mod);
      bn -= 2;
      bn %= mod;
      bn += mod;
      bn %= mod;
      // long second = IntegerUtils.pow(bn, n, mod);
      //res += 2L * IntegerUtils.pow(bn, n, mod);
      // res += base * IntegerUtils.pow(bn, n, mod);
      // res %= mod;
      // res += mod;
      // res %= mod;

      ++bn;
      bn %= mod;
      bn += mod;
      bn %= mod;
      //res -= 4L * IntegerUtils.pow(bn, n, mod);
      long third = IntegerUtils.pow(bn, n, mod);
      // res -= 2L * base * IntegerUtils.pow(bn, n, mod);
      // res %= mod;
      // res += mod;
      // res %= mod;

      bn -= 2;
      bn %= mod;
      bn += mod;
      bn %= mod;
      long forth = IntegerUtils.pow(bn, n, mod);

      res = zero * 2L + first * 3L - third * 6L + forth * 2L;
      res %= mod;
      res += mod;
      res %= mod;

      return res;
    }

  }

  static class IntegerUtils {

    public static int pow(long x, long y, int mod) {
      x %= mod;
      long res = 1;
      while (y > 0) {
        if (y % 2 == 1) {
          --y;
          res = (res * x) % mod;
        } else {
          y /= 2;
          x = (x * x) % mod;
        }
      }
      return (int) (res % mod);
    }

  }

  static class OutputWriter {

    private final PrintWriter writer;

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

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    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();
    }

  }

  static class InputReader {

    private InputStream stream;
    private byte[] buffer = new byte[10000];
    private int cur;
    private int count;

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

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

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

    public int readSkipSpace() {
      int c;
      do {
        c = read();
      } while (isSpace(c));
      return c;
    }

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

  }
}

import java.io.*;
import java.nio.charset.Charset;
import java.util.Comparator;

public class CFCR493C {
    public static void main(String[] args) throws Exception {
        boolean local = System.getProperty("ONLINE_JUDGE") == null;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("E:\\DATABASE\\TESTCASE\\CFCR493C.in"), new FileOutputStream("E:\\DATABASE\\TESTCASE\\CFCR493C.out"), charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io);

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        static final int MOD = 998244353;

        public Task(FastIO io) {
            this.io = io;
        }

        @Override
        public void run() {
            solve();
        }

        public void solve() {
            int n = io.readInt();

            long sum = 0;

            int[][] p = new int[n + 1][2];
            p[0][0] = 1;
            for (int i = 1; i <= n; i++) {
                p[i][0] = (int) ((long) p[i - 1][0] * i % MOD);
            }
            for (int i = 0; i <= n; i++) {
                p[i][1] = Mathematics.inverse(p[i][0], MOD);
            }

            int[] exp3 = new int[n + 1];
            exp3[0] = 1;
            for (int i = 1; i <= n; i++) {
                exp3[i] = (int) (3L * exp3[i - 1] % MOD);
            }

            for (int i = 1; i <= n; i++) {
                sum += powNegOne(i + 1) * comp(p, n, i) % MOD * exp3[i] % MOD * Mathematics.pow(3, (long) (n - i) * n, MOD) % MOD;
            }
            sum = sum * 2 % MOD;

            for (int i = 0; i < n; i++) {
                int exp3_i = MOD - exp3[i];
                sum += 3L * comp(p, n, i) % MOD * powNegOne(i + 1) % MOD * Mathematics.mod(Mathematics.pow(Mathematics.mod(1 + exp3_i, MOD), n, MOD) - Mathematics.pow(exp3_i, n, MOD), MOD) % MOD;
            }

            io.cache.append(sum % MOD);
        }

        public static long powNegOne(int n) {
            return (n & 1) == 1 ? (MOD - 1) : 1;
        }

        public static long comp(int[][] p, int n, int m) {
            return (long) p[n][0] * p[n - m][1] % MOD * p[m][1] % MOD;
        }
    }

    public static class Mathematics {
        /**
         * Get the greatest common divisor of a and b
         */
        public static int gcd(int a, int b) {
            return a >= b ? gcd0(a, b) : gcd0(b, a);
        }

        private static int gcd0(int a, int b) {
            return b == 0 ? a : gcd0(b, a % b);
        }

        public static int extgcd(int a, int b, int[] coe) {
            return a >= b ? extgcd0(a, b, coe) : extgcd0(b, a, coe);
        }

        private static int extgcd0(int a, int b, int[] coe) {
            if (b == 0) {
                coe[0] = 1;
                coe[1] = 0;
                return a;
            }
            int g = extgcd0(b, a % b, coe);
            int n = coe[0];
            int m = coe[1];
            coe[0] = m;
            coe[1] = n - m * (a / b);
            return g;
        }

        /**
         * Get the greatest common divisor of a and b
         */
        public static long gcd(long a, long b) {
            return a >= b ? gcd0(a, b) : gcd0(b, a);
        }

        private static long gcd0(long a, long b) {
            return b == 0 ? a : gcd0(b, a % b);
        }

        public static long extgcd(long a, long b, long[] coe) {
            return a >= b ? extgcd0(a, b, coe) : extgcd0(b, a, coe);
        }

        private static long extgcd0(long a, long b, long[] coe) {
            if (b == 0) {
                coe[0] = 1;
                coe[1] = 0;
                return a;
            }
            long g = extgcd0(b, a % b, coe);
            long n = coe[0];
            long m = coe[1];
            coe[0] = m;
            coe[1] = n - m * (a / b);
            return g;
        }

        /**
         * Get y where x * y = 1 (% mod)
         */
        public static int inverse(int x, int mod) {
            return pow(x, mod - 2, mod);
        }

        /**
         * Get x^n(% mod)
         */
        public static int pow(int x, long n, int mod) {
            return pow(x, (int) (n % (mod - 1)), mod);
        }

        /**
         * Get x^n(% mod)
         */
        public static int pow(int x, int n, int mod) {
            n = mod(n, mod - 1);
            x = mod(x, mod);
            int bit = 31 - Integer.numberOfLeadingZeros(n);
            long product = 1;
            for (; bit >= 0; bit--) {
                product = product * product % mod;
                if (((1 << bit) & n) != 0) {
                    product = product * x % mod;
                }
            }
            return (int) product;
        }

        /**
         * Get x % mod
         */
        public static int mod(int x, int mod) {
            return x >= 0 ? x % mod : (((x % mod) + mod) % mod);
        }

        /**
         * Get n!/(n-m)!
         */
        public static long permute(int n, int m) {
            return m == 0 ? 1 : n * permute(n - 1, m - 1);
        }

        /**
         * Put all primes less or equal to limit into primes after offset
         */
        public static int eulerSieve(int limit, int[] primes, int offset) {
            boolean[] isComp = new boolean[limit + 1];
            int wpos = offset;
            for (int i = 2; i <= limit; i++) {
                if (!isComp[i]) {
                    primes[wpos++] = i;
                }
                for (int j = offset, until = limit / i; j < wpos && primes[j] <= until; j++) {
                    int pi = primes[j] * i;
                    isComp[pi] = true;
                    if (i % primes[j] == 0) {
                        break;
                    }
                }
            }
            return wpos - offset;
        }

        /**
         * Round x into integer
         */
        public static int intRound(double x) {
            if (x < 0) {
                return -(int) (-x + 0.5);
            }
            return (int) (x + 0.5);
        }

        /**
         * Round x into long
         */
        public static long longRound(double x) {
            if (x < 0) {
                return -(long) (-x + 0.5);
            }
            return (long) (x + 0.5);
        }
    }

    public static class FastIO {
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
        public final StringBuilder cache = new StringBuilder();

        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            long num = readLong();
            if (next != '.') {
                return num;
            }

            next = read();
            long divisor = 1;
            long later = 0;
            while (next >= '0' && next <= '9') {
                divisor = divisor * 10;
                later = later * 10 + next - '0';
                next = read();
            }

            if (num >= 0) {
                return num + (later / (double) divisor);
            } else {
                return num - (later / (double) divisor);
            }
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public void flush() {
            try {
                os.write(cache.toString().getBytes(charset));
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Memory {
        public static <T> void swap(T[] data, int i, int j) {
            T tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
        }

        public static <T> int min(T[] data, int from, int to, Comparator<T> cmp) {
            int m = from;
            for (int i = from + 1; i < to; i++) {
                if (cmp.compare(data[m], data[i]) > 0) {
                    m = i;
                }
            }
            return m;
        }

        public static <T> void move(T[] data, int from, int to, int step) {
            int len = to - from;
            step = len - (step % len + len) % len;
            Object[] buf = new Object[len];
            for (int i = 0; i < len; i++) {
                buf[i] = data[(i + step) % len + from];
            }
            System.arraycopy(buf, 0, data, from, len);
        }
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(3, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, 3);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            long zNN = mod.pow(3, (long) n * n);
            first = mod.subtract(first, zNN);

            long second = 0;
            long zKN = mod.pow(3, 1 - n);
            long zNNKN = mod.mult(zNN, 3);
            for (int k = 1; k <= n; k++) {
                zNNKN = mod.mult(zNNKN, zNinv);
                long x = mod.mult(mod.ncr(n, k), zNNKN, parity(k));
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, zKN), n));
                second = mod.add(second, mod.mult(x, summation));
                zKN = mod.mult(zKN, 3);
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(zNN, answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final ArrayList<Long> factorial = new ArrayList<>();
            final ArrayList<Long> invFactorial = new ArrayList<>();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.add(1L);
                invFactorial.add(1L);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.add(mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.add(fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();
        private static final int Z = 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(Z, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, Z);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            first = mod.subtract(first, mod.pow(Z, (long) n * n));

            long second = 0;
            long zKN = mod.pow(Z, 1 - n);
            for (int k = 1; k <= n; k++) {
                long pow = mod.pow(Z, (long) n * n - (long) n * k + 1);
                long x = mod.mult(mod.ncr(n, k), pow, parity(k));
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, zKN), n));
                second = mod.add(second, mod.mult(x, summation));
                zKN = mod.mult(zKN, Z);
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(mod.pow(Z, (long) n * n), answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final ArrayList<Long> factorial = new ArrayList<>();
            final ArrayList<Long> invFactorial = new ArrayList<>();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.add(1L);
                invFactorial.add(1L);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.add(mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.add(fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }
}

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

public class main {

	static final long K=998244353;

	static long mu(long a, long n) {
		if (n==0) return 1;
		long q=mu(a,n/2);
		if (n%2==0) return q*q%K;
		return q*q%K*a%K;
	}

	static long [] gt= new long[1000005];
	static long [] inv= new long[1000005];
	static long [] rev= new long[1000005];
	static long n;

	static long Combi(long n, long k){
		if (k>n) return 0;
		return gt[(int) n]*rev[(int) k]%K*rev[(int) (n-k) ]%K;
	}

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
		gt[0]=rev[0]=1;
		inv[1]=1;
		for (int i = 2; i <= 1000000; ++i) {
            inv[i] = (K - inv[((int) (K % i))] * (K / i) % K) % K;
        }
		for (int i=1; i<=1000000; i++){
			gt[i]=gt[i-1]*1l*i%K;
			rev[i]=rev[i-1]*inv[i]%K;
		}

		n=in.nextLong();
		long A=mu(3,(n*n)%(K-1));
		long q=(mu(3,n)-3) %K;
		q=mu(q,n);
		A=(A-q)%K;

		long C=0; long t=1;
		if (n%2==0) t=-1;
		for (long x=0 ; x<n; x++) {
			long z=(mu(3,(n*x)%(K-1))*t+K)%K;
			long w=(mu(3,x)-1)%K;
			w=mu(w,n)*(-t);
			z=(z+w)%K;
			long Y=Combi(n,x)*z%K;
			if (x%2==1){
				C=(C+Y)%K;
				//cout<<x<<" "<<Y<<endl;
			}
			else {C=(C-Y)%K;} //cout<<x<<" "<<(K-Y)%K<<endl;}
		}
		long res=(2*A+3*C)%K;
		res=(res+K)%K;
		out.println(res);

        out.close();
    }

    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 long nextLong() {
            return Long.parseLong(next());
        }
		public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();
        private static final int Z = 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(Z, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, Z);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            long zNN = mod.pow(Z, (long) n * n);
            first = mod.subtract(first, zNN);

            long second = 0;
            long zKN = mod.pow(Z, 1 - n);
            long zNNKN = mod.mult(zNN, Z);
            for (int k = 1; k <= n; k++) {
                zNNKN = mod.mult(zNNKN, zNinv);
                long x = mod.mult(mod.ncr(n, k), zNNKN);
                if ((k & 1) == 1)
                    x = mod.negative(x);
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, zKN), n));
                second = mod.add(second, mod.mult(x, summation));
                zKN = mod.mult(zKN, Z);
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(zNN, answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final IntStack factorial = new IntStack();
            final IntStack invFactorial = new IntStack();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.push(1);
                invFactorial.push(1);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.push((int) mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.push((int) fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }

    static class Util {
        public static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        private Util() {
        }

    }

    static class IntStack {
        int[] array;
        int size = 0;

        public IntStack() {
            this(8);
        }

        public IntStack(int capacity) {
            array = new int[capacity];
        }

        public void push(int item) {
            if (size >= array.length)
                array = resize(array);
            array[size++] = item;
        }

        public int size() {
            return size;
        }

        public int get(int i) {
            Util.ASSERT(i < size);
            return array[i];
        }

        private static int[] resize(int[] array) {
            int[] newArray = new int[array.length << 1];
            System.arraycopy(array, 0, newArray, 0, array.length);
            return newArray;
        }

        public String toString() {
            int[] trimmed = new int[size];
            System.arraycopy(array, 0, trimmed, 0, size);
            return Arrays.toString(trimmed);
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();
        private static final int Z = 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(Z, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, Z);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            long zNN = mod.pow(Z, (long) n * n);
            first = mod.subtract(first, zNN);

            long second = 0;
            long zKN = mod.pow(Z, 1 - n);
            long zNNKN = mod.mult(zNN, Z);
            for (int k = 1; k <= n; k++) {
                zNNKN = mod.mult(zNNKN, zNinv);
                long x = mod.mult(mod.ncr(n, k), zNNKN, parity(k));
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, zKN), n));
                second = mod.add(second, mod.mult(x, summation));
                zKN = mod.mult(zKN, Z);
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(zNN, answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final IntStack factorial = new IntStack();
            final IntStack invFactorial = new IntStack();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.push(1);
                invFactorial.push(1);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.push((int) mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.push((int) fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }

    static class Util {
        public static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        private Util() {
        }

    }

    static class IntStack {
        int[] array;
        int size = 0;

        public IntStack() {
            this(8);
        }

        public IntStack(int capacity) {
            array = new int[capacity];
        }

        public void push(int item) {
            if (size >= array.length)
                array = resize(array);
            array[size++] = item;
        }

        public int size() {
            return size;
        }

        public int get(int i) {
            Util.ASSERT(i < size);
            return array[i];
        }

        private static int[] resize(int[] array) {
            int[] newArray = new int[array.length << 1];
            System.arraycopy(array, 0, newArray, 0, array.length);
            return newArray;
        }

        public String toString() {
            int[] trimmed = new int[size];
            System.arraycopy(array, 0, trimmed, 0, size);
            return Arrays.toString(trimmed);
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();
        private static final int Z = 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(Z, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, Z);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            long zNN = mod.pow(Z, (long) n * n);
            first = mod.subtract(first, zNN);

            long second = 0;
            long zKN = mod.pow(Z, 1 - n);
            long zNNKN = mod.mult(zNN, Z);
            for (int k = 1; k <= n; k++) {
                zNNKN = mod.mult(zNNKN, zNinv);
                long x = mod.mult(mod.ncr(n, k), zNNKN, parity(k));
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, zKN), n));
                second = mod.add(second, mod.mult(x, summation));
                zKN = mod.mult(zKN, Z);
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(zNN, answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final ArrayList<Long> factorial = new ArrayList<>();
            final ArrayList<Long> invFactorial = new ArrayList<>();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.add(1L);
                invFactorial.add(1L);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.add(mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.add(fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }
}

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 kessido
 */
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            final long MOD = 998244353;
            long n = in.NextInt();
            long[] inv = MathExtensions.InverseModulo.getInverseModulo((int) n, MOD);
            long[] nChooseI = new long[(int) (n + 1)];
            nChooseI[0] = 1;
            for (int i = 1; i <= n; i++)
                nChooseI[i] = nChooseI[i - 1] * (n - i + 1) % MOD * inv[i] % MOD;
            final long threePowerN = MathExtensions.powerMod(3, n, MOD);
            final long invtThreePowerN = MathExtensions.modInverse_For_M_Prime(threePowerN, MOD);
            final long threePowerNPowerN = MathExtensions.powerMod(threePowerN, n, MOD);
            final long inv3 = MathExtensions.modInverse_For_M_Prime(3, MOD);
            long res = threePowerNPowerN;
            res -= (MathExtensions.powerMod(MathExtensions.powerMod(3, n, MOD) - 3, n, MOD)) % MOD;
            res %= MOD;
            int sign = 1;
            long[] threePowerI = new long[(int) (n + 1)];
            long[] threePowerN_I = new long[(int) (n + 1)];
            long[] threePowerN_IPowerN = new long[(int) (n + 1)];
            threePowerN_I[0] = threePowerN;
            threePowerN_IPowerN[0] = threePowerNPowerN;
            threePowerI[0] = 1;
            for (int i = 1; i <= n; i++) {
                threePowerI[i] = threePowerI[i - 1] * 3;
                while (threePowerI[i] >= MOD) threePowerI[i] -= MOD;
                threePowerN_I[i] = threePowerN_I[i - 1] * inv3 % MOD;
                threePowerN_IPowerN[i] = threePowerN_IPowerN[i - 1] * invtThreePowerN % MOD;
            }
            for (int i = 1; i <= n; i++) {
                long addToRes = 0;
                addToRes = threePowerI[i] - 3;
                addToRes = addToRes * threePowerN_IPowerN[i] % MOD;
                addToRes += 3 * MathExtensions.powerMod(threePowerN_I[i] - 1, n, MOD) % MOD;
                addToRes %= MOD;
                addToRes *= sign * nChooseI[i];
                addToRes %= MOD;
                sign *= -1;
                res = (res + addToRes) % MOD;
            }
            if (res < 0) res += MOD;
            out.println(res);
        }

    }

    static class InputReader {
        BufferedReader reader;
        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(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }

    static class MathExtensions {
        public static long modInverse_For_M_Prime(final long a, final long m) {
            return powerMod(a, m - 2, m);
        }

        public static long powerMod(final long x, final long y, final long m) {
            if (y == 0)
                return 1;

            long p = powerMod(x, y / 2, m) % m;
            p = (p * p) % m;

            if (y % 2 == 0)
                return p;
            else
                return (x * p) % m;
        }

        public static class InverseModulo {
            public static long[] getInverseModulo(final int max, final long MOD) {
                long[] inv = new long[max + 1];
                if (max == 0) return inv;
                inv[1] = 1;
                for (int i = 2; i <= max; i++) {
                    inv[i] = (MOD - MOD / i) * inv[(int) (MOD % i)] % MOD;
                }
                return inv;
            }

        }

    }
}

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 SkyFullOfStars2 {

    int N = (int) 1e6 + 10;
    int MOD = 998244353;

    long[] fac = new long[N], ifac = new long[N], inv = new long[N];

    {
        inv[1] = 1;
        for (int i = 2; i < N; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;

        fac[0] = fac[1] = 1;
        ifac[0] = ifac[1] = 1;
        for (int i = 2; i < N; i++) {
            fac[i] = fac[i - 1] * i % MOD;
            ifac[i] = ifac[i - 1] * inv[i] % MOD;
        }
    }

    int n;

    void solve() {
        n = in.nextInt();

        long[] c = new long[n + 1];
        c[0] = 1;
        for (int i = 1; i <= n; i++) c[i] = fac[n] * ifac[i] % MOD * ifac[n - i] % MOD;

        long ans = 0;
        for (int i = 0; i < n; i++) {
            long delta = c[i] * (pow((1 - pow(3, i) + MOD) % MOD, n) - pow((MOD - pow(3, i)) % MOD, n) + MOD) % MOD;
            if ((i + 1) % 2 == 0) ans = (ans + delta) % MOD;
            else ans = (ans - delta + MOD) % MOD;
        }
        ans = 3 * ans % MOD;

        for (int i = 1; i <= n; i++) {
            long delta = 2 * c[i] % MOD * pow(3, i + (long) n * (n - i)) % MOD;
            if (i % 2 == 1) ans = (ans + delta) % MOD;
            else ans = (ans - delta + MOD) % MOD;
        }

        out.println(ans);
    }

    long pow(long a, long n) {
        long res = 1;
        while (n > 0) {
            if ((n & 1) > 0) res = res * a % MOD;
            a = a * a % MOD;
            n >>= 1;
        }
        return res;
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new SkyFullOfStars2().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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();
        private static final int Z = 3;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long first = 0;
            long zN = mod.pow(Z, n);
            long zK = 1;
            long zNNK = mod.pow(zN, n);
            long zNinv = mod.inv(zN);
            for (int k = 0; k <= n; k++) {
                long term = mod.mult(mod.ncr(n, k), zK, zNNK, parity(k));
                first = mod.add(first, term);
                zK = mod.mult(zK, Z);
                zNNK = mod.mult(zNNK, zNinv);
            }
            first = mod.mult(first, 2);
            first = mod.subtract(first, mod.pow(Z, (long) n * n));

            long second = 0;
            for (int k = 1; k <= n; k++) {
                long x = mod.mult(mod.ncr(n, k), mod.pow(Z, (long) n * n - (long) n * k + 1), parity(k));
                long summation = mod.add(mod.negative(1), mod.pow(mod.subtract(1, mod.pow(Z, k - n)), n));
                second = mod.add(second, mod.mult(x, summation));
            }

            long answer = mod.add(first, second);
            answer = mod.subtract(mod.pow(Z, (long) n * n), answer);

            out.println(answer);
        }

        private long parity(int k) {
            return (k & 1) == 0 ? 1 : mod.negative(1);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final ArrayList<Long> factorial = new ArrayList<>();
            final ArrayList<Long> invFactorial = new ArrayList<>();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.add(1L);
                invFactorial.add(1L);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.add(mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.add(fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long subtract(long a, long b) {
                long v = a - b;
                return v < 0 ? v + modulus() : v;
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long negative(long x) {
                return x == 0 ? 0 : modulus() - x;
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }
}

//package com.company;

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

public class Main {
    public static class Task {
        int mod = 998244353;
        public int add(int a, int b) {
            int c = a + b;
            if (c < 0) return c + mod;
            if (c >= mod) return c - mod;
            return c;
        }
        public int mult(int a, int b) {
            long c = (long) a * b;
            return (int) (c % mod);
        }
        public int add(int... a) {
            int x = 0;
            for (int b: a) x = add(x, b);
            return x;
        }
        public int mult(int... a) {
            int x = 1;
            for (int b: a) x = mult(x, b);
            return x;
        }
        int[] factorial;
        int[] invFactorial;
        public void precompute() {
            factorial = new int[1_000_001];
            factorial[0] = 1;
            for (int i = 1; i < factorial.length; i++) {
                factorial[i] = mult(factorial[i - 1], i);
            }
            invFactorial = new int[factorial.length];
            for (int i = 0; i < invFactorial.length; i++) {
                invFactorial[i] = power(factorial[i], mod - 2);
            }
        }

        public int power(int a, int b) {
            int res = 1;
            while (b != 0) {
                if ((b & 1) != 0) res = mult(res, a);
                a = mult(a, a);
                b >>= 1;
            }
            return res;
        }

        public int nChoosek(int n, int k) {
            return mult(factorial[n], invFactorial[k], invFactorial[n - k]);
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            precompute();
            int n = sc.nextInt();
            int res0 = 0;
            {
                int pow = (int) ((long) n * n % (mod - 1));
                pow = power(3, pow);
                int inc = power(3, mod - n); // 3 ** (1 - n)
                for (int i = 1; i <= n; i++) {
                    pow = mult(pow, inc);
                    int sub = mult(nChoosek(n, i), pow);
                    if (i % 2 == 1) {
                        res0 = add(res0, sub);
                    } else {
                        res0 = add(res0, -sub);
                    }
                }
            }
            res0 = mult(res0, 2);
            int res1 = 0;
            {
                int pow3 = 1;
                for (int i = 0; i < n; i++) {
                    int pow1 = power(add(1, -pow3), n);
                    int pow2 = power(add(0, -pow3), n);
                    int sub = mult(nChoosek(n, i), add(pow1, -pow2));
                    if (i % 2 == 1) {
                        res1 = add(res1, sub);
                    } else {
                        res1 = add(res1, -sub);
                    }
                    pow3 = mult(pow3, 3);
                }
            }
            res1 = mult(res1, 3);
            pw.println(add(res0, res1) % mod);
        }
    }

    // template, actual code is in class Task.
    static long TIME_START, TIME_END;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("input"));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("output"));

        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
        System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
        System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    // Faster IO with BufferedReader wrapped with Scanner
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader s) throws FileNotFoundException {
            br = new BufferedReader(s);
        }

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

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

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

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

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

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

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

public class Codeforces998E {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());

		int mod = 998244353;
		int phimod = mod-1;

		int[] factorial = new int[n+1];
		factorial[0] = 1;
		for (int i = 1; i <= n; i++) {
			factorial[i] = (int) (((long) factorial[i-1] * i)%mod);
		}

		int[] choose = new int[n+1];
		for (int i = 0; i <= n; i++) {
			int r = inverse(factorial[i], mod);
			int s = inverse(factorial[n-i], mod);
			int rs = (int) (((long) r * (long) s)%mod);
			choose[i] = (int) (((long) rs * (long) factorial[n])%mod);
		}

		int sum1 = 0;
		int threepower = 1;
		for (int i = 0; i <= n; i++) {
			int u = (mod+1 - threepower)%mod;
			int upower = power(u, n, mod);
			int summand = (int) (((long) choose[i] * (long) upower)%mod);
			if (i%2 == 0) {
				summand = mod-summand;
			}
			sum1 = (sum1+summand)%mod;

			threepower = (int) ((3 * (long) threepower)%mod);
		}
		sum1 = (int) ((3 * (long) sum1)%mod);

		int sum2 = power(power(3, n, mod)-1, n, mod);
		sum2 = (2*sum2)%mod;
		sum2 = (sum2+mod-power(3, power(n, 2, phimod), mod))%mod;
		sum2 = (int) ((3 * (long) sum2)%mod);

		int sum3 = power(power(3, n-1, mod)-1, n, mod);
		int aaa = (int) (((long) n * (long) (n-1))%phimod);
		sum3 = (mod + power(3, aaa, mod) - sum3)%mod;
		sum3 = (2*sum3)%mod;
		sum3 = (int) ((long) sum3 * (long) power(3, n, mod)%mod);

		int sum = sum1;
		sum = (sum+sum2)%mod;
		sum = (sum+sum3)%mod;

		System.out.println(sum);
	}

	public static int power(int base, int exp, int mod) {
		long a = base%mod;
		if (exp == 0) {
			return 1;
		}
		else {
			a = (a*a)%mod;
			int b = (int) a;
			int almostanswer = power(b, exp/2, mod);
			if (exp%2 == 0) {
				return almostanswer;
			}
			else {
				long val = (long) almostanswer * (long) base;
				long val2 = val%mod;
				return (int) val2;
			}
		}
	}

	public static int inverse (int a, int n) {
		int m = n;
		int r1 = 1;
		int r2 = 0;
		int r3 = 0;
		int r4 = 1;
		while ((a > 0) && (n > 0)) {
			if (n >= a) {
				r3 -= r1*(n/a);
				r4 -= r2*(n/a);
				n = n%a;
			}
			else {
				int tmp = a;
				a = n;
				n = tmp;
				tmp = r1;
				r1 = r3;
				r3 = tmp;
				tmp = r2;
				r2 = r4;
				r4 = tmp;
			}
		}
		if (a == 0) {
			if (r3 >= 0)
				return (r3%m);
			else
				return (m+(r3%m));
		}
		else {
			if (r1 >= 0)
				return (r1%m);
			else
				return (m+(r1%m));
		}

	}
}

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

public class Main
{
	static long mod = 998244353;

	public static void main(String[] args)
	{
		FastReader reader = new FastReader();
		PrintWriter writer = new PrintWriter(System.out);

		long n = reader.nextLong();
		long[] factorial = new long[(int)n+1];
		long[] invf = new long[(int)n+1];
		factorial[0] = 1;
		for (int i=1; i<=n; i++)
			factorial[i] = (factorial[i-1]*i)%mod;
		invf[(int)n]=inverse(factorial[(int)n]);
		for(int i=(int)n;i>=1;--i)
		    invf[i-1]=invf[i]*i%mod;

		long ans = power(3, n*n);
		ans -= power((power(3, n) - 3 + mod)%mod, n);
		ans += mod;
		ans %= mod;
		long i3=inverse(3),t=1,u=power(3,n),
		ru=inverse(u),p=power(u,n);

		for (int i=1; i<=n; i++)
		{
			long temp = factorial[(int)n];
			temp *= invf[i];
			temp %= mod;
			temp *= invf[(int)n-i];
			temp %= mod;
			t=t*3%mod; u=u*i3%mod;

			if (i%2 == 1)
				temp = mod - temp;
			p=p*ru%mod;

			long a = 3*power((u - 1 + mod)%mod, n);
			a %= mod;

			long b = ((t - 3 + mod)%mod) * p;
			b %= mod;

			ans -= (temp * (a + b))%mod;
			ans += mod;
			ans %= mod;
		}

		writer.println(ans);
		writer.close();
	}

	static long inverse (long a)
	{
		return power(a, mod-2);
	}

	static long power (long a, long p)
	{
		if (p==0)
			return 1;
		else
		{
			long got = power(a, p/2);
			got = got*got;

			got %= mod;

			if (p%2 == 1)
				got *= a;

			return got%mod;
		}
	}

	static class FastReader
	{
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

		String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
	}
}

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

public class Main
{
	static long mod = 998244353;

	public static void main(String[] args)
	{
		FastReader reader = new FastReader();
		PrintWriter writer = new PrintWriter(System.out);

		long n = reader.nextLong();
		long[] factorial = new long[(int)n+1];
		factorial[0] = 1;
		for (int i=1; i<=n; i++)
			factorial[i] = (factorial[i-1]*i)%mod;

		long[] invFactorial = new long[(int)n+1];
		invFactorial[(int)n] = inverse(factorial[(int)n]);
		for (int i=(int)n-1; i>=0; i--)
			invFactorial[i] = (invFactorial[i+1]*(i+1))%mod;

		long ans = power(3, n*n);
		ans -= power((power(3, n) - 3 + mod)%mod, n);
		ans += mod;
		ans %= mod;

		long u = power(3, n);
		long v = 1;
		long w = power(3, n*n);
		long i3 = inverse(3);
		long ru = inverse(u);

		for (int i=1; i<=n; i++)
		{
			long temp = factorial[(int)n];
			temp *= invFactorial[i];
			temp %= mod;
			temp *= invFactorial[(int)n - i];
			temp %= mod;

			if (i%2 == 1)
				temp = mod - temp;

			u *= i3;
			u %= mod;

			v *= 3;
			v %= mod;

			w *= ru;
			w %= mod;

			long a = 3*power((u - 1 + mod)%mod, n);
			a %= mod;

			long b = ((v - 3 + mod)%mod) * w;
			b %= mod;

			ans -= (temp * (a + b))%mod;
			ans += mod;
			ans %= mod;
		}

		writer.println(ans);
		writer.close();
	}

	static long inverse (long a)
	{
		return power(a, mod-2);
	}

	static long power (long a, long p)
	{
		if (p==0)
			return 1;
		else
		{
			long got = power(a, p/2);
			got = got*got;

			got %= mod;

			if (p%2 == 1)
				got *= a;

			return got%mod;
		}
	}

	static class FastReader
	{
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

		String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
	}
}

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

public class MainE {

    private FastScanner in;
    private PrintWriter out;

    long MOD = 998244353L;

    long reverse(long a) {
        return powMod(a, MOD - 2); // works only if M prime
    }

    long powMod(long a, long b) {
        long res = 1;
        long mul = a;
        while (b > 0) {
            if ((b & 1) > 0) {
                res = (res * mul) % MOD;
            }
            mul = (mul * mul) % MOD;
            b /= 2;
        }
        return res;
    }

    public void solve() {
        long N = in.nextInt();
        long[] combs = new long[(int)(N + 1)];
        combs[0] = 1;
        for (int i = 1; i <= N; i++) {
            long t = combs[i - 1];
            t = (t * (N - i + 1)) % MOD;
            t = (t * reverse(i)) % MOD;
            combs[i] = t;
        }
        long res = 0;
        long p1 = 1;
        long p2 = 1;
        long R = powMod(reverse(3), N);
        for (int i = 1; i <= N; i++) {
            p1 = (p1 * R) % MOD;
            p2 = (p2 * 3) % MOD;
            long t = combs[i] * p1 % MOD;
            t = (t * p2) % MOD;
            if ((i & 1) > 0) {
                res += 2 * t;
            } else {
                res = (res + 2 * MOD - 2 * t) % MOD;
            }
            res %= MOD;
        }
        res = res * powMod(3, N * N) % MOD;
        long i3 = 1;
        for (int i = 0; i < N; i++){
            long mi3 = (MOD - i3) % MOD;
            i3 = (i3 * 3) % MOD;
            long t = combs[i] * ((MOD + powMod(1 + mi3, N) - powMod(mi3, N)) % MOD) % MOD;
            if ((i & 1) > 0) {
                res += 3 * t;
            } else {
                res = (res + 3 * MOD - 3 * t) % MOD;
            }
            res %= MOD;
        }
        System.out.println(res);
    }

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

    class FastScanner {

        BufferedReader br;
        StringTokenizer st;

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

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

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

    public static void main(String[] arg) {
        new MainE().run();
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author prakharjain
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        int mod = 998244353;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            long n = in.nextInt();

            long ans = 0;

            long[] _3pow = new long[(int) n + 1];

            _3pow[0] = 1;

            for (int i = 1; i <= n; i++) {
                _3pow[i] = 3 * _3pow[i - 1];
                _3pow[i] %= mod;
            }

            long[] ncr = new long[(int) n + 1];

            ncr[0] = 1;

            for (int i = 1; i <= n; i++) {
                ncr[i] = ncr[i - 1] * (n - (i - 1));
                ncr[i] %= mod;
                ncr[i] *= pow(i, mod - 2, mod);
                ncr[i] %= mod;
            }

            long _3inv = pow(pow(3, n - 1, mod), mod - 2, mod);

            long pv = pow(3, n * n, mod);
            for (int i = 1; i <= n; i++) {
                //long val = _3pow[i] * pow(3, (n * (n - i)), mod);
                long val = pv * _3inv;
                val %= mod;
                pv = val;
                val *= ncr[i];
                val %= mod;
                if (i % 2 == 0)
                    ans -= 2 * val;
                else
                    ans += 2 * val;
                ans %= mod;
            }

            for (int i = 0; i < n; i++) {
                long val1 = 3 * ncr[i];
                long val2 = 1 - _3pow[i];
                long val3 = pow(val2, n, mod);
                val3 -= pow(-_3pow[i], n, mod);
                val1 *= val3;
                val1 %= mod;
                if (i % 2 == 0)
                    ans -= val1;
                else
                    ans += val1;
                ans %= mod;
            }

            ans += mod;
            ans %= mod;
            out.println(ans);
        }

        long pow(long a, long p, int mod) {
            if (p == 0) {
                return 1;
            }

            long t = pow(a, p / 2, mod);

            if (p % 2 != 0) {
                return (((t * t) % mod) * a) % mod;
            } else {
                return (t * t) % mod;
            }
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

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

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

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

        public void println(long i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

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

        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 nextInt() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        }

    }
}

//package round493;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C4 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	public static long invl(long a, long mod) {
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		return p < 0 ? p + mod : p;
	}

	public static long C(int n, int r, int mod, int[][] fif) {
		if (n < 0 || r < 0 || r > n)
			return 0;
		return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod;
	}

	public static long[] enumPows(long a, int n, int mod)
	{
		a %= mod;
		long[] pows = new long[n+1];
		pows[0] = 1;
		for(int i = 1;i <= n;i++)pows[i] = pows[i-1] * a % mod;
		return pows;
	}

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

		int mod = 998244353;
		int[][] fif = enumFIF(1000005, mod);
		long u = 0;
		long i3 = invl(3, mod);

		long n3 = pow(3, n, mod);
		long[] p3n = enumPows(n3, n+1, mod);
		long[] pi3 = enumPows(i3, n+1, mod);

		for(int s = 0, sgn = 1;s <= n;s++, sgn = -sgn){
			u += C(n,s, mod, fif)*sgn*p3n[n-s] % mod * pow(1-pi3[n-s], n, mod);
			u %= mod;
		}
		u = u * 3 % mod;

		// n^2-in+1
		long[] pi3nm = enumPows(pow(i3, n-1, mod), n+1, mod);
		long b = pow(3, (long)n*n, mod);
		long[] pi3n = enumPows(pow(i3, n, mod), n+1, mod);
		// i+(n-i)n = n^2-i(n-1)
		for(int i = 1;i <= n;i++){
			long co = (i % 2 == 0 ? 1 : -1) * C(n, i, mod, fif);
			long lv = (b*6*pi3n[i] - b*pi3nm[i]*2) % mod;
			u -= co * lv;
			u %= mod;
		}
		u -= pow(3, (long)n*n, mod) * 2;

		u %= mod;
		long ans = pow(3, (long)n*n, mod) - u;
		ans %= mod;
		if(ans < 0)ans += mod;
		out.println(ans);
	}

	public static int[][] enumFIF(int n, int mod) {
		int[] f = new int[n + 1];
		int[] invf = new int[n + 1];
		f[0] = 1;
		for (int i = 1; i <= n; i++) {
			f[i] = (int) ((long) f[i - 1] * i % mod);
		}
		long a = f[n];
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		invf[n] = (int) (p < 0 ? p + mod : p);
		for (int i = n - 1; i >= 0; i--) {
			invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod);
		}
		return new int[][] { f, invf };
	}

	public static long pow(long a, long n, long mod) {
		a %= mod;
		if(a < 0)a += mod;
		long ret = 1;
		int x = 63 - Long.numberOfLeadingZeros(n);
		for (; x >= 0; x--) {
			ret = ret * ret % mod;
			if (n << 63 - x < 0)
				ret = ret * a % mod;
		}
		return ret;
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		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 C4().run(); }

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}

import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;
import java.util.Arrays;
import java.util.Stack;
import java.util.TreeMap;
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;
//maintain: array on vertices
//a[i]:
public class Test12   {
    static long p = 998244353;
    static long n = 0;
    static long m = 0;
    public static long raise(long a, long n)   {
        if (a == 0) return 0;
        long pw = a;
        long ans = 1;
        m = n;
        while (m > 0)  {
            if (m % 2 == 1) ans = (ans * pw) % p;
            m = m / 2;
            pw = (pw * pw) % p;
        }
        return ans;
    }
    public static void main(String[] Args)   {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int n0 = (int)n;
        //power of 3
        long[] pow3 = new long[n0 + 1];
        pow3[0] = 1;
        for (long i = 1; i <= n; i++) pow3[(int)i] = (pow3[(int)(i -1)] * 3) % p;
        long[] fact = new long[n0 + 1];
        fact[0] = 1;
        for (long i = 1; i <= n; i++) fact[(int)i] = (fact[(int)(i -1)] * i) % p;
        long[] factrev = new long[n0 + 1];
        for (long i = 0; i <= n; i++) factrev[(int)i] = raise(fact[(int)i], p - 2);
        long ans = 0;
        long sgn = 1;
        for (int k = 0; k < n0; k++)   {
            ans += 3 * ((sgn * ((((fact[n0] * factrev[(n0 -k)]) % p) * factrev[k]) % p) * (raise(pow3[k] - 1,n) - raise(pow3[k], n)))% p);
            sgn = - sgn;
            ans = ans % p;
        }
        ans = ans * sgn;
        ans += 2 * ((pow3[n0] * raise(pow3[n0 - 1] - 1, n))%p - raise(pow3[n0],n));
        ans = ans % p;
        System.out.println((p - ans) % p);
    }
}

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

public class Main {

    void solve() {
        int n=ni();
        precompute(n);
        long ans=(2*f3(n))%M;
        long sm=0;
        for(int i=0;i<n;i++){

            sm=add(sm,f2(i,n),M);
        }
        sm=mul(sm,3,M);
        ans=add(ans,sm,M);
        pw.println(ans);

    }
    long f3(long n){
        if(n==1) return 3;
        long ans=modpow(3,n*n,M);
        long b=modpow((pow[(int)n]-3+M)%M ,n,M);
        ans=sub(ans,b,M);
        return ans;
    }
    long f2(int i,int n){
        long ans=ncr(n,i);
        if(i%2==0) ans*=-1;
        if(ans<0) ans+=M;
        ans=mul(ans,f(i,n),M);
        return ans;
    }
    long f(int i,int n){
        if(i==0){
            if(n%2==0) return -1;
            else return 1;
        }
        long t=modpow(pow[i],n,M);
        long a=t;
        if(n%2==0) a*=-1;
        if(a<0) a+=M;
        long b=(1-pow[i]+M)%M;
        b=modpow(b,n,M);
        long ans=add(a,b,M);
        return ans;
    }
    long ncr(int n,int r){
        long ans=fact[n];
        ans=mul(ans,inv[r],M);
        ans=mul(ans,inv[n-r],M);
        return ans;
    }
    long fact[];
    long pow[];
    long inv[];
    void precompute(int n){
        fact=new long[n+1];
        pow=new long[n+1];
        inv=new long[n+1];
        pow[0]=1;
        fact[0]=1;
        for(int i=1;i<=n;i++) {
            pow[i]=(pow[i-1]*3)%M;
            fact[i]=(fact[i-1]*i)%M;
        }
        inv[n]=modInverse(fact[n],M);
        for(int i=n-1;i>=0;i--){
            inv[i]=(inv[i+1]*(i+1))%M;
        }

    }
    long add(long x, long y,long M) {
        x += y;
        if (x < 0) x+=M;
        if (x >= M) x -=M;
        return x;
    }
    long sub(long x, long y,long M) {
        x -= y;
        if (x < 0) x+=M;
        if (x >= M) x -=M;
        return x;
    }
    long mul(long a, long b,long M)
    {
        return (a*1L*b)%M;
    }
    long modpow(long a, long b,long M)
    {
        long r=1;
        while(b>0)
        {
            if((b&1)>0) r=mul(r,a,M);
            a=mul(a,a,M);
            b>>=1;
        }
        return r;
    }

    long modInverse(long A, long M)
    {

        return modpow(A,M-2,M);
    }

    long M=(long)998244353;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";
    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }

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

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }

        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
}

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;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        static final long MODULO = 998244353;

        long pow(long a, long k) {
            long res = 1;
            while (k > 0) {
                if ((k & 1) != 0) res = res * a % MODULO;
                a = a * a % MODULO;
                k >>= 1;
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextInt();
            long[] invs = new long[(int) (n + 1)];
            invs[1] = 1;
            for (int i = 2; i <= n; ++i) {
                invs[i] = (MODULO - invs[((int) (MODULO % i))] * (MODULO / i) % MODULO) % MODULO;
                if (invs[i] * i % MODULO != 1) throw new RuntimeException();
            }
            long[] fact = new long[invs.length];
            long[] invfact = new long[invs.length];
            fact[0] = 1;
            invfact[0] = 1;
            for (int i = 1; i < invs.length; ++i) {
                fact[i] = i * fact[i - 1] % MODULO;
                invfact[i] = invs[i] * invfact[i - 1] % MODULO;
            }
            long[] comb = new long[(int) (n + 1)];
            for (int a = 0; a <= n; ++a) {
                comb[a] = fact[(int) n] % MODULO * invfact[a] % MODULO * invfact[(int) (n - a)] % MODULO;
            }
            long res = 0;
            for (int a = 1; a <= n; ++a) {
                long z = 2 * pow(3, n * (n - a) + a) * comb[a] % MODULO;
                if (a % 2 == 0) {
                    res -= z;
                } else {
                    res += z;
                }
            }
            for (int a = 1; a <= n; ++a) {
                long u = pow(3, (n - a));
                long m = 3 * comb[a] % MODULO;
                long s = pow(1 - u, n) - pow(-u, n);
                s %= MODULO;
                s *= m;
                s %= MODULO;
                if ((n - a) % 2 == 0)
                    res -= s;
                else
                    res += s;
            }
            res %= MODULO;
            res += MODULO;
            res %= MODULO;
            out.println(res);
        }

    }

    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());
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 *
 * @author ilyakor
 */
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);
    TaskC solver = new TaskC();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskC {

    static final int mod = 998244353;
    static final int base = 3;
    static final int maxn = 1010 * 1000;
    static int[] f = new int[maxn];
    static int[] rf = new int[maxn];

    static {
      f[0] = 1;
      rf[0] = 1;
      for (int i = 1; i < maxn; ++i) {
        f[i] = (int) (((long) f[i - 1] * (long) i) % mod);
        rf[i] = IntegerUtils.pow(f[i], mod - 2, mod);
      }
    }

    public void solve(int testNumber, InputReader in, OutputWriter out) {
      // for (int theta = -9; theta <= 9; ++theta)
      // for (int alpha = -9; alpha <= 9; ++alpha)
      //     for (int beta = -12; beta <= 12; ++beta)
      //         for (int gamma = -12; gamma <= 12; ++gamma)
      //             for (int delta = -12; delta <= 12; ++delta) {
      //                 //if (alpha <= 0) continue;
      //                 int c0 = 3 * theta + alpha * 2 + beta * 1 + gamma * 2 + delta * 0;
      //                 if (c0 != 0) continue;
      //                 int c1 = 81 * theta + alpha * 56 + beta * 49 + gamma * 64 + delta * 36;
      //                 if (c1 != 18) continue;
      //                 int c2 = 19683 * theta + alpha * 16064 + beta * 15625 + gamma * 17576 + delta * 13824;
      //                 if (c2 != 9750) continue;
      //                 System.err.println(theta + " " + alpha + " " + beta + " " + gamma + " " + delta);
      //             }

      // for (int i = 1; i < 5; ++i) {
      //     int val = stupid(i);
      //     int p3 = 1;
      //     for (int j = 0; j < i * i; ++j)
      //         p3 *= base;
      //     System.err.println(val + " " + (p3 - val) + " " + smart(i));
      // }
      // System.err.flush();

      int n = in.nextInt();
      // for (int i = 1; i < 1000; ++i)
      //     System.err.println(i + " " + doSolve(i));
      long res = doSolve(n);
      out.printLine(res);
    }

    public long doSolve(int n) {
      long res = smart(n);
      long p3 = IntegerUtils.pow(3L, (long) n * (long) n, mod);
      res = p3 - res;
      res %= mod;
      res += mod;
      res %= mod;
      return res;
    }

    static long C(int n, int k) {
      if (k < 0 || k > n) {
        return 0;
      }
      long res = f[n] * (long) rf[k];
      res %= mod;
      res *= rf[n - k];
      res %= mod;
      return res;
    }

    long smart(int n) {
      long res = 0;
      long cp = 1;

      for (int k = 0; k <= n; ++k) {
        long cur = C(n, k);
        long cpp = cp - 1;
        if (cpp < 0) {
          cpp += mod;
        }
        cur *= IntegerUtils.pow(cpp, n, mod);
        cur %= mod;
        cp *= base;
        cp %= mod;
        if ((n + k) % 2 == 0) {
          res += cur;
        } else {
          res -= cur;
        }
      }
      res %= mod;
      res += mod;
      res %= mod;
      //return res;
      long first = res;
      //res *= 2L;
      // res *= base;
      // res %= mod;
      int zero = IntegerUtils.pow(base, n * n, mod);
      // res += zero;

      long bn = IntegerUtils.pow(base, n, mod);
      bn -= 2;
      bn %= mod;
      bn += mod;
      bn %= mod;
      // long second = IntegerUtils.pow(bn, n, mod);
      //res += 2L * IntegerUtils.pow(bn, n, mod);
      // res += base * IntegerUtils.pow(bn, n, mod);
      // res %= mod;
      // res += mod;
      // res %= mod;

      ++bn;
      bn %= mod;
      bn += mod;
      bn %= mod;
      //res -= 4L * IntegerUtils.pow(bn, n, mod);
      long third = IntegerUtils.pow(bn, n, mod);
      // res -= 2L * base * IntegerUtils.pow(bn, n, mod);
      // res %= mod;
      // res += mod;
      // res %= mod;

      bn -= 2;
      bn %= mod;
      bn += mod;
      bn %= mod;
      long forth = IntegerUtils.pow(bn, n, mod);

      res = zero * 2L + first * 3L - third * 6L + forth * 2L;
      res %= mod;
      res += mod;
      res %= mod;

      return res;
    }

  }

  static class IntegerUtils {

    public static int pow(long x, long y, int mod) {
      x %= mod;
      long res = 1;
      while (y > 0) {
        if (y % 2 == 1) {
          --y;
          res = (res * x) % mod;
        } else {
          y /= 2;
          x = (x * x) % mod;
        }
      }
      return (int) (res % mod);
    }

  }

  static class OutputWriter {

    private final PrintWriter writer;

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

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    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();
    }

  }

  static class InputReader {

    private InputStream stream;
    private byte[] buffer = new byte[10000];
    private int cur;
    private int count;

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

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

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

    public int readSkipSpace() {
      int c;
      do {
        c = read();
      } while (isSpace(c));
      return c;
    }

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

  }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        NumberTheory.Mod998 mod = new NumberTheory.Mod998();

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            long answer = 0;

            for (int k = 1; k <= n; k++) {
                for (int i = 0; i <= k; i++) {
                    long t = i == 0 || i == k ? mod.pow(3, k) : 3;
                    long sign = (k & 1) > 0 ? 1 : mod.modulus() - 1;
                    long term = mod.mult(t, sign, mod.ncr(n, i), mod.ncr(n, k - i), mod.pow(3, (long) (n - i) * (n - (k - i))));
                    // System.out.println(Pii.of(k, i) + " " + "term = " + (term < 1e6 ? "" + term : "-" + mod.negative(term)));
                    answer = mod.add(answer, term);
                }
            }

            for (int k = n + 1; k <= 2 * n; k++) {
                for (int i = k - n; i <= n; i++) {
                    long t = 3;
                    long sign = (k & 1) > 0 ? 1 : mod.modulus() - 1;
                    long term = mod.mult(t, sign, mod.ncr(n, i), mod.ncr(n, k - i), mod.pow(3, n - Math.max(i, k - i)));
                    // System.out.println(Pii.of(k, i) + " " + "term = " + (term < 1e6 ? "" + term : "-" + mod.negative(term)));
                    answer = mod.add(answer, term);
                }
            }

            out.println(answer);
        }

    }

    static class InputReader {
        public final 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());
        }

    }

    static class NumberTheory {
        private static void ASSERT(boolean assertion) {
            if (!assertion)
                throw new AssertionError();
        }

        public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
            final ArrayList<Long> factorial = new ArrayList<>();
            final ArrayList<Long> invFactorial = new ArrayList<>();

            public abstract long modulus();

            public Modulus() {
                super();
                factorial.add(1L);
                invFactorial.add(1L);
            }

            public long fact(int n) {
                while (factorial.size() <= n) {
                    factorial.add(mult(factorial.get(factorial.size() - 1), factorial.size()));
                }

                return factorial.get(n);
            }

            public long fInv(int n) {
                int lastKnown = invFactorial.size() - 1;

                if (lastKnown < n) {
                    long[] fInv = new long[n - lastKnown];
                    fInv[0] = inv(fact(n));
                    for (int i = 1; i < fInv.length; i++) {
                        fInv[i] = mult(fInv[i - 1], n - i + 1);
                    }
                    for (int i = fInv.length - 1; i >= 0; i--) {
                        invFactorial.add(fInv[i]);
                    }
                }

                return invFactorial.get(n);
            }

            public long ncr(int n, int r) {
                ASSERT(n >= 0);
                if (r < 0 || n < r)
                    return 0;
                return mult(fact(n), mult(fInv(r), fInv(n - r)));
            }

            public long normalize(long x) {
                x %= modulus();
                if (x < 0)
                    x += modulus();
                return x;
            }

            public long add(long a, long b) {
                long v = a + b;
                return v < modulus() ? v : v - modulus();
            }

            public long mult(long... x) {
                long r = 1;
                for (long i : x)
                    r = mult(r, i);
                return r;
            }

            public long mult(long a, long b) {
                return (a * b) % modulus();
            }

            public long pow(long x, int e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long pow(long x, long e) {
                if (e < 0) {
                    x = inv(x);
                    e *= -1;
                }
                if (e == 0)
                    return 1;
                if ((e & 1) > 0)
                    return mult(x, pow(x, e - 1));
                return pow(mult(x, x), e / 2);
            }

            public long inv(long value) {
                long g = modulus(), x = 0, y = 1;
                for (long r = value; r != 0; ) {
                    long q = g / r;
                    g %= r;

                    long temp = g;
                    g = r;
                    r = temp;

                    x -= q * y;

                    temp = x;
                    x = y;
                    y = temp;
                }

                ASSERT(g == 1);
                ASSERT(y == modulus() || y == -modulus());

                return normalize(x);
            }

        }

        public static class Mod998 extends NumberTheory.Modulus<NumberTheory.Mod998> {
            public long modulus() {
                return 998_244_353L;
            }

        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author prakharjain
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        int mod = 998244353;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            long n = in.nextInt();

            long ans = 0;

            long[] _3pow = new long[(int) n + 1];

            _3pow[0] = 1;

            for (int i = 1; i <= n; i++) {
                _3pow[i] = 3 * _3pow[i - 1];
                _3pow[i] %= mod;
            }

            long[] ncr = new long[(int) n + 1];

            ncr[0] = 1;

            for (int i = 1; i <= n; i++) {
                ncr[i] = ncr[i - 1] * (n - (i - 1));
                ncr[i] %= mod;
                ncr[i] *= pow(i, mod - 2, mod);
                ncr[i] %= mod;
            }

            for (int i = 1; i <= n; i++) {
                long val = _3pow[i] * pow(3, (n * (n - i)) % mod, mod);
                val %= mod;
                val *= ncr[i];
                val %= mod;
                if (i % 2 == 0)
                    ans -= 2 * val;
                else
                    ans += 2 * val;
                ans %= mod;
            }

            for (int i = 0; i < n; i++) {
                long val1 = 3 * ncr[i];
                val1 %= mod;
                long val2 = 1 - _3pow[i];
                val2 %= mod;
                long val3 = pow(val2, n, mod);
                val3 -= pow(-_3pow[i], n, mod);
                val1 *= val3;
                val1 %= mod;
                if (i % 2 == 0)
                    ans -= val1;
                else
                    ans += val1;
                ans %= mod;
            }

            ans += mod;
            ans %= mod;
            out.println(ans);
        }

        long pow(long a, long p, int mod) {
            if (p == 0) {
                return 1;
            }

            long t = pow(a, p / 2, mod);

            if (p % 2 != 0) {
                return (((t * t) % mod) * a) % mod;
            } else {
                return (t * t) % mod;
            }
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

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

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

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

        public void println(long i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

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

        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 nextInt() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        }

    }
}

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 kessido
 */
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);
        CSkyFullOfStars solver = new CSkyFullOfStars();
        solver.solve(1, in, out);
        out.close();
    }

    static class CSkyFullOfStars {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            final long MOD = 998244353;
            long n = in.NextInt();
            long[] inv = MathExtensions.InverseModulo.getInverseModulo((int) n, MOD);
            long[] nChooseI = new long[(int) (n + 1)];
            nChooseI[0] = 1;
            for (int i = 1; i <= n; i++)
                nChooseI[i] = nChooseI[i - 1] * (n - i + 1) % MOD * inv[i] % MOD;
            long res = MathExtensions.powerMod(3, n * n, MOD);
            res -= (MathExtensions.powerMod(MathExtensions.powerMod(3, n, MOD) - 3, n, MOD)) % MOD;
            res %= MOD;
            int sign = 1;
            for (int i = 1; i <= n; i++) {
                long addToRes = 0;
                addToRes = MathExtensions.powerMod(3, i, MOD) - 3;
                addToRes = addToRes * MathExtensions.powerMod(3, (n - i) * n, MOD) % MOD;
                addToRes += 3 * MathExtensions.powerMod(MathExtensions.powerMod(3, n - i, MOD) - 1, n, MOD) % MOD;
                addToRes %= MOD;
                addToRes *= sign * nChooseI[i];
                addToRes %= MOD;
                sign *= -1;
                res = (res + addToRes) % MOD;
            }
            out.println(res);
        }

    }

    static class MathExtensions {
        public static long powerMod(final long x, final long y, final long m) {
            if (y == 0)
                return 1;

            long p = powerMod(x, y / 2, m) % m;
            p = (p * p) % m;

            if (y % 2 == 0)
                return p;
            else
                return (x * p) % m;
        }

        public static class InverseModulo {
            public static long[] getInverseModulo(final int max, final long MOD) {
                long[] inv = new long[max + 1];
                if (max == 0) return inv;
                inv[1] = 1;
                for (int i = 2; i <= max; i++) {
                    inv[i] = (MOD - MOD / i) * inv[(int) (MOD % i)] % MOD;
                }
                return inv;
            }

        }

    }

    static class InputReader {
        BufferedReader reader;
        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(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}

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 SkyFullOfStars2 {

    int N = (int) 1e6 + 10;
    int MOD = 998244353;

    long[] fac = new long[N], ifac = new long[N], inv = new long[N];

    {
        inv[1] = 1;
        for (int i = 2; i < N; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;

        fac[0] = fac[1] = 1;
        ifac[0] = ifac[1] = 1;
        for (int i = 2; i < N; i++) {
            fac[i] = fac[i - 1] * i % MOD;
            ifac[i] = ifac[i - 1] * inv[i] % MOD;
        }
    }

    int n;

    void solve() {
        n = in.nextInt();

        long[] c = new long[n + 1];
        c[0] = 1;
        for (int i = 1; i <= n; i++) c[i] = fac[n] * ifac[i] % MOD * ifac[n - i] % MOD;

        long ans = 0;
        for (int i = 0; i < n; i++) {
            long delta = c[i] * (pow((1 - pow(3, i) + MOD) % MOD, n) - pow((MOD - pow(3, i)) % MOD, n) + MOD) % MOD;
            if ((i + 1) % 2 == 0) ans = (ans + delta) % MOD;
            else ans = (ans - delta + MOD) % MOD;
        }
        ans = 3 * ans % MOD;

        for (int i = 1; i <= n; i++) {
            long delta = 2 * c[i] % MOD * pow(3, i + n * (n - i)) % MOD;
            if (i % 2 == 1) ans = (ans + delta) % MOD;
            else ans = (ans - delta + MOD) % MOD;
        }

        out.println(ans);
    }

    long pow(long a, long n) {
        long res = 1;
        while (n > 0) {
            if ((n & 1) > 0) res = res * a % MOD;
            a = a * a % MOD;
            n >>= 1;
        }
        return res;
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new SkyFullOfStars2().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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class MainE {

    private FastScanner in;
    private PrintWriter out;

    long MOD = 998244353L;

    long[] gcd_ex(long a, long b) {
        if (a == 0) {
            return new long[]{0, 1};
        }
        long[] res = gcd_ex(b % a, a);
        long t = res[0];
        res[0] = res[1] - (b / a) * res[0];
        res[1] = t;
        return res;
    }

    long reverse(long a) {
        return (gcd_ex(a, MOD)[0] + 100 * MOD) % MOD;
    }

    long powMod(long a, long b) {
        long res = 1;
        long mul = a;
        while (b > 0) {
            if ((b & 1) > 0) {
                res = (res * mul) % MOD;
            }
            mul = (mul * mul) % MOD;
            b /= 2;
        }
        return res;
    }

    public void solve() {
        long N = in.nextInt();
        long[] combs = new long[(int)(N + 1)];
        combs[0] = 1;
        for (int i = 1; i <= N; i++) {
            long t = combs[i - 1];
            t = (t * (N - i + 1)) % MOD;
            t = (t * reverse(i)) % MOD;
            combs[i] = t;
        }
        long res = 0;
        long p1 = 1;
        long p2 = 1;
        long R = powMod(reverse(3), N);
        for (int i = 1; i <= N; i++) {
            p1 = (p1 * R) % MOD;
            p2 = (p2 * 3) % MOD;
            long t = combs[i] * p1 % MOD;
            t = (t * p2) % MOD;
            if ((i & 1) > 0) {
                res += 2 * t;
            } else {
                res = (res + 2 * MOD - 2 * t) % MOD;
            }
            res %= MOD;
        }
        res = res * powMod(3, N * N) % MOD;
        long i3 = 1;
        for (int i = 0; i < N; i++){
            long mi3 = (MOD - i3) % MOD;
            i3 = (i3 * 3) % MOD;
            long t = combs[i] * ((MOD + powMod(1 + mi3, N) - powMod(mi3, N)) % MOD);
            if ((i & 1) > 0) {
                res += 3 * t;
            } else {
                res = (res + 3 * MOD - 3 * t) % MOD;
            }
            res %= MOD;
        }
        System.out.println(res);
    }

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

    class FastScanner {

        BufferedReader br;
        StringTokenizer st;

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

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

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

    public static void main(String[] arg) {
        new MainE().run();
    }
}