#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
  a += b;
  if (a < 0) a += MOD;
  if (a >= MOD) a -= MOD;
  return a;
}
int mul(long long a, int b) {
  a *= b;
  a %= MOD;
  return a;
}
int mpow(int a, int m) {
  int res = 1;
  for (; m;) {
    if (m % 2 == 0) {
      a = mul(a, a);
      m /= 2;
    } else {
      res = mul(res, a);
      m--;
    }
  }
  return res;
}
int inv(int a) { return mpow(a, MOD - 2); }
const int MAXFAC = 1e6 + 3;
int fac[MAXFAC + 1];
int Cnk(int n, int k) { return mul(mul(fac[n], inv(fac[k])), inv(fac[n - k])); }
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  fac[0] = fac[1] = 1;
  for (int(i) = (2); (i) < (MAXFAC + 1); (i)++) {
    fac[i] = mul(i, fac[i - 1]);
  }
  int res = 0;
  for (int(i) = (1); (i) < (n + 1); (i)++) {
    int sign = i % 2 == 1 ? 1 : MOD - 1;
    int triPow = mpow(3, i);
    int triPow2Tmp = mpow(3, n - i);
    int triPow2 = mpow(triPow2Tmp, n);
    int cnk = Cnk(n, i);
    int two = 2;
    int term = mul(mul(mul(mul(sign, triPow), triPow2), cnk), two);
    res = add(res, term);
  }
  for (int(i) = (1); (i) < (n + 1); (i)++) {
    int minus3 = add(0, -3);
    int sign = i % 2 == 0 ? 1 : MOD - 1;
    int cnk = Cnk(n, i);
    int part3n_i = mpow(3, n - i);
    int part1 = add(part3n_i, -1);
    part1 = mpow(part1, n);
    int part2 = part3n_i;
    part2 = mpow(part2, n);
    int rightPart = add(part1, -part2);
    int term = mul(mul(mul(minus3, sign), cnk), rightPart);
    res = add(res, term);
  }
  cout << res << endl;
  return 0;
}
